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

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

export interface CommonProps {
	examID?: string;
	onNext: (userSolutions: UserSolution) => void;
	onBack: (userSolutions: UserSolution) => void;
	enableNavigation?: boolean;
	disableProgressButtons?: boolean
	preview?: boolean;
}

export const renderExercise = (
	exercise: Exercise,
	examID: string,
	onNext: (userSolutions: UserSolution) => void,
	onBack: (userSolutions: UserSolution) => void,
	enableNavigation?: boolean,
	disableProgressButtons?: boolean,
	preview?: boolean,
) => {
	switch (exercise.type) {
		case "fillBlanks":
			return <FillBlanks disableProgressButtons={disableProgressButtons} key={exercise.id} {...(exercise as FillBlanksExercise)} onNext={onNext} onBack={onBack} examID={examID} preview={preview} />;
		case "trueFalse":
			return <TrueFalse disableProgressButtons={disableProgressButtons} key={exercise.id} {...(exercise as TrueFalseExercise)} onNext={onNext} onBack={onBack} examID={examID} preview={preview} />;
		case "matchSentences":
			return <MatchSentences disableProgressButtons={disableProgressButtons} key={exercise.id} {...(exercise as MatchSentencesExercise)} onNext={onNext} onBack={onBack} examID={examID} preview={preview} />;
		case "multipleChoice":
			return <MultipleChoice disableProgressButtons={disableProgressButtons} key={exercise.id} {...(exercise as MultipleChoiceExercise)} onNext={onNext} onBack={onBack} examID={examID} preview={preview} />;
		case "writeBlanks":
			return <WriteBlanks disableProgressButtons={disableProgressButtons} key={exercise.id} {...(exercise as WriteBlanksExercise)} onNext={onNext} onBack={onBack} examID={examID} preview={preview} />;
		case "writing":
			return <Writing key={exercise.id} {...(exercise as WritingExercise)} onNext={onNext} onBack={onBack} examID={examID} enableNavigation={enableNavigation} preview={preview} />;
		case "speaking":
			return <Speaking key={exercise.id} {...(exercise as SpeakingExercise)} onNext={onNext} onBack={onBack} examID={examID} preview={preview} />;
		case "interactiveSpeaking":
			return (
				<InteractiveSpeaking
					key={exercise.id}
					{...(exercise as InteractiveSpeakingExercise)}
					examID={examID}
					onNext={onNext}
					onBack={onBack}
					preview={preview}
				/>
			);
	}
};
