import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import log from "electron-log/renderer";
import type { RootState } from "./redux-store";

interface AppState {
  value: number;
  watcher: {
    started: boolean;
    error: string | null;
    polling: {
      enabled: boolean;
      interval: number;
    };
  };
  updates: {
    available: boolean;
    checking: boolean;
    progress: number;
    speed: number;
    completed: boolean;
  };
}

// Define the initial state using that type
const initialState: AppState = {
  value: 0,
  watcher: {
    started: false,
    error: null,
    polling: {
      enabled: false,
      interval: 30000,
    },
  },
  updates: {
    available: false,
    checking: false,
    progress: 0,
    speed: 0,
    completed: false,
  },
};

export const appSlice = createSlice({
  name: "app",
  // `createSlice` will infer the state type from the `initialState` argument
  initialState,
  reducers: {
    watcherStarted: (state) => {
      state.watcher.started = true;
    },
    watcherStopped: (state) => {
      state.watcher.started = false;
    },
    watcherError: (state, action: PayloadAction<string>) => {
      state.watcher.error = action.payload;
      state.watcher.started = false;
      log.error("[Redux] AppSlice: Watcher Error", action.payload);
    },

    updateChecking: (state) => {
      state.updates.checking = true;
    },
    updateAvailable: (state) => {
      state.updates.available = true;
      state.updates.checking = false;
    },
    updateProgress: (
      state,
      action: PayloadAction<{ progress: number; speed: number }>,
    ) => {
      state.updates.available = true;
      state.updates.progress = action.payload.progress;
      state.updates.speed = action.payload.speed;
    },
    updateDownloaded: (state) => {
      state.updates.completed = true;
      state.updates.progress = 100;
      state.updates.speed = 0;
    },
    setWatcherPolling: (
      state,
      action: PayloadAction<{ enabled: boolean; interval: number }>,
    ) => {
      state.watcher.polling.enabled = action.payload.enabled;
      state.watcher.polling.interval = action.payload.interval;
    },
  },
});

export const {
  watcherError,
  watcherStarted,
  watcherStopped,
  updateAvailable,
  updateChecking,
  updateDownloaded,
  updateProgress,
  setWatcherPolling,
} = appSlice.actions;

// Other code such as selectors can use the imported `RootState` type
export const selectWatcherStatus = (state: RootState): boolean =>
  state.app.watcher.started;

export const selectWatcherError = (state: RootState): string | null =>
  state.app.watcher.error;

export const selectUpdateAvailable = (state: RootState): boolean =>
  state.app.updates.available;

export const selectAppUpdateProgress = (state: RootState): number =>
  state.app.updates.progress;

export const selectAppUpdateSpeed = (state: RootState): number =>
  state.app.updates.speed;

export const selectAppUpdateCompleted = (state: RootState): boolean =>
  state.app.updates.completed;

export const selectWatcherPolling = (
  state: RootState,
): {
  enabled: boolean;
  interval: number;
} => state.app.watcher.polling;

//Async Functions - Thunks
// Define a thunk that dispatches those action creators
// const fetchUsers = () => async (dispatch) => {
//   //dispatch(watcherStarted());
//   //Some sort of async action.
//   // dispatch(incrementByAmount(100));
// };

export default appSlice.reducer;
