import { createFeature, createReducer, on } from '@ngrx/store';
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { PaginationMeta } from '../../../models/api.model';
import { HrShift } from '../../../models/hr-shift.model';
import { HrShiftsActions } from './shifts.actions';

export interface HrShiftsState extends EntityState<HrShift> {
  meta: PaginationMeta | null;
  loading: boolean;
  saving: boolean;
  error: string | null;
  loaded: boolean;
}

export const hrShiftsAdapter: EntityAdapter<HrShift> = createEntityAdapter<HrShift>();

const initialState: HrShiftsState = hrShiftsAdapter.getInitialState({
  meta: null,
  loading: false,
  saving: false,
  error: null,
  loaded: false,
});

export const hrShiftsFeature = createFeature({
  name: 'hrShifts',
  reducer: createReducer(
    initialState,
    // Load
    on(HrShiftsActions.loadHRShifts, (state, action) =>
      state.loaded && !action.forceReload
        ? state
        : {
            ...state,
            loading: true,
            error: null,
          }
    ),
    on(HrShiftsActions.loadHRShiftsSuccess, (state, { shifts, meta }) =>
      hrShiftsAdapter.setAll(shifts, { ...state, meta, loading: false, loaded: true })
    ),
    on(HrShiftsActions.loadHRShiftsFailure, (state, { error }) => ({
      ...state,
      loading: false,
      error,
    })),
    // Create
    on(HrShiftsActions.createHRShift, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(HrShiftsActions.createHRShiftSuccess, (state, { shift }) =>
      hrShiftsAdapter.addOne(shift, { ...state, saving: false })
    ),
    on(HrShiftsActions.createHRShiftFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Update
    on(HrShiftsActions.updateHRShift, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(HrShiftsActions.updateHRShiftSuccess, (state, { shift }) =>
      hrShiftsAdapter.upsertOne(shift, { ...state, saving: false })
    ),
    on(HrShiftsActions.updateHRShiftFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Delete
    on(HrShiftsActions.deleteHRShift, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(HrShiftsActions.deleteHRShiftSuccess, (state, { id }) =>
      hrShiftsAdapter.removeOne(id, { ...state, saving: false })
    ),
    on(HrShiftsActions.deleteHRShiftFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Clear Error
    on(HrShiftsActions.clearHRShiftsError, (state) => ({
      ...state,
      error: null,
    }))
  ),
});

const { selectAll, selectEntities, selectTotal } = hrShiftsAdapter.getSelectors(
  hrShiftsFeature.selectHrShiftsState
);

export const selectAllHrShifts = selectAll;
export const selectHrShiftEntities = selectEntities;
export const selectHrShiftsTotal = selectTotal;
export const selectHrShiftsMeta = hrShiftsFeature.selectMeta;
export const selectHrShiftsLoading = hrShiftsFeature.selectLoading;
export const selectHrShiftsSaving = hrShiftsFeature.selectSaving;
export const selectHrShiftsError = hrShiftsFeature.selectError;
export const selectHrShiftsLoaded = hrShiftsFeature.selectLoaded;
