import {User} from "@/interfaces/user";
import {Dialog, Transition} from "@headlessui/react";
import axios from "axios";
import clsx from "clsx";
import Image from "next/image";
import Link from "next/link";
import {useRouter} from "next/router";
import {Fragment} from "react";
import {BsShield, BsShieldFill, BsXLg} from "react-icons/bs";

interface Props {
	isOpen: boolean;
	onClose: () => void;
	path: string;
	user: User;
}

export default function MobileMenu({isOpen, onClose, path, user}: Props) {
	const router = useRouter();

	const logout = async () => {
		axios.post("/api/logout").finally(() => {
			setTimeout(() => router.reload(), 500);
		});
	};

	return (
		<Transition appear show={isOpen} as={Fragment}>
			<Dialog as="div" className="relative z-10" onClose={onClose}>
				<Transition.Child
					as={Fragment}
					enter="ease-out duration-300"
					enterFrom="opacity-0"
					enterTo="opacity-100"
					leave="ease-in duration-200"
					leaveFrom="opacity-100"
					leaveTo="opacity-0">
					<div className="fixed inset-0 bg-black bg-opacity-25" />
				</Transition.Child>

				<div className="fixed inset-0 overflow-y-auto">
					<div className="flex min-h-full items-center justify-center text-center">
						<Transition.Child
							as={Fragment}
							enter="ease-out duration-300"
							enterFrom="opacity-0 scale-95"
							enterTo="opacity-100 scale-100"
							leave="ease-in duration-200"
							leaveFrom="opacity-100 scale-100"
							leaveTo="opacity-0 scale-95">
							<Dialog.Panel className="w-full h-screen transform overflow-hidden bg-white text-left align-middle shadow-xl transition-all text-black flex flex-col gap-8">
								<Dialog.Title as="header" className="w-full px-8 py-2 -md:flex justify-between items-center md:hidden shadow-sm">
									<Link href="/">
										<Image src="/logo_title.png" alt="EnCoach logo" width={69} height={69} />
									</Link>
									<div className="cursor-pointer" onClick={onClose} tabIndex={0}>
										<BsXLg className="text-2xl text-mti-purple-light" onClick={onClose} />
									</div>
								</Dialog.Title>
								<div className="flex flex-col h-full gap-6 px-8 text-lg">
									<Link
										href="/"
										className={clsx(
											"transition ease-in-out duration-300 w-fit",
											path === "/" && "text-mti-purple-light font-semibold border-b-2 border-b-mti-purple-light ",
										)}>
										Dashboard
									</Link>
									{(user.type === "student" || user.type === "teacher" || user.type === "developer") && (
										<>
											<Link
												href="/exam"
												className={clsx(
													"transition ease-in-out duration-300 w-fit",
													path === "/exam" && "text-mti-purple-light font-semibold border-b-2 border-b-mti-purple-light ",
												)}>
												Exams
											</Link>
											<Link
												href="/exercises"
												className={clsx(
													"transition ease-in-out duration-300 w-fit",
													path === "/exercises" &&
														"text-mti-purple-light font-semibold border-b-2 border-b-mti-purple-light ",
												)}>
												Exercises
											</Link>
										</>
									)}
									<Link
										href="/stats"
										className={clsx(
											"transition ease-in-out duration-300 w-fit",
											path === "/stats" && "text-mti-purple-light font-semibold border-b-2 border-b-mti-purple-light ",
										)}>
										Stats
									</Link>
									<Link
										href="/record"
										className={clsx(
											"transition ease-in-out duration-300 w-fit",
											path === "/record" && "text-mti-purple-light font-semibold border-b-2 border-b-mti-purple-light ",
										)}>
										Record
									</Link>
									{["admin", "developer", "agent", "corporate"].includes(user.type) && (
										<Link
											href="/payment-record"
											className={clsx(
												"transition ease-in-out duration-300 w-fit",
												path === "/payment-record" &&
													"text-mti-purple-light font-semibold border-b-2 border-b-mti-purple-light ",
											)}>
											Payment Record
										</Link>
									)}
									{["admin", "developer", "corporate", "teacher"].includes(user.type) && (
										<Link
											href="/settings"
											className={clsx(
												"transition ease-in-out duration-300 w-fit",
												path === "/settings" && "text-mti-purple-light font-semibold border-b-2 border-b-mti-purple-light ",
											)}>
											Settings
										</Link>
									)}
									<Link
										href="/profile"
										className={clsx(
											"transition ease-in-out duration-300 w-fit",
											path === "/profile" && "text-mti-purple-light font-semibold border-b-2 border-b-mti-purple-light ",
										)}>
										Profile
									</Link>

									<span
										className={clsx("transition ease-in-out duration-300 w-fit justify-self-end cursor-pointer")}
										onClick={logout}>
										Logout
									</span>
								</div>
							</Dialog.Panel>
						</Transition.Child>
					</div>
				</div>
			</Dialog>
		</Transition>
	);
}
