import {
	Exercise,
	FillBlanksExercise,
	InteractiveSpeakingExercise,
	MatchSentencesExercise,
	MultipleChoiceExercise,
	SpeakingExercise,
	TrueFalseExercise,
	UserSolution,
	WriteBlanksExercise,
	WritingExercise,
} from "@/interfaces/exam";
import dynamic from "next/dynamic";
import FillBlanks from "./FillBlanks";
import InteractiveSpeaking from "./InteractiveSpeaking";
import MultipleChoice from "./MultipleChoice";
import Speaking from "./Speaking";
import TrueFalseSolution from "./TrueFalse";
import WriteBlanks from "./WriteBlanks";
import Writing from "./Writing";

const MatchSentences = dynamic(() => import("@/components/Solutions/MatchSentences"), {ssr: false});

export interface CommonProps {
	updateIndex?: (internalIndex: number) => void;
	onNext: (userSolutions: UserSolution) => void;
	onBack: (userSolutions: UserSolution) => void;
}

export const renderSolution = (exercise: Exercise, onNext: () => void, onBack: () => void, updateIndex?: (internalIndex: number) => void) => {
	switch (exercise.type) {
		case "fillBlanks":
			return <FillBlanks key={exercise.id} {...(exercise as FillBlanksExercise)} onNext={onNext} onBack={onBack} />;
		case "trueFalse":
			return <TrueFalseSolution key={exercise.id} {...(exercise as TrueFalseExercise)} onNext={onNext} onBack={onBack} />;
		case "matchSentences":
			return <MatchSentences key={exercise.id} {...(exercise as MatchSentencesExercise)} onNext={onNext} onBack={onBack} />;
		case "multipleChoice":
			return (
				<MultipleChoice
					key={exercise.id}
					{...(exercise as MultipleChoiceExercise)}
					updateIndex={updateIndex}
					onNext={onNext}
					onBack={onBack}
				/>
			);
		case "writeBlanks":
			return <WriteBlanks key={exercise.id} {...(exercise as WriteBlanksExercise)} onNext={onNext} onBack={onBack} />;
		case "writing":
			return <Writing key={exercise.id} {...(exercise as WritingExercise)} onNext={onNext} onBack={onBack} />;
		case "speaking":
			return <Speaking key={exercise.id} {...(exercise as SpeakingExercise)} onNext={onNext} onBack={onBack} />;
		case "interactiveSpeaking":
			return <InteractiveSpeaking key={exercise.id} {...(exercise as InteractiveSpeakingExercise)} onNext={onNext} onBack={onBack} />;
	}
};
