import {createReducer} from '@reduxjs/toolkit';

import * as Constants from '../../constants';
import {
  addRemoteMount2,
  addS3Mount2,
  DISPLAY_CONFIGURATION,
  removeRemoteMount3,
  RESET_MOUNTS_STATE,
  SET_ALLOW_MOUNT,
  SET_AUTO_MOUNT_PROCESSED,
  SET_MOUNT_STATE,
  SET_MOUNTED,
  SET_PROVIDER_STATE,
  setBusy
} from '../actions/mount_actions';

export const createMountReducer = state => {
  let providerList = [
    ...Constants.PROVIDER_LIST,
    ...(state.RemoteMounts || []),
    ...(state.S3Mounts || []),
  ];
  const providerState = providerList
  .map(provider => {
    return {
      [provider]: {
        AutoMount: false,
        AutoRestart: false,
        MountLocation: '',
      }
    }
  })
  .reduce((map, obj) => {
    return {...map, ...obj}
  });

  const mountState = providerList
  .map(provider => {
    return {
      [provider]: {
        AllowMount: false,
        DriveLetters: [],
        Mounted: false,
      }
    }
  })
  .reduce((map, obj) => {
    return {...map, ...obj}
  });

  const autoMountProcessed =
    providerList.map(provider => {
      return {[provider]: false,}
    })
    .reduce((map, obj) => {
      return {...map, ...obj}
    });

  return createReducer(
    {
      AutoMountProcessed: autoMountProcessed,
      DisplayConfiguration: null,
      DisplayRemoteConfiguration: false,
      DisplayS3Configuration: false,
      MountsBusy: false,
      MountState: mountState,
      ProviderState: providerState,
      RemoteMounts: state.RemoteMounts ? state.RemoteMounts : [],
      S3Mounts: state.S3Mounts ? state.S3Mounts : [],
    },
    {
      [addRemoteMount2]: (state, action) => {
        let mountState = {...state.MountState};
        mountState[action.payload] = {
          AllowMount: false,
          DriveLetters: [],
          Mounted: false,
        };

        let providerState = {...state.ProviderState};
        providerState[action.payload] = {
          AutoMount: false,
          AutoRestart: false,
          MountLocation: '',
        };

        let autoMountProcessed = {...state.AutoMountProcessed};
        autoMountProcessed[action.payload] = true;

        return {
          ...state, AutoMountProcessed: autoMountProcessed,
          MountState: mountState, ProviderState: providerState,
          RemoteMounts: [...state.RemoteMounts, action.payload],
        }
      },
      [addS3Mount2]: (state, action) => {
        let mountState = {...state.MountState};
        mountState[action.payload] = {
          AllowMount: false,
          DriveLetters: [],
          Mounted: false,
        };

        let providerState = {...state.ProviderState};
        providerState[action.payload] = {
          AutoMount: false,
          AutoRestart: false,
          MountLocation: '',
        };

        let autoMountProcessed = {...state.AutoMountProcessed};
        autoMountProcessed[action.payload] = true;

        return {
          ...state, AutoMountProcessed: autoMountProcessed,
          MountState: mountState, ProviderState: providerState,
          S3Mounts: [...state.S3Mounts, action.payload],
        }
      },
      [DISPLAY_CONFIGURATION]: (state, action) => {
        return {
          ...state,
          DisplayConfiguration: action.payload.provider,
          DisplayRemoteConfiguration: action.payload.remote,
          DisplayS3Configuration: action.payload.s3,
        };
      },
      [removeRemoteMount3]: (state, action) => {
        let mountState = {...state.MountState};
        delete mountState[action.payload];

        let providerState = {...state.ProviderState};
        delete providerState[action.payload];

        let autoMountProcessed = {...state.AutoMountProcessed};
        delete autoMountProcessed[action.payload];

        const remoteMounts =
          state.RemoteMounts.filter(i => i !== action.payload);
        return {
          ...state,
          AutoMountProcessed: autoMountProcessed,
          MountState: mountState,
          ProviderState: providerState,
          RemoteMounts: remoteMounts,
        };
      },
      [RESET_MOUNTS_STATE]: (state, action) => {
        return {...state, MountsBusy: false, MountState: mountState,}
      },
      [SET_AUTO_MOUNT_PROCESSED]: (state, action) => {
        return {
          ...state,
          AutoMountProcessed: {
            ...state.AutoMountProcessed,
            [action.payload.provider]: action.payload.processed,
          }
        };
      },
      [SET_ALLOW_MOUNT]: (state, action) => {
        return {
          ...state,
          MountState: {
            ...state.MountState,
            [action.payload.provider]: {
              ...state.MountState[action.payload.provider],
              AllowMount: action.payload.allow,
            }
          }
        };
      },
      [setBusy]:
        (state,
         action) => {
          return {...state, MountsBusy: action.payload};
        },
      [SET_MOUNT_STATE]: (state, action) => {
        return {
          ...state,
          MountState: {
            ...state.MountState,
            [action.payload.provider]: {
              ...state.MountState[action.payload.provider],
              ...action.payload.state
            },
          }
        };
      },
      [SET_MOUNTED]: (state, action) => {
        return {
          ...state,
          MountState: {
            ...state.MountState,
            [action.payload.provider]: {
              ...state.MountState[action.payload.provider],
              Mounted: action.payload.mounted,
            }
          }
        };
      },
      [SET_PROVIDER_STATE]: (state, action) => {
        return {
          ...state,
          ProviderState: {
            ...state.ProviderState,
            [action.payload.provider]: {
              ...state.ProviderState[action.payload.provider],
              ...action.payload.state
            },
          }
        };
      }
    });
};
