"use client";
import { useEffect } from "react";
import { CheckCircledIcon, EnterIcon, DrawingPinFilledIcon } from "@radix-ui/react-icons";
import { Event, Participant, User } from "@repo/db";
import { cn } from "@repo/shared-components";
import { inscribeToMoodleCourse, upsertParticipant } from "../actions";
import {
	BookCheck,
	Check,
	CirclePlay,
	ExternalLink,
	EyeIcon,
	Info,
	TriangleAlert,
} from "lucide-react";
import { eventCompleted } from "@repo/shared-components";
import MDEditor from "@uiw/react-md-editor";

interface ModalBtnProps {
	title: string;
	event: Event;
	participant?: Participant;
	user: User;
	modalId: string;
}

const ModalBtn = ({ title, modalId, participant, event, user }: ModalBtnProps) => {
	useEffect(() => {
		const modal = document.getElementById(modalId) as HTMLDialogElement;
		const handleOpen = () => {
			document.body.classList.add("modal-open");
		};
		const handleClose = () => {
			document.body.classList.remove("modal-open");
		};
		modal?.addEventListener("show", handleOpen);
		modal?.addEventListener("close", handleClose);
		return () => {
			modal?.removeEventListener("show", handleOpen);
			modal?.removeEventListener("close", handleClose);
		};
	}, [modalId]);

	const openModal = () => {
		const modal = document.getElementById(modalId) as HTMLDialogElement;
		modal?.showModal();
	};

	const closeModal = () => {
		const modal = document.getElementById(modalId) as HTMLDialogElement;
		modal?.close();
	};

	const missingRequirements =
		event.requiredBadges?.length > 0 &&
		!event.requiredBadges.some((badge) => user.badges.includes(badge));

	return (
		<>
			<button
				className={cn(
					"btn btn-outline btn-info btn-wide",
					event.type === "COURSE" && "btn-secondary",
					eventCompleted(event, participant) && "btn-success",
				)}
				onClick={openModal}
				disabled={eventCompleted(event, participant) || missingRequirements}
			>
				{missingRequirements && (
					<>
						<TriangleAlert className="h-6 w-6 shrink-0 stroke-current" fill="none" />
						fehlende Anforderungen
					</>
				)}
				{participant && !eventCompleted(event, participant) && !missingRequirements && (
					<>
						<EyeIcon /> Anzeigen
					</>
				)}
				{!participant && !missingRequirements && (
					<>
						<EnterIcon /> Anmelden
					</>
				)}
				{eventCompleted(event, participant) && (
					<>
						<Check /> Abgeschlossen
					</>
				)}
			</button>
			<dialog id={modalId} className="modal">
				<div className="modal-box w-11/12 max-w-5xl overflow-hidden">
					<form method="dialog">
						<button className="btn btn-sm btn-circle btn-ghost absolute right-3 top-3">✕</button>
					</form>
					<h3 className="text-left text-lg font-bold">{title}</h3>
					<div className="mt-4 flex flex-wrap gap-4">
						<div className="bg-base-300 flex min-w-[300px] flex-1 flex-col gap-2 rounded-lg p-3 shadow">
							<h2 className="flex gap-2 text-lg font-bold">
								<Info /> Details
							</h2>
							<div className="text-balance text-left" data-color-mode="dark">
								<MDEditor.Markdown
									source={event.description}
									style={{
										backgroundColor: "transparent",
									}}
								/>
							</div>
						</div>
						{event.finisherMoodleCourseId && (
							<div className="bg-base-300 flex min-w-[300px] flex-1 flex-col gap-2 rounded-lg p-3 shadow">
								<h2 className="flex gap-2 text-lg font-bold">
									<BookCheck /> Moodle-Kurs
								</h2>
								<div className="flex h-full flex-col items-center justify-center">
									<MoodleCourseIndicator
										participant={participant}
										user={user}
										moodleCourseId={event.finisherMoodleCourseId}
										completed={participant?.finisherMoodleCurseCompleted || false}
										event={event}
									/>
								</div>
							</div>
						)}
					</div>

					<div className="mt-5 flex items-end justify-between">
						<div>
							<p className="flex items-center gap-2 text-left text-gray-600">
								<DrawingPinFilledIcon /> <b>Teilnahmevoraussetzungen: </b>
								{!event.requiredBadges.length && "Keine"}
							</p>
						</div>
					</div>
				</div>
				<button className="modal-backdrop" onClick={closeModal}>
					Abbrechen
				</button>
			</dialog>
		</>
	);
};

export default ModalBtn;

const MoodleCourseIndicator = ({
	completed,
	moodleCourseId,
	event,
	user,
}: {
	user: User;
	participant?: Participant;
	completed?: boolean;
	moodleCourseId: string;
	event: Event;
}) => {
	const courseUrl = `${process.env.NEXT_PUBLIC_MOODLE_URL}/course/view.php?id=${moodleCourseId}`;
	if (completed)
		return (
			<p className="flex items-center justify-center gap-2 py-4">
				<CheckCircledIcon className="text-success" />
				Moodle Kurs abgeschlossen
			</p>
		);
	return (
		<div className="flex flex-col items-center justify-center gap-2">
			<p className="flex items-center gap-2 text-sm">
				<CirclePlay className="text-warning" /> Moodle-Kurs bereit
			</p>
			<p className="font-bold text-gray-400">
				Wenn du nach dem Anmelden den moodle Kurs nicht siehst, warte ein paar Sekunden und lade die
				Seite neu.
			</p>
			<button
				className="btn btn-sm btn-outline btn-info ml-2"
				onClick={async () => {
					try {
						await upsertParticipant({
							eventId: event.id,
							userId: user.id,
							finisherMoodleCurseCompleted: false,
						});

						if (user.moodleId) {
							await inscribeToMoodleCourse(moodleCourseId, user.moodleId);
						}
						window.open(courseUrl, "_blank");
					} catch (error) {
						console.log("Fehler beim öffnen des Moodle-Kurses", error);
					}
				}}
			>
				<ExternalLink size={16} />
				Zum Moodle Kurs
			</button>
		</div>
	);
};
