import {MatchSentenceExerciseSentence, MatchSentencesExercise} from "@/interfaces/exam";
import clsx from "clsx";
import LineTo from "react-lineto";
import {CommonProps} from ".";
import {errorButtonStyle, infoButtonStyle} from "@/constants/buttonStyles";
import {mdiArrowLeft, mdiArrowRight} from "@mdi/js";
import Icon from "@mdi/react";
import {Fragment} from "react";
import Button from "../Low/Button";
import Xarrow from "react-xarrows";
import useExamStore from "@/stores/examStore";

function QuestionSolutionArea({
	question,
	userSolution,
}: {
	question: MatchSentenceExerciseSentence;
	userSolution?: {question: string; option: string};
}) {
	return (
		<div className="grid grid-cols-3 gap-4">
			<div className="flex items-center gap-3 cursor-pointer col-span-2">
				<button
					className={clsx(
						"text-white w-8 h-8 rounded-full z-10",
						!userSolution
							? "bg-mti-gray-davy"
							: userSolution.option.toString() === question.solution.toString()
							? "bg-mti-purple"
							: "bg-mti-rose",
						"transition duration-300 ease-in-out",
					)}>
					{question.id}
				</button>
				<span>{question.sentence}</span>
			</div>
			<div
				className={clsx(
					"w-56 h-10 border rounded-xl items-center justify-center flex gap-3 px-2",
					!userSolution
						? "border-mti-gray-davy"
						: userSolution.option.toString() === question.solution.toString()
						? "border-mti-purple"
						: "border-mti-rose",
				)}>
				<span className="line-through">
					{userSolution && userSolution?.option.toString() !== question.solution.toString() && `Paragraph ${userSolution.option}`}
				</span>
				<span className="font-semibold">Paragraph {question.solution}</span>
			</div>
		</div>
	);
}

export default function MatchSentencesSolutions({
	id,
	type,
	options,
	prompt,
	sentences,
	userSolutions,
	onNext,
	onBack,
}: MatchSentencesExercise & CommonProps) {
	const {questionIndex, setQuestionIndex, partIndex, exam} = useExamStore((state) => state);
	
	const calculateScore = () => {
		const total = sentences.length;
		const correct = userSolutions.filter(
			(x) => sentences.find((y) => y.id.toString() === x.question.toString())?.solution === x.option || false,
		).length;
		const missing = total - userSolutions.filter((x) => sentences.find((y) => y.id.toString() === x.question.toString())).length;

		return {total, correct, missing};
	};

	return (
		<>
			<div className="flex flex-col gap-4 mt-4 h-full w-full mb-20">
				<span className="text-sm w-full leading-6">
					{prompt.split("\\n").map((line, index) => (
						<Fragment key={index}>
							{line}
							<br />
						</Fragment>
					))}
				</span>
				<div className="flex flex-col gap-8 w-full items-center justify-between bg-mti-gray-smoke rounded-xl px-24 py-6">
					<div className="flex flex-col gap-4">
						{sentences.map((question) => (
							<QuestionSolutionArea
								question={question}
								userSolution={userSolutions.find((x) => x.question.toString() === question.id.toString())}
								key={`question_${question.id}`}
							/>
						))}
					</div>
				</div>

				<div className="flex gap-4 items-center">
					<div className="flex gap-2 items-center">
						<div className="w-4 h-4 rounded-full bg-mti-purple" /> Correct
					</div>
					<div className="flex gap-2 items-center">
						<div className="w-4 h-4 rounded-full bg-mti-gray-davy" /> Unanswered
					</div>
					<div className="flex gap-2 items-center">
						<div className="w-4 h-4 rounded-full bg-mti-rose" /> Wrong
					</div>
				</div>
			</div>

			<div className="self-end flex justify-between w-full gap-8 absolute bottom-8 left-0 px-8">
				<Button
					color="purple"
					variant="outline"
					onClick={() => onBack({exercise: id, solutions: userSolutions, score: calculateScore(), type})}
					className="max-w-[200px] w-full"
					disabled={
						exam &&
						typeof partIndex !== "undefined" &&
						exam.module === "level" &&
						questionIndex === 0 &&
						partIndex === 0
					}>
					Back
				</Button>

				<Button
					color="purple"
					onClick={() => onNext({exercise: id, solutions: userSolutions, score: calculateScore(), type})}
					className="max-w-[200px] self-end w-full">
					Next
				</Button>
			</div>
		</>
	);
}
