import useExamEditorStore from "@/stores/examEditor";
import { LevelSectionSettings, SpeakingSectionSettings } from "@/stores/examEditor/types";
import { useCallback, useEffect, useState } from "react";
import { generate } from "../Shared/Generate";
import Dropdown from "../Shared/SettingsDropdown";
import Input from "@/components/Low/Input";
import GenerateBtn from "../Shared/GenerateBtn";
import clsx from "clsx";
import { FaFemale, FaMale } from "react-icons/fa";
import { InteractiveSpeakingExercise, LevelPart, SpeakingExercise } from "@/interfaces/exam";
import { toast } from "react-toastify";
import { generateVideos } from "../Shared/generateVideos";
import { Module } from "@/interfaces";
import useCanGenerate from "./useCanGenerate";

export interface Avatar {
    name: string;
    gender: string;
}

interface Props {
    localSettings: SpeakingSectionSettings | LevelSectionSettings;
    updateLocalAndScheduleGlobal: (updates: Partial<SpeakingSectionSettings | LevelSectionSettings>, schedule?: boolean) => void;
    section: SpeakingExercise | InteractiveSpeakingExercise | LevelPart;
    level?: boolean;
    module?: Module;
    id?: string;
    sectionId?: number;
}

const SpeakingComponents: React.FC<Props> = ({ localSettings, updateLocalAndScheduleGlobal, section, level = false, module = "speaking", id, sectionId }) => {

    const { currentModule, speakingAvatars, dispatch, modules } = useExamEditorStore();
    const { focusedSection, difficulty, sections } = useExamEditorStore((store) => store.modules[level ? "level" : currentModule])
    const state = sections.find((s) => s.sectionId === sectionId);

    const [selectedAvatar, setSelectedAvatar] = useState<Avatar | null>(null);

    const generateScript = useCallback((scriptSectionId: number) => {
        const queryParams: {
            difficulty: string;
            first_topic?: string;
            second_topic?: string;
            topic?: string;
        } = { difficulty };

        if (scriptSectionId === 1) {
            if (localSettings.speakingTopic) {
                queryParams['first_topic'] = localSettings.speakingTopic;
            }
            if (localSettings.speakingSecondTopic) {
                queryParams['second_topic'] = localSettings.speakingSecondTopic;
            }
        } else {
            if (localSettings.speakingTopic) {
                queryParams['topic'] = localSettings.speakingTopic;
            }
        }
        generate(
            level ? section.sectionId! : focusedSection,
            "speaking",
            `${id ? `${id}-` : ''}speakingScript`,
            {
                method: 'GET',
                queryParams
            },
            (data: any) => {
                switch (level ? section.sectionId! : focusedSection) {
                    case 1:
                        return [{
                            prompts: data.questions,
                            first_topic: data.first_topic,
                            second_topic: data.second_topic
                        }];
                    case 2:
                        return [{
                            topic: data.topic,
                            question: data.question,
                            prompts: data.prompts,
                            suffix: data.suffix
                        }];
                    case 3:
                        return [{
                            title: data.topic,
                            prompts: data.questions
                        }];
                    default:
                        return [data];
                }
            },
            sectionId,
            level
        );

    }, [difficulty, level, section.sectionId, focusedSection, id, sectionId, localSettings.speakingTopic, localSettings.speakingSecondTopic]);

    const onTopicChange = useCallback((speakingTopic: string) => {
        updateLocalAndScheduleGlobal({ speakingTopic });
    }, [updateLocalAndScheduleGlobal]);

    const onSecondTopicChange = useCallback((speakingSecondTopic: string) => {
        updateLocalAndScheduleGlobal({ speakingSecondTopic });
    }, [updateLocalAndScheduleGlobal]);

    const canGenerate = useCanGenerate({
        section,
        sections,
        id,
        focusedSection
    });

    useEffect(() => {
        if (!canGenerate) {
            updateLocalAndScheduleGlobal({ isGenerateVideoOpen: false }, false);
        }
    }, [canGenerate, updateLocalAndScheduleGlobal]);

    const generateVideoCallback = useCallback((sectionId: number) => {
        if (level) {
            dispatch({ type: "UPDATE_SECTION_SINGLE_FIELD", payload: { sectionId, module: "level", field: "levelGenerating", value: [...state!.levelGenerating, `${id ? `${id}-` : ''}video`] } })
        } else {
            dispatch({ type: "UPDATE_SECTION_SINGLE_FIELD", payload: { sectionId: focusedSection, module: "speaking", field: "generating", value: "video" } })
        }

        generateVideos(
            section as InteractiveSpeakingExercise | SpeakingExercise,
            level ? section.sectionId! : focusedSection,
            selectedAvatar,
            speakingAvatars
        ).then((results) => {
            switch (level ? section.sectionId! : focusedSection) {
                case 1:
                case 3: {
                    const interactiveSection = section as InteractiveSpeakingExercise;
                    const updatedPrompts = interactiveSection.prompts.map((prompt, index) => ({
                        ...prompt,
                        video_url: results[index].url || ''
                    }));
                    if (level) {
                        dispatch({
                            type: "UPDATE_SECTION_SINGLE_FIELD", payload: {
                                sectionId, field: "levelGenResults", value: [...state!.levelGenResults,
                                { generating: `${id ? `${id}-` : ''}video`, result: [{ prompts: updatedPrompts }] }], module: "level"
                            }
                        })
                    } else {
                        dispatch({
                            type: "UPDATE_SECTION_SINGLE_FIELD", payload: {
                                sectionId: focusedSection, module: "speaking", field: "genResult", value:
                                    { generating: "video", result: [{ prompts: updatedPrompts }], module: module }
                            }
                        })
                    }
                    break;
                }
                case 2: {
                    if (results[0]?.url) {
                        if (level) {
                            dispatch({
                                type: "UPDATE_SECTION_SINGLE_FIELD", payload: {
                                    sectionId, field: "levelGenResults", value: [...state!.levelGenResults,
                                    { generating: `${id ? `${id}-` : ''}video`, result: [{ video_url: results[0].url }] }], module: "level"
                                }
                            })
                        } else {
                            dispatch({
                                type: "UPDATE_SECTION_SINGLE_FIELD", payload: {
                                    sectionId: focusedSection, module, field: "genResult", value:
                                        { generating: 'video', result: [{ video_url: results[0].url }], module: "speaking" }
                                }
                            })
                        }
                    }
                    break;
                }
            }
        }).catch((error) => {
            toast.error("Failed to generate the video, try again later!")
        });
        
    }, [level, section, focusedSection, selectedAvatar, speakingAvatars, dispatch, module, state, id]);

    const secId = level ? section.sectionId! : focusedSection;

    return (
        <>
            <Dropdown
                title="Generate Script"
                module="speaking"
                open={localSettings.isSpeakingTopicOpen}
                setIsOpen={(isOpen: boolean) => updateLocalAndScheduleGlobal({ isSpeakingTopicOpen: isOpen }, false)}
                contentWrapperClassName={level ? `border border-ielts-speaking` : ''}
            >

                <div className={clsx("gap-2 px-2 pb-4", secId === 1 ? "flex flex-col w-full" : "flex flex-row items-center")}>
                    <div className="flex flex-col flex-grow gap-4 px-2">
                        <label className="font-normal text-base text-mti-gray-dim">{`${secId === 1 ? "First Topic" : "Topic"}`} (Optional)</label>
                        <Input
                            key={`section-${secId}`}
                            type="text"
                            placeholder="Topic"
                            name="category"
                            onChange={onTopicChange}
                            roundness="full"
                            value={localSettings.speakingTopic}
                        />
                    </div>
                    {secId === 1 &&
                        <div className="flex flex-col flex-grow gap-4 px-2">
                            <label className="font-normal text-base text-mti-gray-dim">Second Topic (Optional)</label>
                            <Input
                                key={`section-${secId}`}
                                type="text"
                                placeholder="Topic"
                                name="category"
                                onChange={onSecondTopicChange}
                                roundness="full"
                                value={localSettings.speakingSecondTopic}
                            />
                        </div>
                    }
                    <div className={clsx("flex h-16 mb-1", secId === 1 ? "justify-center mt-4" : "self-end")}>
                        <GenerateBtn
                            module="speaking"
                            genType={`${id ? `${id}-` : ''}speakingScript`}
                            sectionId={focusedSection}
                            generateFnc={generateScript}
                            level={level}
                        />
                    </div>
                </div>
            </Dropdown>
            <Dropdown
                title="Generate Video"
                module="speaking"
                open={localSettings.isGenerateVideoOpen}
                disabled={!canGenerate}
                setIsOpen={(isOpen: boolean) => updateLocalAndScheduleGlobal({ isGenerateVideoOpen: isOpen }, false)}
                contentWrapperClassName={level ? `border border-ielts-speaking` : ''}
            >
                <div className={clsx("flex items-center justify-between gap-4 px-2 pb-4")}>
                    <div className="relative flex-1 max-w-xs">
                        <select
                            value={selectedAvatar ? `${selectedAvatar.name}-${selectedAvatar.gender}` : ""}
                            onChange={(e) => {
                                if (e.target.value === "") {
                                    setSelectedAvatar(null);
                                } else {
                                    const [name, gender] = e.target.value.split("-");
                                    const avatar = speakingAvatars.find(a => a.name === name && a.gender === gender);
                                    if (avatar) setSelectedAvatar(avatar);
                                }
                            }}
                            className="w-full appearance-none px-4 py-2 border border-gray-200 rounded-full text-base bg-white focus:ring-1 focus:ring-blue-500 focus:outline-none"
                        >
                            <option value="">Select an avatar (Optional)</option>
                            {speakingAvatars.map((avatar) => (
                                <option
                                    key={`${avatar.name}-${avatar.gender}`}
                                    value={`${avatar.name}-${avatar.gender}`}
                                >
                                    {avatar.name}
                                </option>
                            ))}
                        </select>
                        <div className="absolute right-2.5 top-2.5 pointer-events-none">
                            {selectedAvatar && (
                                selectedAvatar.gender === 'male' ? (
                                    <FaMale className="w-5 h-5 text-blue-500" />
                                ) : (
                                    <FaFemale className="w-5 h-5 text-pink-500" />
                                )
                            )}
                        </div>
                    </div>

                    <GenerateBtn
                        module="speaking"
                        genType={`${id ? `${id}-` : ''}video`}
                        sectionId={focusedSection}
                        generateFnc={generateVideoCallback}
                        level={level}
                    />
                </div>
            </Dropdown>
        </>
    );
};

export default SpeakingComponents;
