import {SpeakingExercise} from "@/interfaces/exam";
import {CommonProps} from ".";
import {Fragment, useEffect, useState} from "react";
import {BsCheckCircleFill, BsMicFill, BsPauseCircle, BsPlayCircle, BsTrashFill} from "react-icons/bs";
import dynamic from "next/dynamic";
import Button from "../Low/Button";
import useExamStore from "@/stores/examStore";
import {downloadBlob} from "@/utils/evaluation";
import axios from "axios";
import Modal from "../Modal";

const Waveform = dynamic(() => import("../Waveform"), {ssr: false});
const ReactMediaRecorder = dynamic(() => import("react-media-recorder").then((mod) => mod.ReactMediaRecorder), {
	ssr: false,
});

export default function Speaking({id, title, text, video_url, type, prompts, suffix, userSolutions, onNext, onBack}: SpeakingExercise & CommonProps) {
	const [recordingDuration, setRecordingDuration] = useState(0);
	const [isRecording, setIsRecording] = useState(false);
	const [mediaBlob, setMediaBlob] = useState<string>();
	const [audioURL, setAudioURL] = useState<string>();
	const [isLoading, setIsLoading] = useState(false);
	const [isPromptsModalOpen, setIsPromptsModalOpen] = useState(false);
	const [inputText, setInputText] = useState("");

	const hasExamEnded = useExamStore((state) => state.hasExamEnded);

	const saveToStorage = async () => {
		if (mediaBlob && mediaBlob.startsWith("blob")) {
			const blobBuffer = await downloadBlob(mediaBlob);
			const audioFile = new File([blobBuffer], "audio.wav", {type: "audio/wav"});

			const seed = Math.random().toString().replace("0.", "");

			const formData = new FormData();
			formData.append("audio", audioFile, `${seed}.wav`);
			formData.append("root", "speaking_recordings");

			const config = {
				headers: {
					"Content-Type": "audio/wav",
				},
			};

			const response = await axios.post<{path: string}>("/api/storage/insert", formData, config);
			if (audioURL) await axios.post("/api/storage/delete", {path: audioURL});
			return response.data.path;
		}

		return undefined;
	};

	useEffect(() => {
		if (userSolutions.length > 0) {
			const {solution} = userSolutions[0] as {solution?: string};
			if (solution && !mediaBlob) setMediaBlob(solution);
			if (solution && !solution.startsWith("blob")) setAudioURL(solution);
		}
	}, [userSolutions, mediaBlob]);

	useEffect(() => {
		if (hasExamEnded) next();
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [hasExamEnded]);

	useEffect(() => {
		let recordingInterval: NodeJS.Timer | undefined = undefined;
		if (isRecording) {
			recordingInterval = setInterval(() => setRecordingDuration((prev) => prev + 1), 1000);
		} else if (recordingInterval) {
			clearInterval(recordingInterval);
		}

		return () => {
			if (recordingInterval) clearInterval(recordingInterval);
		};
	}, [isRecording]);

	const next = async () => {
		onNext({
			exercise: id,
			solutions: mediaBlob ? [{id, solution: mediaBlob}] : [],
			score: {correct: 0, total: 100, missing: 0},
			type,
		});
	};

	const back = async () => {
		onBack({
			exercise: id,
			solutions: mediaBlob ? [{id, solution: mediaBlob}] : [],
			score: {correct: 0, total: 100, missing: 0},
			type,
		});
	};

	const handleNoteWriting = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
		const newText = e.target.value;
		const words = newText.match(/\S+/g);
		const wordCount = words ? words.length : 0;

		if (wordCount <= 100) {
			setInputText(newText);
		} else {
			let count = 0;
			let lastIndex = 0;
			const matches = newText.matchAll(/\S+/g);
			for (const match of matches) {
				count++;
				if (count > 100) break;
				lastIndex = match.index! + match[0].length;
			}

			setInputText(newText.slice(0, lastIndex));
		}
	};

	return (
		<div className="flex flex-col gap-4 mt-4 w-full">
			<div className="flex justify-between w-full gap-8">
				<Button color="purple" variant="outline" isLoading={isLoading} onClick={back} className="max-w-[200px] self-end w-full">
					Back
				</Button>
				<Button color="purple" isLoading={isLoading} disabled={!mediaBlob} onClick={next} className="max-w-[200px] self-end w-full">
					Next
				</Button>
			</div>

			<div className="flex flex-col h-full w-full gap-9">
				<Modal title="Prompts" className="!w-96 aspect-square" isOpen={isPromptsModalOpen} onClose={() => setIsPromptsModalOpen(false)}>
					<div className="flex flex-col items-center justify-center gap-4 w-full h-full">
						<div className="flex flex-col gap-1 ml-4">
							{prompts.map((x, index) => (
								<li className="italic" key={index}>
									{x}
								</li>
							))}
						</div>
						{!!suffix && <span className="font-bold">{suffix}</span>}
					</div>
				</Modal>
				<div className="flex flex-col w-full gap-2 bg-mti-gray-smoke rounded-xl py-8 px-16">
					<div className="flex flex-col gap-3">
						<div className="flex flex-col gap-0">
							<span className="font-semibold">{title}</span>
							{prompts.length > 0 && (
								<span className="font-semibold">
									You should talk for at least 1 minute and 30 seconds for your answer to be valid.
								</span>
							)}
						</div>
						{!video_url && (
							<span className="font-regular">
								{text.split("\\n").map((line, index) => (
									<Fragment key={index}>
										<span>{line}</span>
										<br />
									</Fragment>
								))}
							</span>
						)}
					</div>
					<div className="flex flex-col gap-6 items-center">
						{video_url && (
							<div className="flex flex-col gap-4 w-full items-center">
								<video key={id} autoPlay controls className="max-w-3xl rounded-xl">
									<source src={video_url} />
								</video>
							</div>
						)}
						{prompts && prompts.length > 0 && <Button onClick={() => setIsPromptsModalOpen(true)}>View Prompts</Button>}
					</div>
				</div>

				{prompts && prompts.length > 0 && (
					<div className="w-full h-full flex flex-col gap-4">
						<textarea
							onContextMenu={(e) => e.preventDefault()}
							className="w-full h-full min-h-[200px] cursor-text px-7 py-8 input border-2 border-mti-gray-platinum bg-white rounded-3xl"
							onChange={handleNoteWriting}
							value={inputText}
							placeholder="Write your notes here..."
							spellCheck={false}
						/>
						<span className="text-base self-end text-mti-gray-cool">Word Count: {(inputText.match(/\S+/g) || []).length}/100</span>
					</div>
				)}

				<ReactMediaRecorder
					audio
					onStop={(blob) => setMediaBlob(blob)}
					render={({status, startRecording, stopRecording, pauseRecording, resumeRecording, clearBlobUrl, mediaBlobUrl}) => (
						<div className="w-full p-4 px-8 bg-transparent border-2 border-mti-gray-platinum rounded-2xl flex-col gap-8 items-center">
							<p className="text-base font-normal">Record your answer:</p>
							<div className="flex gap-8 items-center justify-center py-8">
								{status === "idle" && !mediaBlob && (
									<>
										<div className="w-full h-2 max-w-4xl bg-mti-gray-smoke rounded-full" />
										{status === "idle" && (
											<BsMicFill
												onClick={() => {
													setRecordingDuration(0);
													startRecording();
													setIsRecording(true);
												}}
												className="h-5 w-5 text-mti-gray-cool cursor-pointer"
											/>
										)}
									</>
								)}
								{status === "recording" && (
									<>
										<div className="flex gap-4 items-center">
											<span className="text-xs w-9">
												{Math.floor(recordingDuration / 60)
													.toString(10)
													.padStart(2, "0")}
												:
												{Math.floor(recordingDuration % 60)
													.toString(10)
													.padStart(2, "0")}
											</span>
										</div>
										<div className="w-full h-2 max-w-4xl bg-mti-gray-smoke rounded-full" />
										<div className="flex gap-4 items-center">
											<BsPauseCircle
												onClick={() => {
													setIsRecording(false);
													pauseRecording();
												}}
												className="text-red-500 w-8 h-8 cursor-pointer"
											/>
											<BsCheckCircleFill
												onClick={() => {
													setIsRecording(false);
													stopRecording();
												}}
												className="text-mti-purple-light w-8 h-8 cursor-pointer"
											/>
										</div>
									</>
								)}
								{status === "paused" && (
									<>
										<div className="flex gap-4 items-center">
											<span className="text-xs w-9">
												{Math.floor(recordingDuration / 60)
													.toString(10)
													.padStart(2, "0")}
												:
												{Math.floor(recordingDuration % 60)
													.toString(10)
													.padStart(2, "0")}
											</span>
										</div>
										<div className="w-full h-2 max-w-4xl bg-mti-gray-smoke rounded-full" />
										<div className="flex gap-4 items-center">
											<BsPlayCircle
												onClick={() => {
													setIsRecording(true);
													resumeRecording();
												}}
												className="text-mti-purple-light w-8 h-8 cursor-pointer"
											/>
											<BsCheckCircleFill
												onClick={() => {
													setIsRecording(false);
													stopRecording();
												}}
												className="text-mti-purple-light w-8 h-8 cursor-pointer"
											/>
										</div>
									</>
								)}
								{((status === "stopped" && mediaBlobUrl) || (status === "idle" && mediaBlob)) && (
									<>
										<Waveform audio={mediaBlobUrl ? mediaBlobUrl : mediaBlob!} waveColor="#FCDDEC" progressColor="#EF5DA8" />
										<div className="flex gap-4 items-center">
											<BsTrashFill
												className="text-mti-gray-cool cursor-pointer w-5 h-5"
												onClick={() => {
													setRecordingDuration(0);
													clearBlobUrl();
													setMediaBlob(undefined);
												}}
											/>

											<BsMicFill
												onClick={() => {
													clearBlobUrl();
													setRecordingDuration(0);
													startRecording();
													setIsRecording(true);
													setMediaBlob(undefined);
												}}
												className="h-5 w-5 text-mti-gray-cool cursor-pointer"
											/>
										</div>
									</>
								)}
							</div>
						</div>
					)}
				/>

				<div className="self-end flex justify-between w-full  gap-8">
					<Button color="purple" variant="outline" isLoading={isLoading} onClick={back} className="max-w-[200px] self-end w-full">
						Back
					</Button>
					<Button color="purple" isLoading={isLoading} disabled={!mediaBlob} onClick={next} className="max-w-[200px] self-end w-full">
						Next
					</Button>
				</div>
			</div>
		</div>
	);
}
