"use client";

import React from "react";
import { useRouter } from "next/navigation";
import { ChevronLeft, Layers } from "lucide-react";
import { useTimer } from "@/context/TimerContext";
import styles from "@/css/Header.module.css";
import { useExam } from "@/context/ExamContext";
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { useModal } from "@/context/ModalContext";
import { useAuth } from "@/context/AuthContext";

interface HeaderProps {
  displayUser?: boolean;
  displaySubject?: string;
  displayTabTitle?: string;
}

const Header = ({
  displayUser,
  displaySubject,
  displayTabTitle,
}: HeaderProps) => {
  const router = useRouter();
  const { open } = useModal();
  const { cancelExam } = useExam();
  const { stopTimer, timeRemaining } = useTimer();
  const { user } = useAuth();

  const showExitDialog = () => {
    const confirmed = window.confirm("Are you sure you want to quit the exam?");
    if (confirmed) {
      stopTimer();
      cancelExam();
      router.push("/categories");
    }
  };

  const handleBackClick = () => {
    router.back();
  };

  // format time from context
  const hours = Math.floor(timeRemaining / 3600);
  const minutes = Math.floor((timeRemaining % 3600) / 60);
  const seconds = timeRemaining % 60;

  return (
    <header className={styles.header}>
      {displayUser && (
        <div className={styles.profile}>
          <Avatar className="bg-gray-200 w-10 h-10">
            <AvatarFallback className=" text-lg">
              {user?.username ? user.username.charAt(0).toUpperCase() : ""}
            </AvatarFallback>
          </Avatar>
          <span className={styles.text}>
            Hello, {user?.username ? user.username.split(" ")[0] : ""}
          </span>
        </div>
      )}

      {displayTabTitle && (
        <div className={styles.profile}>
          <button onClick={handleBackClick} className={styles.iconButton}>
            <ChevronLeft size={24} color="white" />
          </button>
          <span className={styles.text}>{displayTabTitle}</span>
        </div>
      )}

      {displaySubject && (
        <div className={styles.profile}>
          <span className={styles.text}>{displaySubject}</span>
        </div>
      )}

      {/* Exam timer header */}
      {timeRemaining > 0 && (
        <div className={styles.examHeader}>
          <button onClick={showExitDialog} className={styles.iconButton}>
            <ChevronLeft size={30} color="white" />
          </button>

          <div className={styles.timer}>
            <div className={styles.timeUnit}>
              <span className={styles.timeValue}>
                {String(hours).padStart(2, "0")}
              </span>
              <span className={styles.timeLabel}>Hrs</span>
            </div>
            <div className={styles.timeUnit}>
              <span className={styles.timeValue}>
                {String(minutes).padStart(2, "0")}
              </span>
              <span className={styles.timeLabel}>Mins</span>
            </div>
            <div className={styles.timeUnit} style={{ borderRight: "none" }}>
              <span className={styles.timeValue}>
                {String(seconds).padStart(2, "0")}
              </span>
              <span className={styles.timeLabel}>Secs</span>
            </div>
          </div>

          <button onClick={open} className={`${styles.iconButton}`}>
            <Layers size={30} color="white" />
          </button>
        </div>
      )}
    </header>
  );
};

export default Header;
