import ipcTypes from "../../ipc.types";
import ApplicationActionTypes from "./application.types";
const INITIAL_STATE = {
  watcherStatus: "Not Started",
  watchedPaths: [],
  watcherError: null,
  selectedJobId: null,
  selectedJobTargetPc: 0,
  settings: {},
  updateAvailable: false,
  updateProgress: null,
  releaseNotes: null,
  esResults: {
    jobid: null,
    identified_items: []
  }
};

const { ipcRenderer } = window;

const applicationReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case ApplicationActionTypes.SET_WATCHED_PATHS:
      return {
        ...state,
        watchedPaths: action.payload,
      };
    case ApplicationActionTypes.ADD_WATCHED_PATH:
      ipcRenderer.send(ipcTypes.app.toMain.track, {
        event: "ADD_WATCHED_PATH",
      });
      return {
        ...state,
        watchedPaths: [...state.watchedPaths, action.payload],
      };
    case ApplicationActionTypes.REMOVE_WATCHED_PATH:
      ipcRenderer.send(ipcTypes.app.toMain.track, {
        event: "REMOVE_WATCHED_PATH",
      });
      return {
        ...state,
        watchedPaths: state.watchedPaths.filter((p) => p !== action.payload),
      };
    case ApplicationActionTypes.SET_WATCHER_STATUS:
      return {
        ...state,
        watcherStatus: action.payload,
      };
    case ApplicationActionTypes.SET_WATCHER_ERROR:
      ipcRenderer.send(ipcTypes.app.toMain.track, {
        event: "WATCHER_ERROR",
        error: action.payload,
      });
      return {
        ...state,
        watcherError: action.payload,
      };
    case ApplicationActionTypes.SET_SELECTED_JOB_TARGET_PC_SUCCESS:
      return {
        ...state,
        selectedJobTargetPc: action.payload,
      };
    case ApplicationActionTypes.SET_SELECTED_JOB_ID:
      // ipcRenderer.send(ipcTypes.app.toMain.track, {
      //   event: "SET_SELECTED_JOB",
      // });
      return { ...state, selectedJobId: action.payload };
    case ApplicationActionTypes.SET_SETTINGS:
      return { ...state, settings: { ...state.settings, ...action.payload } };
    case ApplicationActionTypes.SET_UPDATE_AVAILABLE:
      return { ...state, updateAvailable: action.payload };
    case ApplicationActionTypes.SET_UPDATE_PROGRESS:
      return { ...state, updateProgress: action.payload };
    case ApplicationActionTypes.SET_RELEASE_NOTES:
      return { ...state, releaseNotes: action.payload };
    case ApplicationActionTypes.SET_ES_RESULTS:
      return { ...state, esResults: action.payload };
    case ApplicationActionTypes.CLEAR_ES_RESULTS:
      return { ...state, esResults: { jobid: null, identified_items: [] } };
    default:
      return state;
  }
};

export default applicationReducer;
