import * as Constants from '../../constants';
import {createAction} from '@reduxjs/toolkit';
import {getIPCRenderer} from '../../utils';

const ipcRenderer = getIPCRenderer();
let yesNoResolvers = [];

export const confirmYesNo = title => {
  return dispatch => {
    return new Promise(resolve => {
      dispatch(handleConfirmYesNo(true, title, resolve));
    });
  };
};

export const DISPLAY_CONFIRM_YES_NO = 'common/displayConfirmYesNo';
const displayConfirmYesNo = (show, title) => {
  return {
    type: DISPLAY_CONFIRM_YES_NO,
    payload: {
      show,
      title
    },
  };
};

export const displaySelectAppPlatform = display => {
  return dispatch => {
    if (display) {
      dispatch(showWindow());
    }

    dispatch(setDisplaySelectAppPlatform(display));
  };
};

export const hideConfirmYesNo = confirmed => {
  return dispatch => {
    dispatch(handleConfirmYesNo(false, confirmed));
  };
};

const handleConfirmYesNo = (show, titleOrConfirmed, resolve) => {
  return dispatch => {
    if (show) {
      yesNoResolvers.push(resolve);
      dispatch(displayConfirmYesNo(show, titleOrConfirmed));
    } else {
      yesNoResolvers[0](titleOrConfirmed);
      yesNoResolvers.splice(0, 1);
      dispatch(displayConfirmYesNo(false));
    }
  };
};

export const NOTIFY_APPLICATION_BUSY = 'common/notifyApplicationBusy';
export const notifyApplicationBusy = (busy, transparent) => {
  return {
    type: NOTIFY_APPLICATION_BUSY,
    payload: {
      busy,
      transparent
    },
  };
};

export const notifyRebootRequired = createAction('common/notifyRebootRequired');

export const rebootSystem = () => {
  return dispatch => {
    dispatch(setApplicationReady(false));
    if (ipcRenderer) {
      ipcRenderer.send(Constants.IPC_Reboot_System);
    }
  }
};

export const saveState = () => {
  return (dispatch, getState) => {
    const state = getState();
    if (state.common.AppReady) {
      let currentState = {
        Release: state.relver.Release,
        RemoteMounts: state.mounts.RemoteMounts,
        Version: state.relver.Version,
      };

      const providerList = [
        ...Constants.PROVIDER_LIST,
        ...state.mounts.RemoteMounts,
      ];
      for (const provider of providerList) {
        currentState[provider] = state.mounts.ProviderState[provider];
      }

      if (ipcRenderer) {
        ipcRenderer.send(Constants.IPC_Save_State, {
          State: currentState
        });
      }
    }
  };
};

export const setAllowMount = createAction('common/setAllowMount');
export const setApplicationReady = createAction('common/setApplicationReady');

export const SET_DISPLAY_SELECT_APPPLATFORM = 'common/displaySelectAppPlatform';
export const setDisplaySelectAppPlatform = display => {
  return {
    type: SET_DISPLAY_SELECT_APPPLATFORM,
    payload: display,
  };
};

export const setLinuxAppPlatform = createAction('common/setLinuxAppPlatform');

export const setRebootRequired = () => {
  return dispatch => {
    dispatch(showWindow());
    dispatch(notifyRebootRequired(true));
  };
};

export const showWindow = () => {
  return dispatch => {
    if (ipcRenderer) {
      ipcRenderer.send(Constants.IPC_Show_Window);
    }
  };
};

export const shutdownApplication = () => {
  return dispatch => {
    dispatch(setApplicationReady(false));
    if (ipcRenderer) {
      ipcRenderer.send(Constants.IPC_Shutdown);
    }
  };
};
