// redux/slices/kabelueberwachungChartSlice.ts

import { createSlice, PayloadAction } from "@reduxjs/toolkit";

// Definition des Interface für den Zustand des TDR-Charts
interface TDRData {
  timestamp: string;
  tdr: number;
}

// Definition des Interface für den gesamten Zustand der Kabelüberwachung
interface KabelueberwachungChartState {
  loopMeasurementCurveChartData: any[];
  vonDatum: string;
  bisDatum: string;
  selectedMode: "DIA0" | "DIA1" | "DIA2";
  selectedSlotType: "isolationswiderstand" | "schleifenwiderstand";
  isChartOpen: boolean;
  slotNumber: number | null;
  tdrChartData: TDRData[];
  isFullScreen: boolean;
  unit: "kOhm" | "MOhm";
  isLoading: boolean;
}

// Dynamische Initialisierung der Datumswerte
const today = new Date();
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(today.getDate() - 30);

// Initialer Zustand des Slices
const initialState: KabelueberwachungChartState = {
  isLoading: false,
  loopMeasurementCurveChartData: [],
  vonDatum: thirtyDaysAgo.toISOString().split("T")[0], // 30 Tage zurück
  bisDatum: today.toISOString().split("T")[0], // Heute
  selectedMode: "DIA0",
  selectedSlotType: "isolationswiderstand",
  isChartOpen: false,
  slotNumber: null,
  tdrChartData: [],
  isFullScreen: false,
  unit: "MOhm",
};

// Erstellung des Slices
const kabelueberwachungChartSlice = createSlice({
  name: "kabelueberwachungChartSlice",
  initialState,
  reducers: {
    setSlotNumber: (state, action: PayloadAction<number | null>) => {
      state.slotNumber = action.payload;
    },
    setLoopMeasurementCurveChartData: (state, action: PayloadAction<any[]>) => {
      state.loopMeasurementCurveChartData = action.payload;
    },
    setVonDatum: (state, action: PayloadAction<string>) => {
      state.vonDatum = action.payload;
    },
    setBisDatum: (state, action: PayloadAction<string>) => {
      state.bisDatum = action.payload;
    },
    setSelectedMode: (
      state,
      action: PayloadAction<"DIA0" | "DIA1" | "DIA2">
    ) => {
      state.selectedMode = action.payload;
    },
    setSelectedSlotType: (
      state,
      action: PayloadAction<"isolationswiderstand" | "schleifenwiderstand">
    ) => {
      state.selectedSlotType = action.payload;
      state.unit = action.payload === "schleifenwiderstand" ? "kOhm" : "MOhm";
    },
    setChartOpen: (state, action: PayloadAction<boolean>) => {
      state.isChartOpen = action.payload;
    },
    setTDRChartData: (
      state,
      action: PayloadAction<{ timestamp: string; tdr: number }[]>
    ) => {
      state.tdrChartData = action.payload;
    },
    setFullScreen: (state, action: PayloadAction<boolean>) => {
      state.isFullScreen = action.payload;
    },
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.isLoading = action.payload;
    },
  },
});

// Export der Aktionen
export const {
  setSlotNumber,
  setLoopMeasurementCurveChartData,
  setVonDatum,
  setBisDatum,
  setSelectedMode,
  setSelectedSlotType,
  setChartOpen,
  setTDRChartData,
  setFullScreen,
  setLoading,
} = kabelueberwachungChartSlice.actions;

// Export des Reducers
export default kabelueberwachungChartSlice.reducer;
