import { AccessType, Difficulty, InteractiveSpeakingExercise, LevelPart, ListeningPart, ReadingPart, Script, SpeakingExercise, WritingExercise } from "@/interfaces/exam";
import { Module } from "@/interfaces";
import Option from "@/interfaces/option";

export interface GeneratedExercises {
    exercises: Record<string, string>[];
    sectionId: number;
    module: string;
}

export interface SectionSettings {
    category: string;
    introOption: Option;
    customIntro: string;
    currentIntro: string | undefined;
    isCategoryDropdownOpen: boolean;
    isIntroDropdownOpen: boolean;
    isTypeDropdownOpen: boolean;
}

export interface SpeakingSectionSettings extends SectionSettings {
    speakingTopic: string;
    speakingSecondTopic?: string;
    isSpeakingTopicOpen: boolean;
    isGenerateVideoOpen: boolean;
}

export interface ReadingSectionSettings extends SectionSettings {
    isPassageOpen: boolean;
    readingTopic: string;
    isReadingTopicOpean: boolean;
}

export interface ListeningSectionSettings extends SectionSettings {
    isAudioContextOpen: boolean;
    isAudioGenerationOpen: boolean;
    listeningTopic: string;
    isListeningTopicOpen: boolean;
    uploadedAudioURL: string | undefined;
    audioCutURL: string | undefined;
    useEntireAudioFile: boolean;
}

export interface WritingSectionSettings extends SectionSettings {
    isWritingTopicOpen: boolean;
    writingTopic: string;
    isImageUploadOpen: boolean;
}

export interface LevelSectionSettings extends SectionSettings {
    isReadingDropdownOpen: boolean;
    isWritingDropdownOpen: boolean;
    isSpeakingDropdownOpen: boolean;
    isListeningDropdownOpen: boolean;
    isLevelDropdownOpen: boolean;
    readingSection?: number;
    listeningSection?: number;

    // writing
    isWritingTopicOpen: boolean;
    isImageUploadOpen: boolean;
    writingTopic: string;

    // reading
    isPassageOpen: boolean;
    readingTopic: string;
    isReadingTopicOpean: boolean;

    // listening
    isAudioContextOpen: boolean;
    isAudioGenerationOpen: boolean;
    listeningTopic: string;
    isListeningTopicOpen: boolean;

    // speaking
    speakingTopic?: string;
    speakingSecondTopic?: string;
    isSpeakingTopicOpen: boolean;
    isGenerateVideoOpen: boolean;
    isSpeakingExercisesOpen: boolean;
    isConfigureExercisesOpen: boolean;

    // section picker
    isReadingPickerOpen: boolean;
    isListeningPickerOpen: boolean;
}

export type Context = "passage" | "video" | "audio" | "listeningScript" | "speakingScript" | "writing";
export type Generating = Context | "exercises" | string | undefined;
export type LevelGenResults = { generating: string, result: Record<string, any>[], module: Module };
export type Section = LevelPart | ReadingPart | ListeningPart | WritingExercise | SpeakingExercise | InteractiveSpeakingExercise;
export type ExamPart = ListeningPart | ReadingPart | LevelPart;

export interface SectionState {
    sectionId: number;
    settings: SectionSettings;
    state: Section;
    expandedSubSections: number[];
    generating: Generating;
    genResult: { generating: string, result: Record<string, any>[], module: Module } | undefined;
    levelGenerating: Generating[];
    levelGenResults: LevelGenResults[];
    focusedExercise?: { questionId: number; id: string } | undefined;
    writingSection?: number;
    speakingSection?: number;
    readingSection?: number;
    listeningSection?: number;
    scriptLoading: boolean;
}

export interface ListeningInstructionsState {
    isInstructionsOpen: boolean;
    chosenOption: Option;

    currentInstructions: string;
    presetInstructions: string;
    customInstructions: string;

    currentInstructionsURL: string;
    presetInstructionsURL: string;
    customInstructionsURL: string;
}

export interface ModuleState {
    examLabel: string;
    sections: SectionState[];
    minTimer: number;
    difficulty: Difficulty[];
    access: AccessType;
    sectionLabels: { id: number; label: string; }[];
    expandedSections: number[];
    focusedSection: number;
    importModule: boolean;
    importing: boolean;
    edit: number[];
    type?: "general" | "academic";
    academic_url?: string | undefined;
    instructionsState: ListeningInstructionsState;
}

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

export default interface ExamEditorStore {
    title: string;
    currentModule: Module;
    speakingAvatars: Avatar[];
    modules: {
        [K in Module]: ModuleState
    };
}
