import ExerciseItem, { ReadingExercise } from './types';
import WriteBlanks from "@/editor/Exercises/WriteBlanks";
import ExerciseLabel from '../../Shared/ExerciseLabel';
import MatchSentences from '../../Exercises/MatchSentences';
import TrueFalse from '../../Exercises/TrueFalse';
import FillBlanksLetters from '../../Exercises/Blanks/Letters';

const getExerciseItems = (exercises: ReadingExercise[], sectionId: number): ExerciseItem[] => {

    const previewLabel = (text: string) => {
        return text !== undefined ? text.replaceAll('\\n', ' ').split(' ').slice(0, 15).join(' ') : ""
    }

    const items: ExerciseItem[] = exercises.map((exercise, index) => {
        let firstWordId, lastWordId;

        switch (exercise.type) {
            case "fillBlanks":
                firstWordId = exercise.solutions[0].id;
                lastWordId = exercise.solutions[exercise.solutions.length - 1].id;
                return {
                    id: index,
                    sectionId,
                    label: (
                        <ExerciseLabel
                            label={`Fill Blanks Question #${firstWordId} ${firstWordId === lastWordId ? '' : `- #${lastWordId}`}`}
                            preview={
                                <>
                                    &quot;{previewLabel(exercise.prompt)}...&quot;
                                </>
                            }
                        />
                    ),
                    content: <FillBlanksLetters exercise={exercise} sectionId={sectionId} />
                };
            case "writeBlanks":
                firstWordId = exercise.solutions[0].id;
                lastWordId = exercise.solutions[exercise.solutions.length - 1].id;
                return {
                    id: index,
                    sectionId,
                    label: (
                        <ExerciseLabel
                            label={`Write Blanks Question #${firstWordId} ${firstWordId === lastWordId ? '' : `- #${lastWordId}`}`}
                            preview={
                                <>
                                    &quot;{previewLabel(exercise.prompt)}...&quot;
                                </>
                            }
                        />
                    ),
                    content: <WriteBlanks exercise={exercise} sectionId={sectionId} />
                };
            case "matchSentences":
                firstWordId = exercise.sentences[0].id;
                lastWordId = exercise.sentences[exercise.sentences.length - 1].id;
                return {
                    id: index,
                    sectionId,
                    label: (
                        <ExerciseLabel
                            label={`${exercise.variant == "ideaMatch" ? "Idea Match" : "Paragraph Match"} #${firstWordId} ${firstWordId === lastWordId ? '' : `- #${lastWordId}`}`}
                            preview={
                                <>
                                    &quot;{previewLabel(exercise.prompt)}...&quot;
                                </>
                            }
                        />
                    ),
                    content: <MatchSentences exercise={exercise} sectionId={sectionId}/>
                };
            case "trueFalse":
                firstWordId = exercise.questions[0].id
                lastWordId = exercise.questions[exercise.questions.length - 1].id;
                return {
                    id: index,
                    sectionId,
                    label: (
                        <ExerciseLabel
                            label={`True/False/Not Given #${firstWordId} ${firstWordId === lastWordId ? '' : `- #${lastWordId}`}`}
                            preview={
                                <>
                                    &quot;{previewLabel(exercise.prompt)}...&quot;
                                </>
                            }
                        />
                    ),
                    content: <TrueFalse exercise={exercise} sectionId={sectionId} />
                };

        }
    }).filter((item) => item !== undefined);

    return items || [];
};


export default getExerciseItems;
