"use client"; // KVZChartView.tsx

import React, { useEffect } from "react";
import ReactModal from "react-modal";
import { useDispatch, useSelector } from "react-redux";
import { AppDispatch, RootState } from "@/redux/store";
import {
  setChartOpen,
  setFullScreen,
  setSlotNumber,
  setVonDatum,
  setBisDatum,
  setSelectedMode,
  setSelectedSlotType,
} from "@/redux/slices/kabelueberwachungChartSlice";
import { resetBrushRange } from "@/redux/slices/brushSlice";
import FallSensors from "../../../../fall-detection-sensors/FallSensors";
import Report from "../IsoMeasurementChart/Report";

interface KVZChartViewProps {
  isOpen: boolean;
  onClose: () => void;
  slotIndex: number;
}

// Modal zur Anzeige der KVz Zustände (Sturzsensoren / Fall Detection LEDs)
// Stil und Verhalten analog zu ISO / RSL / TDR Modals
const KVZChartView: React.FC<KVZChartViewProps> = ({
  isOpen,
  onClose,
  slotIndex,
}) => {
  const dispatch = useDispatch<AppDispatch>();
  const isFullScreen = useSelector(
    (state: RootState) => state.kabelueberwachungChartSlice.isFullScreen
  );
  const slotNumber = useSelector(
    (state: RootState) => state.kabelueberwachungChartSlice.slotNumber
  );

  // Beim Öffnen Slot setzen (damit konsistent zu anderen Modals)
  useEffect(() => {
    if (isOpen) {
      dispatch(setSlotNumber(slotIndex));
    }
  }, [isOpen, slotIndex, dispatch]);

  const handleClose = () => {
    const today = new Date();
    const thirtyDaysAgo = new Date();
    thirtyDaysAgo.setDate(today.getDate() - 30);
    const toISO = (d: Date) => d.toLocaleDateString("sv-SE");

    // Zurücksetzen – entspricht Verhalten der anderen Modals
    dispatch(setVonDatum(toISO(thirtyDaysAgo)));
    dispatch(setBisDatum(toISO(today)));
    dispatch(setSelectedMode("DIA1"));
    dispatch(setSelectedSlotType("isolationswiderstand"));
    dispatch(setChartOpen(false));
    dispatch(setFullScreen(false));
    dispatch(resetBrushRange());

    onClose();
  };

  const toggleFullScreen = () => {
    dispatch(setFullScreen(!isFullScreen));
  };

  return (
    <ReactModal
      isOpen={isOpen}
      onRequestClose={handleClose}
      ariaHideApp={false}
      style={{
        overlay: { backgroundColor: "rgba(0, 0, 0, 0.5)" },
        content: {
          top: "50%",
          left: "50%",
          bottom: "auto",
          marginRight: "-50%",
          transform: "translate(-50%, -50%)",
          width: isFullScreen ? "90vw" : "50rem",
          height: isFullScreen ? "90vh" : "28rem",
          padding: "1rem",
          transition: "all 0.3s ease-in-out",
          display: "flex",
          flexDirection: "column",
        },
      }}
    >
      {/* Action Buttons */}
      <div
        style={{
          position: "absolute",
          top: "0.625rem",
          right: "0.625rem",
          display: "flex",
          gap: "0.75rem",
        }}
      >
        <button
          onClick={toggleFullScreen}
          style={{
            background: "transparent",
            border: "none",
            fontSize: "1.5rem",
            cursor: "pointer",
          }}
        >
          <i
            className={
              isFullScreen ? "bi bi-fullscreen-exit" : "bi bi-arrows-fullscreen"
            }
          ></i>
        </button>
        <button
          onClick={handleClose}
          style={{
            background: "transparent",
            border: "none",
            fontSize: "1.5rem",
            cursor: "pointer",
          }}
        >
          <i className="bi bi-x-circle-fill"></i>
        </button>
      </div>

      {/* Content */}
      <div className="flex flex-col h-full">
        <h3 className="text-lg font-semibold mb-1">KVz Zustände & Meldungen</h3>

        {/* LED Bereich */}
        <div className="w-full flex justify-between mb-4">
          <div className="flex items-center">
            <label className="text-sm font-semibold">
              KÜ {slotNumber !== null ? slotNumber + 1 : "-"}
            </label>
          </div>
          <div style={{ width: "12rem" }}>
            <FallSensors slotIndex={slotIndex} />
          </div>
          <div></div>
        </div>
        {/* Meldungen Bereich */}
        <div className="flex-1 border rounded bg-white overflow-hidden">
          <Report moduleType="KVZ" />
        </div>
      </div>
    </ReactModal>
  );
};

export default KVZChartView;
