// redux/slices/variablesSlice.ts
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { DataTDR } from "../types/chartDataTypesTDR";

// Typ für den State
export interface VariablesState {
  selectedChartData: DataTDR[] | null;
  selectedFileName: string | null;
  //------------
  kueBezeichnungen: string[];
  isolationsgrenzwerte: number[];
  verzoegerung: number[];
  untereSchleifenGrenzwerte: number[];
  obereSchleifenGrenzwerte: number[];
  schleifenintervall: number[];
  //---------------
  last20Messages: string | null;
  deviceName: string | null;
  mac1: string | null;
  ip: string | null;
  subnet: string | null;
  gateway: string | null;
  cplInternalTimestamp: string | null;
  ntp1: string | null;
  ntp2: string | null;
  ntp3: string | null;
  ntpTimezone: string | null;
  ntpActive: boolean | null;
  de: string | null;
  counter: number | null;
  flutter: string | null;
  kueOnline: string[] | number[];
  kueID: string[];
  kueIso: string[];
  kuePSTmMinus96V: number[];
  kueAlarm1: number[];
  kueAlarm2: number[];
  kueResidence: string[];
  kueCableBreak: number[];
  kueGroundFault: number[];
  kueLimit1: number | null;
  kueLimit2Low: number | null;
  kueDelay1: number | null;
  kueLoopInterval: number | null;
  kueVersion: number[] | string[];
  tdrAtten: number | null;
  tdrPulse: number | null;
  tdrSpeed: number | null;
  tdrAmp: number | null;
  tdrTrigger: number | null;
  tdrLocation: number | null;
  tdrActive: boolean | null;
  kueOverflow: number[] | null;
  tdrLast: string | null;
  appVersion: string | null;
  win_analogeEingaenge1: string | null;
  win_analogeEingaenge2: string | null;
  win_analogeEingaenge3: string | null;
  win_analogeEingaenge4: string | null;
  win_analogeEingaenge5: string | null;
  win_analogeEingaenge6: string | null;
  win_analogeEingaenge7: string | null;
  win_analogeEingaenge8: string | null;
  opcUaZustand: string | null;
  opcUaActiveClientCount: number | null;
  opcUaNodesetName: string | null;
}

// Initialer Zustand
const initialState: VariablesState = {
  selectedFileName: null,
  //------------
  selectedChartData: null,
  kueBezeichnungen: [],
  isolationsgrenzwerte: [],
  verzoegerung: [],
  untereSchleifenGrenzwerte: [],
  obereSchleifenGrenzwerte: [],
  schleifenintervall: [],
  //---------------
  last20Messages: null,
  deviceName: null,
  mac1: null,
  ip: null,
  subnet: null,
  gateway: null,
  cplInternalTimestamp: null,
  ntp1: null,
  ntp2: null,
  ntp3: null,
  ntpTimezone: null,
  ntpActive: null,
  de: null,
  counter: null,
  flutter: null,
  kueOnline: [],
  kueID: [],
  kueIso: [],
  kuePSTmMinus96V: [],
  kueAlarm1: [],
  kueAlarm2: [],
  kueResidence: [],
  kueCableBreak: [],
  kueGroundFault: [],
  kueLimit1: null,
  kueLimit2Low: null,
  kueDelay1: null,
  kueLoopInterval: null,
  kueVersion: [],
  tdrAtten: null,
  tdrPulse: null,
  tdrSpeed: null,
  tdrAmp: null,
  tdrTrigger: null,
  tdrLocation: null,
  tdrActive: null,
  kueOverflow: null,
  tdrLast: null,
  appVersion: null,
  win_analogeEingaenge1: null,
  win_analogeEingaenge2: null,
  win_analogeEingaenge3: null,
  win_analogeEingaenge4: null,
  win_analogeEingaenge5: null,
  win_analogeEingaenge6: null,
  win_analogeEingaenge7: null,
  win_analogeEingaenge8: null,
  opcUaZustand: null,
  opcUaActiveClientCount: null,
  opcUaNodesetName: null,
};

// Slice erstellen
const variablesSlice = createSlice({
  name: "variables",
  initialState,
  reducers: {
    setVariable(
      state,
      action: PayloadAction<{
        key: keyof VariablesState;
        value: VariablesState[keyof VariablesState];
      }>
    ) {
      const { key, value } = action.payload;
      (state[key] as VariablesState[keyof VariablesState]) = value;
    },
    setVariables(state, action: PayloadAction<Partial<VariablesState>>) {
      Object.entries(action.payload).forEach(([key, value]) => {
        (state[
          key as keyof VariablesState
        ] as VariablesState[keyof VariablesState]) = value!;
      });
    },
    setSelectedChartData(state, action: PayloadAction<DataTDR[] | null>) {
      state.selectedChartData = action.payload;
    },
    setSelectedFileName(state, action: PayloadAction<string | null>) {
      state.selectedFileName = action.payload;
    },
    setOpcUaZustand(state, action: PayloadAction<string | null>) {
      state.opcUaZustand = action.payload;
    },
    setOpcUaActiveClientCount(state, action: PayloadAction<number | null>) {
      state.opcUaActiveClientCount = action.payload;
    },
    setOpcUaNodesetName(state, action: PayloadAction<string | null>) {
      state.opcUaNodesetName = action.payload;
    },
  },
});

export const {
  setVariable,
  setVariables,
  setSelectedChartData,
  setSelectedFileName,
  setOpcUaZustand,
  setOpcUaActiveClientCount,
  setOpcUaNodesetName,
} = variablesSlice.actions;
export default variablesSlice.reducer;
