"use client"; // components/Header.jsx
import React, { useEffect, useState } from "react";
import Image from "next/image";
import "bootstrap-icons/font/bootstrap-icons.css";
import { loadWindowVariables } from "../utils/loadWindowVariables";
import SettingsModal from "./modales/SettingsModal";

function Header() {
  const [isClient, setIsClient] = useState(false);
  const [stationsname, setStationsname] = useState("Lädt...");
  const [cplStatus, setCplStatus] = useState("Lädt...");
  const [showSettingsModal, setShowSettingsModal] = useState(false);
  const [logOnce, setLogOnce] = useState(false);

  useEffect(() => {
    setIsClient(true);
  }, []);

  const handleSettingsClick = () => {
    setShowSettingsModal(true);
  };

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

  const handleLogout = () => {
    window.location.href = "/offline.html";
  };

  useEffect(() => {
    if (isClient && !logOnce) {
      const environment = process.env.NEXT_PUBLIC_NODE_ENV || "production";
      const script = document.createElement("script");

      // Laden von nur System.js abhängig von der Umgebung
      script.src =
        environment === "production"
          ? `/CPL?/CPL/SERVICE/System.js`
          : `/mockData/SERVICE/System.js`;

      script.async = true;
      script.onload = () => {
        console.log(`System.js wurde erfolgreich geladen.`);

        // Systemvariablen überprüfen und zuweisen
        if (window.deviceName) {
          setStationsname(window.deviceName || "Unbekannt");
          setCplStatus(window.hardware_version || "Unbekannt");
        } else {
          console.error("Systemvariablen sind nicht vollständig verfügbar.");
        }

        console.log(" Header Systemvariablen geladen:", {
          deviceName: window.deviceName,
          ip: window.ip,
          mac1: window.mac1,
          mac2: window.mac2,
          subnet: window.subnet,
          gateway: window.gateway,
          datetime: window.datetime,
        });

        setLogOnce(true);
      };

      script.onerror = (error) =>
        console.error("Fehler beim Laden von System.js:", error);

      document.head.appendChild(script);
    }
  }, [isClient, logOnce]);

  if (!isClient) return null;

  return (
    <header className="bg-gray-300 flex justify-between items-center w-full h-28 relative text-black">
      <div className="absolute left-32 top-32 transform -translate-y-1/2">
        <Image
          src="/images/Logo.png"
          alt="Logo"
          width={80}
          height={80}
          style={{ width: "auto", height: "auto" }}
          priority={false}
        />
      </div>
      <div className="w-64 flex-shrink-0 h-full mt-24 "></div>

      <div className="flex items-start space-x-4 w-full">
        <div className="flex flex-col text-left pl-8">
          <h2 className="text-base">Stationsname</h2>
          <p className="text-base text-gray-600">{stationsname}</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">
          <button
            onClick={handleSettingsClick}
            className="text-3xl text-black mr-7"
          >
            <i className="bi bi-gear"></i>
          </button>
        </div>
        <div className="flex items-center justify-end w-full">
          <button
            onClick={handleLogout}
            className="text bg-littwin-blue text-white px-4 py-2 rounded"
          >
            Abmelden
          </button>
        </div>
      </div>
      {showSettingsModal && (
        <SettingsModal
          showModal={showSettingsModal}
          onClose={handleCloseSettingsModal}
        />
      )}
    </header>
  );
}

export default Header;
