"use client"; // components/Header.jsx
import React, { useState, useEffect } from "react";
import Image from "next/image";
import { useRouter } from "next/router";
import "bootstrap-icons/font/bootstrap-icons.css";
import SettingsModal from "@/components/header/settingsModal/SettingsModal";
import { RootState } from "@/redux/store";
import { useSelector, useDispatch } from "react-redux";
import { AppDispatch } from "@/redux/store";

import { getSystemSettingsThunk } from "@/redux/thunks/getSystemSettingsThunk";

function Header() {
  const router = useRouter();
  const [showSettingsModal, setShowSettingsModal] = useState(false);
  const [isAdminLoggedIn, setIsAdminLoggedIn] = useState(false);
  // Removed duplicate declaration of deviceName

  const handleCloseSettingsModal = () => setShowSettingsModal(false);

  const handleLogout = () => {
    sessionStorage.removeItem("token"); // Token entfernen
    localStorage.setItem("isAdminLoggedIn", "false"); // Admin-Status entfernen
    setIsAdminLoggedIn(false); // Zustand sofort aktualisieren
    router.push("/offline.html"); // Weiterleitung
  };

  useEffect(() => {
    // Initialer Check beim Laden der Komponente
    const isAdmin = localStorage.getItem("isAdminLoggedIn") === "true";
    setIsAdminLoggedIn(isAdmin);

    // Beobachten von Änderungen in localStorage
    const interval = setInterval(() => {
      const updatedIsAdmin = localStorage.getItem("isAdminLoggedIn") === "true";
      if (updatedIsAdmin !== isAdminLoggedIn) {
        setIsAdminLoggedIn(updatedIsAdmin);
      }
    }, 500); // Überprüfung alle 500ms

    return () => {
      clearInterval(interval); // Intervall stoppen, wenn die Komponente entladen wird
    };
  }, [isAdminLoggedIn]);
  //----------------------------------------------------------------
  const dispatch = useDispatch<AppDispatch>();

  const deviceName = useSelector(
    (state: RootState) => state.systemSettingsSlice.deviceName
  );

  useEffect(() => {
    if (!deviceName) {
      dispatch(getSystemSettingsThunk());
    }
  }, [deviceName, dispatch]);
  //----------------------------------------------------------------

  return (
    <header className="bg-gray-300 flex justify-between items-center w-full h-[13vh] laptop:h-[10vh] relative text-black ">
      <div
        className="absolute transform -translate-y-1/2 
                left-[8%] sm:left-[8%] md:left-[8%] lg:left-[8%] xl:left-[6%] 2xl:left-[2%] laptop:left-[4%] laptop:
                top-[90%] sm:top-[90%] md:top-[90%] lg:top-[90%] xl:top-[90%]"
        style={{
          height: "10vh", // Dynamische Höhe des Containers
          width: "auto",
          aspectRatio: "1", // Beibehaltung des Seitenverhältnisses
        }}
      >
        <Image
          src="/images/Logo.png"
          alt="Logo"
          fill
          sizes="(max-width: 640px) 7vh, (max-width: 1024px) 8vh, (max-width: 1280px) 9vh, 10vh"
          className="object-contain"
          priority={false}
        />
      </div>
      {/* TALAS-Logo + Text nebeneinander (flexibel oben links) */}
      <div className="flex items-center space-x-8 transform translate-x-3/4 mt-8 laptop:ml-10 laptop:translate-y-1 laptop:mt-4">
        <Image
          src="/images/logo-talas-2024.png"
          alt="TALAS Logo"
          className="object-contain w-[120px] laptop:w-[80px] xl:w-[140px] 2xl:w-[180px]"
          width={160}
          height={60}
          priority
        />
        <div className="flex flex-col leading-tight whitespace-nowrap">
          <h2 className="text-xl laptop:text-base xl:text-lg font-bold">
            Meldestation
          </h2>
          <p className="text-gray-600 text-lg laptop:text-sm xl:text-base truncate max-w-[20vw]">
            {deviceName}
          </p>
        </div>
      </div>

      <div className="p-4 w-full lg:w-full flex flex-row gap-4 justify-between">
        <div className="flex items-center justify-end w-full">
          {/* Admin-Login */}
          {/* 
          <button
            onClick={handleSettingsClick}
            className="text-3xl text-black mr-0"
          >
            <i className="bi bi-gear"></i>
          </button>
          */}
        </div>

        {/* Logout-Button */}

        <div className="flex items-center justify-end w-1/4 space-x-1">
          <button
            onClick={handleLogout}
            className="bg-littwin-blue text-white px-4 py-2 rounded"
          >
            Abmelden
          </button>
        </div>
      </div>

      {/* Warnhinweis, wenn der Admin angemeldet ist */}
      {isAdminLoggedIn && (
        <div className="absolute top-0 left-1/2 transform -translate-x-1/2 w-full xl:w-1/4 2xl:w-1/4 h-8 bg-yellow-400 text-center py-2 text-black font-bold">
          Admin-Modus aktiv
        </div>
      )}

      {showSettingsModal && (
        <SettingsModal
          showModal={showSettingsModal}
          onClose={handleCloseSettingsModal}
        />
      )}
    </header>
  );
}

export default Header;
