"use client";

import React, { useEffect, useRef } from "react";
import { Listbox } from "@headlessui/react";
import ReactModal from "react-modal";
import IsoMeasurementChart from "./IsoMeasurementChart";
import IsoChartActionBar from "./IsoChartActionBar";
import Report from "./Report";
import { useSelector, useDispatch } from "react-redux";
import { AppDispatch, RootState } from "@/redux/store";
import {
  setChartOpen,
  setFullScreen,
  setSlotNumber,
  setChartTitle,
  setVonDatum,
  setBisDatum,
  setSelectedMode,
  setSelectedSlotType,
} from "@/redux/slices/kabelueberwachungChartSlice";
import { resetBrushRange } from "@/redux/slices/brushSlice";
import { resetDateRange } from "@/redux/slices/dateRangePickerSlice";

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

type ActionBarRefType = { handleFetchData: () => void };

const IsoChartView: React.FC<IsoChartViewProps> = ({
  isOpen,
  onClose,
  slotIndex,
}) => {
  const dispatch = useDispatch<AppDispatch>();
  const { isFullScreen, chartTitle } = useSelector(
    (state: RootState) => state.kabelueberwachungChartSlice
  );

  const actionBarRef = useRef<ActionBarRefType>(null);

  const initDates = () => {
    const today = new Date();
    const thirtyDaysAgo = new Date();
    thirtyDaysAgo.setDate(today.getDate() - 30);
    const toISO = (d: Date) => d.toLocaleDateString("sv-SE");
    dispatch(setVonDatum(toISO(thirtyDaysAgo)));
    dispatch(setBisDatum(toISO(today)));
  };

  const handleClose = () => {
    initDates();
    dispatch(resetDateRange());
    dispatch(setSelectedMode("DIA0"));
    dispatch(setSelectedSlotType("isolationswiderstand"));
    dispatch(setChartTitle("Messkurve"));
    dispatch(setChartOpen(false));
    dispatch(setFullScreen(false));
    dispatch(resetBrushRange());
    onClose();
  };

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

  useEffect(() => {
    if (isOpen) {
      dispatch(setSlotNumber(slotIndex));
      // inline initDates to avoid extra dependency
      const today = new Date();
      const thirtyDaysAgo = new Date();
      thirtyDaysAgo.setDate(today.getDate() - 30);
      const toISO = (d: Date) => d.toLocaleDateString("sv-SE");
      dispatch(setVonDatum(toISO(thirtyDaysAgo)));
      dispatch(setBisDatum(toISO(today)));
      dispatch(setSelectedSlotType("isolationswiderstand"));
      dispatch(setSelectedMode("DIA0"));
      dispatch(setChartTitle("Messkurve"));
      const t = setTimeout(() => actionBarRef.current?.handleFetchData(), 120);
      return () => clearTimeout(t);
    }
  }, [isOpen, slotIndex, dispatch]);

  return (
    <ReactModal
      isOpen={isOpen}
      onRequestClose={handleClose}
      ariaHideApp={false}
      style={{
        overlay: {
          backgroundColor: "rgba(0,0,0,0.55)",
          backdropFilter: "blur(2px)",
        },
        content: {
          inset: "50% auto auto 50%",
          transform: "translate(-50%, -50%)",
          width: isFullScreen ? "90vw" : "72rem",
          height: isFullScreen ? "90vh" : "38rem",
          padding: 0,
          border: "1px solid var(--color-border)",
          background: "var(--color-surface)",
          borderRadius: "14px",
          display: "flex",
          flexDirection: "column",
          overflow: "hidden",
        },
      }}
      contentLabel="Isolationswiderstand"
    >
      <header className="modal-header relative pr-56">
        <h3 className="text-sm font-semibold tracking-wide">
          Isolationswiderstand
        </h3>
        <div className="absolute top-2 right-2 flex gap-2">
          <button
            onClick={toggleFullScreen}
            className="icon-btn"
            aria-label={isFullScreen ? "Vollbild verlassen" : "Vollbild"}
            type="button"
          >
            <i
              className={
                isFullScreen
                  ? "bi bi-fullscreen-exit"
                  : "bi bi-arrows-fullscreen"
              }
            />
          </button>
          <button
            onClick={handleClose}
            className="icon-btn"
            aria-label="Schließen"
            type="button"
          >
            <i className="bi bi-x-lg" />
          </button>
        </div>
        <div className="absolute top-2 right-28">
          <Listbox
            value={chartTitle}
            onChange={(value: "Messkurve" | "Meldungen") =>
              dispatch(setChartTitle(value))
            }
          >
            <div className="relative w-40">
              <Listbox.Button className="dropdown-surface w-full flex items-center justify-between h-8">
                <span className="dropdown-text-fix">{chartTitle}</span>
                <i className="bi bi-chevron-down text-sm opacity-70" />
              </Listbox.Button>
              <Listbox.Options className="dropdown-options absolute z-50 mt-1 w-full max-h-60 overflow-auto text-sm">
                {(["Messkurve", "Meldungen"] as const).map((option) => (
                  <Listbox.Option
                    key={option}
                    value={option}
                    className={({ selected, active }) =>
                      `px-3 py-1.5 cursor-pointer rounded-sm m-0.5 ${
                        selected
                          ? "dropdown-option-active"
                          : active
                          ? "dropdown-option-hover"
                          : ""
                      }`
                    }
                  >
                    {option}
                  </Listbox.Option>
                ))}
              </Listbox.Options>
            </div>
          </Listbox>
        </div>
      </header>
      <div className="flex flex-col flex-1 p-3 gap-3">
        <IsoChartActionBar ref={actionBarRef} />
        <div className="flex-1 relative">
          {chartTitle === "Messkurve" ? (
            <IsoMeasurementChart />
          ) : (
            <Report moduleType="ISO" autoLoad={chartTitle === "Meldungen"} />
          )}
        </div>
      </div>
    </ReactModal>
  );
};

export default IsoChartView;
