import { Exercise } from "@/interfaces/exam";
import ExerciseItem, { isExerciseItem } from "./types";
import MultipleChoice from "../../Exercises/MultipleChoice";
import ExerciseLabel from "../../Shared/ExerciseLabel";
import writeBlanks from "./writeBlanks";
import TrueFalse from "../../Exercises/TrueFalse";
import fillBlanks from "./fillBlanks";
import MatchSentences from "../../Exercises/MatchSentences";
import Writing from "../../Exercises/Writing";
import Speaking2 from "../../Exercises/Speaking/Speaking2";
import Speaking1 from "../../Exercises/Speaking/Speaking1";
import InteractiveSpeaking from "../../Exercises/Speaking/InteractiveSpeaking";

const getExerciseItems = (exercises: Exercise[], sectionId: number): ExerciseItem[] => {
    const items: ExerciseItem[] = exercises.map((exercise, index) => {
        let firstQuestionId, lastQuestionId;
        switch (exercise.type) {
            case "multipleChoice":
                firstQuestionId = exercise.questions[0].id;
                lastQuestionId = exercise.questions[exercise.questions.length - 1].id;
                return {
                    id: index.toString(),
                    sectionId,
                    label: (
                        <ExerciseLabel
                            type='Multiple Choice Questions'
                            firstId={firstQuestionId}
                            lastId={lastQuestionId}
                            prompt={exercise.prompt}
                        />
                    ),
                    content: <MultipleChoice exercise={exercise} sectionId={sectionId} />
                };
            case "trueFalse":
                firstQuestionId = exercise.questions[0].id
                lastQuestionId = exercise.questions[exercise.questions.length - 1].id;
                return {
                    id: index.toString(),
                    sectionId,
                    label: (
                        <ExerciseLabel
                            type='True/False/Not Given'
                            firstId={firstQuestionId}
                            lastId={lastQuestionId}
                            prompt={exercise.prompt}
                        />
                    ),
                    content: <TrueFalse exercise={exercise} sectionId={sectionId} />
                };
            case "matchSentences":
                firstQuestionId = exercise.sentences[0].id;
                lastQuestionId = exercise.sentences[exercise.sentences.length - 1].id;
                return {
                    id: index.toString(),
                    sectionId,
                    label: (
                        <ExerciseLabel
                            type={exercise.variant == "ideaMatch" ? "Idea Match" : "Paragraph Match"}
                            firstId={firstQuestionId}
                            lastId={lastQuestionId}
                            prompt={exercise.prompt}
                        />
                    ),
                    content: <MatchSentences exercise={exercise} sectionId={sectionId} />
                };
            case "fillBlanks":
                return fillBlanks(exercise, index, sectionId);
            case "writeBlanks":
                return writeBlanks(exercise, index, sectionId);
            case "writing":
                return  {
                    id: index.toString(),
                    sectionId,
                    label: (
                        <ExerciseLabel
                            type={`Writing Task: ${exercise.variant === "letter" ? "Letter" : "Essay"}`}
                            firstId={exercise.sectionId!.toString()}
                            lastId={exercise.sectionId!.toString()}
                            prompt={exercise.prompt}
                        />
                    ),
                    content: <Writing key={exercise.id} exercise={exercise} sectionId={sectionId} index={index} module="level" />
                };
            case "speaking":
                return {
                    exerciseId: exercise.id,
                    id: index.toString(),
                    sectionId,
                    label: (
                        <ExerciseLabel
                            type={`Speaking Section 2: Question`}
                            firstId={(index+1).toString()}
                            lastId={(index+1).toString()}
                            prompt={exercise.prompts[2]}
                        />
                    ),
                    content: <Speaking2 key={exercise.id} exercise={exercise} sectionId={sectionId} module="level" />
                };
            case "interactiveSpeaking":
                const content = exercise.sectionId === 1 ? <Speaking1 key={exercise.id} exercise={exercise} sectionId={sectionId} module="level" /> :
                <InteractiveSpeaking key={exercise.id} exercise={exercise} sectionId={sectionId} module="level"/>
                return {
                    exerciseId: exercise.id,
                    id: index.toString(),
                    sectionId,
                    label: (
                        <ExerciseLabel
                            type={`${exercise.sectionId === 1 ? 'Speaking Section 1': 'Interactive Speaking'}: Question`}
                            firstId={(index+1).toString()}
                            lastId={(index+1).toString()}
                            prompt={exercise.prompts[2].text}
                        />
                    ),
                    content: content
                };
            default:
                return {} as unknown as ExerciseItem;
        }
    }).filter(isExerciseItem);
    return items;
};


export default getExerciseItems;
