import Button from "@/components/Low/Button";
import ProgressBar from "@/components/Low/ProgressBar";
import Modal from "@/components/Modal";
import useUsers from "@/hooks/useUsers";
import { Module } from "@/interfaces";
import { Assignment } from "@/interfaces/results";
import { Stat, User } from "@/interfaces/user";
import useExamStore from "@/stores/exam";
import { getExamById } from "@/utils/exams";
import { sortByModule } from "@/utils/moduleUtils";
import { calculateBandScore } from "@/utils/score";
import { getUserName } from "@/utils/users";
import axios from "axios";
import clsx from "clsx";
import { capitalize, uniqBy } from "lodash";
import moment from "moment";
import { useRouter } from "next/router";
import { BsBook, BsClipboard, BsHeadphones, BsMegaphone, BsPen } from "react-icons/bs";
import { toast } from "react-toastify";
import { futureAssignmentFilter } from "@/utils/assignments";

interface Props {
	isOpen: boolean;
	users: User[];
	assignment?: Assignment;
	onClose: () => void;
}

export default function AssignmentView({ isOpen, users, assignment, onClose }: Props) {
	const router = useRouter();

	const dispatch = useExamStore((s) => s.dispatch);

	const deleteAssignment = async () => {
		if (!confirm("Are you sure you want to delete this assignment?")) return;

		axios
			.delete(`/api/assignments/${assignment?.id}`)
			.then(() => toast.success(`Successfully deleted the assignment "${assignment?.name}".`))
			.catch(() => toast.error("Something went wrong, please try again later."))
			.finally(onClose);
	};

	const startAssignment = () => {
		if (assignment) {
			axios
				.post(`/api/assignments/${assignment.id}/start`)
				.then(() => {
					toast.success(`The assignment "${assignment.name}" has been started successfully!`);
				})
				.catch((e) => {
					console.log(e);
					toast.error("Something went wrong, please try again later!");
				});
		}
	};

	const formatTimestamp = (timestamp: string) => {
		const date = moment(parseInt(timestamp));
		const formatter = "YYYY/MM/DD - HH:mm";

		return date.format(formatter);
	};

	const calculateAverageModuleScore = (module: Module) => {
		if (!assignment) return -1;

		const resultModuleBandScores = assignment.results.map((r) => {
			const moduleStats = r.stats.filter((s) => s.module === module);

			const correct = moduleStats.reduce((acc, curr) => acc + curr.score.correct, 0);
			const total = moduleStats.reduce((acc, curr) => acc + curr.score.total, 0);
			return calculateBandScore(correct, total, module, r.type);
		});

		return resultModuleBandScores.length === 0 ? -1 : resultModuleBandScores.reduce((acc, curr) => acc + curr, 0) / assignment.results.length;
	};

	const aggregateScoresByModule = (stats: Stat[]): { module: Module; total: number; missing: number; correct: number }[] => {
		const scores: {
			[key in Module]: { total: number; missing: number; correct: number };
		} = {
			reading: {
				total: 0,
				correct: 0,
				missing: 0,
			},
			listening: {
				total: 0,
				correct: 0,
				missing: 0,
			},
			writing: {
				total: 0,
				correct: 0,
				missing: 0,
			},
			speaking: {
				total: 0,
				correct: 0,
				missing: 0,
			},
			level: {
				total: 0,
				correct: 0,
				missing: 0,
			},
		};

		stats.filter(x => !x.isPractice).forEach((x) => {
			scores[x.module!] = {
				total: scores[x.module!].total + x.score.total,
				correct: scores[x.module!].correct + x.score.correct,
				missing: scores[x.module!].missing + x.score.missing,
			};
		});

		return Object.keys(scores)
			.filter((x) => scores[x as Module].total > 0)
			.map((x) => ({ module: x as Module, ...scores[x as Module] }));
	};

	const customContent = (stats: Stat[], user: string, focus: "academic" | "general") => {
		const correct = stats.reduce((accumulator, current) => accumulator + current.score.correct, 0);
		const total = stats.reduce((accumulator, current) => accumulator + current.score.total, 0);
		const aggregatedScores = aggregateScoresByModule(stats).filter((x) => x.total > 0);

		const aggregatedLevels = aggregatedScores.map((x) => ({
			module: x.module,
			level: calculateBandScore(x.correct, x.total, x.module, focus),
		}));

		const timeSpent = stats[0].timeSpent;

		const selectExam = () => {
			const examPromises = uniqBy(stats, "exam").map((stat) => getExamById(stat.module, stat.exam));

			Promise.all(examPromises).then((exams) => {
				if (exams.every((x) => !!x)) {
					dispatch({
						type: 'INIT_SOLUTIONS', payload: {
							exams: exams.map((x) => x!).sort(sortByModule),
							modules: exams
								.map((x) => x!)
								.sort(sortByModule)
								.map((x) => x!.module),
							stats
						}
					});
					router.push("/exam");
				}
			});
		};

		const content = (
			<>
				<div className="-md:items-center flex w-full justify-between 2xl:items-center">
					<div className="-md:gap-2 -md:items-center flex md:flex-col md:gap-1 2xl:flex-row 2xl:items-center 2xl:gap-2">
						<span className="font-medium">{formatTimestamp(stats[0].date.toString())}</span>
						{timeSpent && (
							<>
								<span className="md:hidden 2xl:flex">â€¢ </span>
								<span className="text-sm">{Math.floor(timeSpent / 60)} minutes</span>
							</>
						)}
					</div>
					<span
						className={clsx(
							correct / total >= 0.7 && "text-mti-purple",
							correct / total >= 0.3 && correct / total < 0.7 && "text-mti-red",
							correct / total < 0.3 && "text-mti-rose",
						)}>
						Level{" "}
						{(aggregatedLevels.reduce((accumulator, current) => accumulator + current.level, 0) / aggregatedLevels.length).toFixed(1)}
					</span>
				</div>

				<div className="flex w-full flex-col gap-1">
					<div className="-md:mt-2 grid w-full grid-cols-4 place-items-start gap-2">
						{aggregatedLevels.map(({ module, level }) => (
							<div
								key={module}
								className={clsx(
									"-md:px-4 flex w-fit items-center gap-2 rounded-xl py-2 text-white md:px-2 xl:px-4",
									module === "reading" && "bg-ielts-reading",
									module === "listening" && "bg-ielts-listening",
									module === "writing" && "bg-ielts-writing",
									module === "speaking" && "bg-ielts-speaking",
									module === "level" && "bg-ielts-level",
								)}>
								{module === "reading" && <BsBook className="h-4 w-4" />}
								{module === "listening" && <BsHeadphones className="h-4 w-4" />}
								{module === "writing" && <BsPen className="h-4 w-4" />}
								{module === "speaking" && <BsMegaphone className="h-4 w-4" />}
								{module === "level" && <BsClipboard className="h-4 w-4" />}
								<span className="text-sm">{level.toFixed(1)}</span>
							</div>
						))}
					</div>
				</div>
			</>
		);

		return (
			<div className="flex flex-col gap-2">
				<span>
					{(() => {
						const student = users.find((u) => u.id === user);
						return `${student?.name} (${student?.email})`;
					})()}
				</span>
				<div
					key={user}
					className={clsx(
						"border-mti-gray-platinum -md:hidden flex cursor-pointer flex-col gap-4 rounded-xl border p-4 transition duration-300 ease-in-out",
						correct / total >= 0.7 && "hover:border-mti-purple",
						correct / total >= 0.3 && correct / total < 0.7 && "hover:border-mti-red",
						correct / total < 0.3 && "hover:border-mti-rose",
					)}
					onClick={selectExam}
					role="button">
					{content}
				</div>
				<div
					key={user}
					className={clsx(
						"border-mti-gray-platinum -md:tooltip flex cursor-pointer flex-col gap-4 rounded-xl border p-4 transition duration-300 ease-in-out md:hidden",
						correct / total >= 0.7 && "hover:border-mti-purple",
						correct / total >= 0.3 && correct / total < 0.7 && "hover:border-mti-red",
						correct / total < 0.3 && "hover:border-mti-rose",
					)}
					data-tip="Your screen size is too small to view previous exams."
					role="button">
					{content}
				</div>
			</div>
		);
	};

	const shouldRenderStart = () => {
		if (assignment) {
			if (futureAssignmentFilter(assignment)) {
				return true;
			}
		}

		return false;
	};

	return (
		<Modal isOpen={isOpen} onClose={onClose} title={assignment?.name}>
			<div className="mt-4 flex w-full flex-col gap-4">
				<ProgressBar
					color="purple"
					label={`${assignment?.results.length}/${assignment?.assignees.length} assignees completed`}
					className="h-6"
					textClassName={
						(assignment?.results.length || 0) / (assignment?.assignees.length || 1) < 0.5 ? "!text-mti-gray-dim font-light" : "text-white"
					}
					percentage={((assignment?.results.length || 0) / (assignment?.assignees.length || 1)) * 100}
				/>
				<div className="flex items-start gap-8">
					<div className="flex flex-col gap-2">
						<span>Start Date: {moment(assignment?.startDate).format("DD/MM/YY, HH:mm")}</span>
						<span>End Date: {moment(assignment?.endDate).format("DD/MM/YY, HH:mm")}</span>
					</div>
					<div className="flex flex-col gap-2">
						<span>
							Assignees:{" "}
							{users
								.filter((u) => assignment?.assignees.includes(u.id))
								.map((u) => `${u.name} (${u.email})`)
								.join(", ")}
						</span>
						<span>Assigner: {getUserName(users.find((x) => x.id === assignment?.assigner))}</span>
					</div>
				</div>
				<div className="flex flex-col gap-2">
					<span className="text-xl font-bold">Average Scores</span>
					<div className="-md:mt-2 flex w-full items-center gap-4">
						{assignment &&
							uniqBy(assignment.exams, (x) => x.module).map(({ module }) => (
								<div
									data-tip={capitalize(module)}
									key={module}
									className={clsx(
										"-md:px-4 tooltip flex w-fit items-center gap-2 rounded-xl py-2 text-white md:px-2 xl:px-4",
										module === "reading" && "bg-ielts-reading",
										module === "listening" && "bg-ielts-listening",
										module === "writing" && "bg-ielts-writing",
										module === "speaking" && "bg-ielts-speaking",
										module === "level" && "bg-ielts-level",
									)}>
									{module === "reading" && <BsBook className="h-4 w-4" />}
									{module === "listening" && <BsHeadphones className="h-4 w-4" />}
									{module === "writing" && <BsPen className="h-4 w-4" />}
									{module === "speaking" && <BsMegaphone className="h-4 w-4" />}
									{module === "level" && <BsClipboard className="h-4 w-4" />}
									{calculateAverageModuleScore(module) > -1 && (
										<span className="text-sm">{calculateAverageModuleScore(module).toFixed(1)}</span>
									)}
								</div>
							))}
					</div>
				</div>
				<div className="flex flex-col gap-2">
					<span className="text-xl font-bold">
						Results ({assignment?.results.length}/{assignment?.assignees.length})
					</span>
					<div>
						{assignment && assignment?.results.length > 0 && (
							<div className="grid w-full grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3 xl:gap-6">
								{assignment.results.map((r) => customContent(r.stats, r.user, r.type))}
							</div>
						)}
						{assignment && assignment?.results.length === 0 && <span className="ml-1 font-semibold">No results yet...</span>}
					</div>
				</div>

				<div className="flex gap-4 w-full items-center justify-end">
					{assignment && (assignment.results.length === assignment.assignees.length || moment().isAfter(moment(assignment.endDate))) && (
						<Button variant="outline" color="red" className="w-full max-w-[200px]" onClick={deleteAssignment}>
							Delete
						</Button>
					)}
					{/** if the assignment is not deemed as active yet, display start */}
					{shouldRenderStart() && (
						<Button variant="outline" color="green" className="w-full max-w-[200px]" onClick={startAssignment}>
							Start
						</Button>
					)}
					<Button onClick={onClose} className="w-full max-w-[200px]">
						Close
					</Button>
				</div>
			</div>
		</Modal>
	);
}
