import {createReducer} from '@reduxjs/toolkit';
import {
  DISPLAY_CONFIRM_YES_NO,
  NOTIFY_APPLICATION_BUSY,
  notifyRebootRequired,
  setAllowMount,
  setApplicationReady,
  setLinuxAppPlatform,
  SET_DISPLAY_SELECT_APPPLATFORM
} from '../actions/common_actions';

export const createCommonReducer = (platformInfo, version) => {
  return createReducer({
    AllowMount: false,
    AppBusy: false,
    AppBusyTransparent: false,
    AppPlatform: platformInfo.AppPlatform,
    AppReady: false,
    DisplayConfirmYesNo: false,
    ConfirmTitle: null,
    DisplaySelectAppPlatform: false,
    Platform: platformInfo.Platform,
    RebootRequired: false,
    Version: version,
  }, {
    [DISPLAY_CONFIRM_YES_NO]: (state, action) => {
      return {
        ...state,
        DisplayConfirmYesNo: action.payload.show,
        ConfirmTitle: action.payload.show ? action.payload.title : null,
      }
    },
    [SET_DISPLAY_SELECT_APPPLATFORM]: (state, action) => {
      return {
        ...state,
        DisplaySelectAppPlatform: action.payload,
      }
    },
    [setAllowMount]: (state, action) => {
      return {
        ...state,
        AllowMount: action.payload,
      }
    },
    [setApplicationReady]: (state, action) => {
      return {
        ...state,
        AppReady: action.payload,
      };
    },
    [setLinuxAppPlatform]: (state, action) => {
      return {
        ...state,
        AppPlatform: action.payload,
      }
    },
    [NOTIFY_APPLICATION_BUSY]: (state, action) => {
      return {
        ...state,
        AppBusy: action.payload.busy,
        AppBusyTransparent: action.payload.busy && action.payload.transparent,
      };
    },
    [notifyRebootRequired]: (state, action) => {
      return {
        ...state,
        RebootRequired: action.payload,
      };
    },
  });
};
