"use client"; // components/modules/Kue705FO.jsx
import React, { useState, useEffect } from "react";
import ReactModal from "react-modal";
import Chart from "chart.js/auto";
import { useSelector } from "react-redux";
import KueModal from "../modales/KueModal";
import "bootstrap-icons/font/bootstrap-icons.css"; // Import Bootstrap Icons

function Kue705FO({
  isolationswert,
  schleifenwiderstand,
  modulName,
  kueOnline,
  slotIndex,
  tdrLocation,
  alarmStatus,
}) {
  const [kueVersion, setKueVersion] = useState("V4.19");
  const [currentAlarmStatus, setCurrentAlarmStatus] = useState(false);
  const [currentModulName, setCurrentModulName] = useState(modulName);
  const [activeButton, setActiveButton] = useState("Schleife");
  const [loopTitleText, setloopTitleText] = useState(
    "Schleifenwiderstand [kOhm]"
  );
  const [isoDisplayText, setIsoDisplayText] = useState("Aderbruch");
  const [groundFaultDisplayText, setGroundFaultDisplayText] =
    useState("Erdschluss");
  const [loopFaultDisplayText, setLoopFaultDisplayText] =
    useState("Schleifenfehler");
  const [isoFaultDisplayText, setIsoFaultDisplayText] =
    useState("Isolationsfehler");
  const [isoGreaterThan200, setIsoGreaterThan200] = useState(">200 MOhm");

  const [loading, setLoading] = useState(false);
  const [currentDisplayValue, setCurrentDisplayValue] = useState(
    schleifenwiderstand || "0"
  );
  const [showModal, setShowModal] = useState(false);
  const [showChartModal, setShowChartModal] = useState(false);
  const [chartData, setChartData] = useState(null);

  // Redux-Variablen abrufen
  const {
    kuePSTmMinus96V,
    kueCableBreak,
    kueGroundFault,
    kueAlarm1,
    kueAlarm2,
    kueOverflow,
    kueVersion: reduxKueVersion,
    tdrActive,
  } = useSelector((state) => state.variables);

  const handleOpenModal = () => setShowModal(true);
  const handleCloseModal = () => setShowModal(false);

  const handleOpenChartModal = () => {
    setShowChartModal(true);
    if (activeButton === "TDR") {
      loadTDRChartData();
    } else {
      loadLoopChartData();
    }
  };
  const handleButtonClick = (button) => {
    if (button === "Schleife") {
      setActiveButton("Schleife");
      setloopTitleText("Schleifenwiderstand [kOhm]");
      setCurrentDisplayValue(schleifenwiderstand || "0");
    } else if (button === "TDR") {
      setActiveButton("TDR");
      setloopTitleText("Entfernung [Km]");
      setCurrentDisplayValue(tdrLocation || "0");
    }
  };
  // Bestimme, welche Funktion ausgeführt wird, basierend auf dem aktiven Button
  const handleRefreshClick = () => {
    if (activeButton === "Schleife") {
      goLoop(); // Wenn Schleife aktiv ist, starte goLoop
    } else if (activeButton === "TDR") {
      goTDR(); // Wenn TDR aktiv ist, starte goTDR
    }
  };

  const handleCloseChartModal = () => setShowChartModal(false);

  const loadTDRChartData = () => {
    const slot = slotIndex;
    const environment = process.env.NODE_ENV || "production";
    const fileData =
      environment === "production"
        ? `/CPL?/CPL/lastTDR/slot${slot}.json`
        : `/CPLmockData/lastTDR/slot${slot}.json`;

    fetch(fileData)
      .then((response) => response.json())
      .then((data) => {
        setChartData(data);
        createTDRChart(data);
      })
      .catch((error) =>
        console.error("Fehler beim Laden der TDR-Messkurvendaten:", error)
      );
  };

  const loadLoopChartData = () => {
    const slot = slotIndex;
    const environment = process.env.NODE_ENV || "production";
    const fileData =
      environment === "production"
        ? `/CPL?/CPL/4000values/slot${slot}.json`
        : `/CPLmockData/4000values/slot${slot}.json`;

    fetch(fileData)
      .then((response) => response.json())
      .then((data) => {
        setChartData(data);
        createChart(data, "Schleifenmesskurve");
      })
      .catch((error) =>
        console.error("Fehler beim Laden der Schleifenmesskurvendaten:", error)
      );
  };

  const createChart = (data) => {
    const ctx = document.getElementById("myChart").getContext("2d");
    new Chart(ctx, {
      type: "line",
      data: {
        labels: data
          .map((row) => new Date(row.t).toLocaleString("de-DE"))
          .reverse(),
        datasets: [
          {
            label: "Isolationswiderstand (MOhm)",
            data: data.map((row) => row.m).reverse(),
            borderColor: "#00AEEF",
            fill: false,
            yAxisID: "y",
          },
          {
            label: "Schleifenwiderstand (kOhm)",
            data: data.map((row) => row.n).reverse(),
            borderColor: "black",
            fill: false,
            yAxisID: "y1",
          },
        ],
      },
      options: {
        scales: {
          y: {
            type: "linear",
            position: "left",
            title: { display: true, text: "MOhm" },
          },
          y1: {
            type: "linear",
            position: "right",
            title: { display: true, text: "kOhm" },
          },
        },
      },
    });
  };

  useEffect(() => {
    const updateAlarmStatus = () => {
      const alarmStatus =
        (kueAlarm1 && kueAlarm1[slotIndex]) ||
        (kueAlarm2 && kueAlarm2[slotIndex]) ||
        (kueCableBreak && kueCableBreak[slotIndex]) ||
        (kueGroundFault && kueGroundFault[slotIndex]);

      setCurrentAlarmStatus(alarmStatus);
    };

    updateAlarmStatus();
    const interval = setInterval(updateAlarmStatus, 10000);
    return () => clearInterval(interval);
  }, [slotIndex, kueAlarm1, kueAlarm2, kueCableBreak, kueGroundFault]);

  useEffect(() => {
    const updateDisplay = () => {
      if (kuePSTmMinus96V?.[slotIndex] === 1) {
        setCurrentDisplayValue("PST-M prüfen");
        return;
      }
      if (kueCableBreak?.[slotIndex] === 1) {
        setCurrentDisplayValue(isoDisplayText);
        return;
      }
      if (kueGroundFault?.[slotIndex] === 1) {
        setCurrentDisplayValue(groundFaultDisplayText);
        return;
      }
      if (kueAlarm1?.[slotIndex] === 1) {
        setCurrentDisplayValue(isoFaultDisplayText);
        return;
      }
      if (kueAlarm2?.[slotIndex] === 1) {
        setCurrentDisplayValue(loopFaultDisplayText);
        return;
      }
      if (kueOverflow?.[slotIndex] === 1) {
        setCurrentDisplayValue(isoGreaterThan200);
        return;
      }
      setCurrentDisplayValue(isolationswert);
    };

    updateDisplay();
    const interval = setInterval(updateDisplay, 5000);
    return () => clearInterval(interval);
  }, [
    slotIndex,
    isolationswert,
    kuePSTmMinus96V,
    kueCableBreak,
    kueGroundFault,
    kueAlarm1,
    kueAlarm2,
    kueOverflow,
  ]);

  useEffect(() => {
    if (reduxKueVersion?.[slotIndex]) {
      setKueVersion("V" + reduxKueVersion[slotIndex] / 100);
    }
  }, [slotIndex, reduxKueVersion]);

  return (
    <div className="relative bg-gray-300 w-[116px] h-[390px] border border-gray-400 scale-110 top-3">
      {kueOnline === 1 ? (
        <>
          <div className="relative w-[113.202px] h-[242.492px] bg-littwin-blue border-[1.5px] border-gray-400 z-0">
            <div className="flex items-start justify-between h-[30px]">
              <div className="relative w-[20px] h-[20px] bg-gray-800 flex items-center justify-center">
                <span className="text-white text-[10px]">{slotIndex + 1}</span>
              </div>

              <h3 className="text-white font-bold text-[9px] mr-4">KÜ705-FO</h3>

              <button
                onClick={handleOpenModal}
                className="w-[15px] h-[15px] bg-gray-400 flex items-center justify-center"
              >
                <span className="text-white text-[20px]">⚙</span>
              </button>
            </div>

            <KueModal
              showModal={showModal}
              onClose={handleCloseModal}
              slot={slotIndex}
              onModulNameChange={setCurrentModulName}
            />
            <div className="flex flex-col mt-[10px] ml-[10px]">
              <div className="flex items-center">
                <div className="w-[10px] h-[10px] bg-green-500 rounded-full mr-2"></div>
                <span className="text-white text-[10px]">Betrieb</span>
              </div>
              <div className="flex items-center mt-1">
                <div
                  className={`w-[10px] h-[10px] rounded-full mr-2 ${
                    currentAlarmStatus ? "bg-red-500" : "bg-gray-300"
                  }`}
                ></div>
                <span className="text-white text-[10px]">Alarm</span>
              </div>
            </div>

            <div className="relative mt-[50px] mx-auto bg-black text-white w-[100px] h-[40px] flex items-center justify-center text-[18px] z-10">
              <div className="text-center">
                <span
                  className={
                    kuePSTmMinus96V?.[slotIndex] === 1 ||
                    kueCableBreak?.[slotIndex] === 1 ||
                    kueGroundFault?.[slotIndex] === 1 ||
                    kueAlarm1?.[slotIndex] === 1 ||
                    kueAlarm2?.[slotIndex] === 1
                      ? "text-red-500 text-[14px]"
                      : kueOverflow?.[slotIndex] === 1
                      ? "text-white text-[14px]"
                      : ""
                  }
                >
                  {currentDisplayValue}
                </span>

                {kuePSTmMinus96V?.[slotIndex] !== 1 &&
                  kueCableBreak?.[slotIndex] !== 1 &&
                  kueGroundFault?.[slotIndex] !== 1 &&
                  kueAlarm1?.[slotIndex] !== 1 &&
                  kueAlarm2?.[slotIndex] !== 1 &&
                  kueOverflow?.[slotIndex] !== 1 && (
                    <div className="text-[8px]">ISO MOhm</div>
                  )}
              </div>
            </div>

            <div className="absolute top-0 left-[75px] w-[3px] h-full bg-white z-0"></div>
            <div className="absolute top-[40px] left-[75px] w-[40px] h-[3px] bg-white z-0"></div>

            <div className="absolute bottom-[20px] left-0 right-0 text-black text-[10px] bg-gray-300 p-1 text-center">
              {currentModulName || "Test1"}
            </div>

            <div className="absolute bottom-1 right-1 text-black text-[8px]">
              {kueVersion}
            </div>
          </div>

          {/* loopDisplay: Zeigt Schleifenwiderstand oder TDR-Distanz an, je nach Modus */}
          <div className="absolute bottom-1 left-[1.095px] w-[113.182px] h-[130px] bg-gray-300 border-[1.5px] border-gray-400 p-1">
            <span className="text-black text-[7px] absolute top-[2px] left-1 mt-1">
              {loopTitleText}
            </span>

            <div className="relative w-full h-[45px] bg-gray-100 border border-gray-400 flex items-center justify-center mt-3">
              <button
                onClick={handleRefreshClick} // Dynamische Funktion basierend auf aktivem Button
                className="absolute -top-1 -right-1 w-[20px] h-[20px] bg-gray-400 flex items-center justify-center"
                disabled={loading} // Disable button while loading
              >
                <span className="text-white text-[18px]">⟳</span>
              </button>

              <div className="absolute bottom-[5px] left-1/2 transform -translate-x-1/2 w-[100px] flex justify-center items-center">
                <div className="text-center text-black text-[10px]">
                  <p>{schleifenwiderstand + " KOhm"}</p>
                </div>
              </div>
            </div>

            <div className="flex mt-2 space-x-1">
              <button
                onClick={() => handleButtonClick("Schleife")}
                className={`w-[50%] h-[25px] text-white text-[10px] flex items-center justify-center ${
                  activeButton === "Schleife"
                    ? "bg-littwin-blue"
                    : "bg-gray-400"
                }`}
              >
                Schleife
              </button>
              <button
                onClick={() => handleButtonClick("TDR")}
                className={`w-[50%] h-[25px] text-white text-[10px] flex items-center justify-center ${
                  tdrActive[slotIndex] === 0
                    ? "bg-gray-200 cursor-not-allowed" // Deaktiviert: Hellgrau
                    : activeButton === "TDR"
                    ? "bg-littwin-blue" // Aktiviert: Littwin Blau
                    : "bg-gray-400" // Nicht geklickt: Dunkelgrau
                }`}
                disabled={tdrActive[slotIndex] === 0} // Button deaktiviert, wenn TDR für diesen Slot nicht aktiv ist
              >
                TDR
              </button>
            </div>

            <button
              onClick={handleOpenChartModal} // Öffnet das Chart-Modal
              className="w-full h-[25px] bg-littwin-blue text-white text-[10px] flex items-center justify-center mt-1"
            >
              Messkurve
            </button>
          </div>

          {/* Modal für Messkurve */}
          {showChartModal && (
            <ReactModal
              isOpen={showChartModal}
              onRequestClose={handleCloseChartModal}
              ariaHideApp={false}
              style={{
                overlay: { backgroundColor: "rgba(0, 0, 0, 0.5)" },
                content: {
                  top: "50%",
                  left: "50%",
                  right: "auto",
                  bottom: "auto",
                  marginRight: "-50%",
                  transform: "translate(-50%, -50%)",
                  width: "95%",
                  maxWidth: "1200px",
                  height: "650px",
                  padding: "10px",
                },
              }}
            >
              <button
                onClick={handleCloseChartModal}
                style={{
                  position: "absolute",
                  top: "10px",
                  right: "10px",
                  background: "transparent",
                  border: "none",
                  fontSize: "24px",
                  cursor: "pointer",
                }}
              >
                <i className="bi bi-x-circle-fill"></i>{" "}
                {/* Bootstrap Icon "X" */}
              </button>
              <h2>Messkurve Slot {slotIndex + 1}</h2>
              <canvas
                id="myChart"
                style={{ width: "100%", height: "600px" }}
              ></canvas>
            </ReactModal>
          )}
        </>
      ) : (
        <div className="flex items-center justify-center h-full text-gray-500">
          <p>Kein Modul im Slot {slotIndex + 1}</p>
        </div>
      )}
    </div>
  );
}

export default Kue705FO;
