"use client"; // /components/modules/kue705FO/charts/ChartSwitcher.tsx
import React, { useState } from "react";
import ReactModal from "react-modal";
import LoopChartActionBar from "./LoopMeasurementChart/LoopChartActionBar";
import TDRChartActionBar from "./TDRChart/TDRChartActionBar";
import LoopMeasurementChart from "./LoopMeasurementChart/LoopMeasurementChart";
import TDRChart from "./TDRChart/TDRChart";
import { useSelector, useDispatch } from "react-redux";
import { RootState } from "../../../../../redux/store";
import { setChartOpen } from "../../../../../redux/slices/kabelueberwachungChartSlice";

interface ChartSwitcherProps {
  isOpen: boolean;
  onClose: () => void;
}

const ChartSwitcher: React.FC<ChartSwitcherProps> = ({ isOpen, onClose }) => {
  const dispatch = useDispatch();
  const activeMode = useSelector(
    (state: RootState) => state.kueChartMode.activeMode
  );
  const [isFullScreen, setIsFullScreen] = useState(false);

  // **Modal schließen + Redux-Status zurücksetzen**
  const handleClose = () => {
    dispatch(setChartOpen(false)); // Schalter zurücksetzen
    onClose(); // Originale Schließen-Funktion aufrufen
  };

  // **Vollbildmodus umschalten**
  const toggleFullScreen = () => {
    setIsFullScreen(!isFullScreen);
  };

  return (
    <ReactModal
      isOpen={isOpen}
      onRequestClose={handleClose} // Hier `handleClose` statt `onClose`
      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 ? "100vw" : "70rem",
          height: isFullScreen ? "100vh" : "35rem",
          padding: "1rem",
          transition: "all 0.3s ease-in-out",
        },
      }}
    >
      {/* Schließen-Button */}
      <button
        onClick={handleClose}
        style={{
          position: "absolute",
          top: "0.625rem",
          right: "3rem",
          background: "transparent",
          border: "none",
          fontSize: "1.5rem",
          cursor: "pointer",
        }}
      >
        <i className="bi bi-x-circle-fill"></i>
      </button>

      {/* Fullscreen-Button */}
      <button
        onClick={toggleFullScreen}
        style={{
          position: "absolute",
          top: "0.625rem",
          right: "0.625rem",
          background: "transparent",
          border: "none",
          fontSize: "1.5rem",
          cursor: "pointer",
        }}
      >
        <i
          className={
            isFullScreen ? "bi bi-fullscreen-exit" : "bi bi-arrows-fullscreen"
          }
        ></i>
      </button>

      {/* Nur die richtige ActionBar und das richtige Chart basierend auf dem Modus anzeigen */}
      {activeMode === "Schleife" ? (
        <>
          <h3 className="text-lg font-semibold">Schleifenmessung</h3>
          <LoopChartActionBar />
          <LoopMeasurementChart />
        </>
      ) : (
        <>
          <h3 className="text-lg font-semibold">TDR-Messung</h3>
          <TDRChartActionBar />
          <TDRChart />
        </>
      )}
    </ReactModal>
  );
};

export default ChartSwitcher;
