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";

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" />
                };
            default:
                return {} as unknown as ExerciseItem;
        }
    }).filter(isExerciseItem);
    /*return mappedItems.filter((item): item is ExerciseItem => 
       item !== null && isExerciseItem(item)
   );*/
    return items;
};


export default getExerciseItems;
