import { createFeature, createReducer, on } from '@ngrx/store';
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { PaginationMeta } from '../../../models/api.model';
import { HrPosition } from '../../../models/hr-position.model';
import { HrPositionsActions } from './positions.actions';

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

export const hrPositionsAdapter: EntityAdapter<HrPosition> =
  createEntityAdapter<HrPosition>();

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

export const hrPositionsFeature = createFeature({
  name: 'hrPositions',
  reducer: createReducer(
    initialState,
    // Load
    on(HrPositionsActions.loadHRPositions, (state, action) =>
      state.loaded && !action.forceReload
        ? state
        : {
            ...state,
            loading: true,
            error: null,
          }
    ),
    on(HrPositionsActions.loadHRPositionsSuccess, (state, { positions, meta }) =>
      hrPositionsAdapter.setAll(positions, { ...state, meta, loading: false, loaded: true })
    ),
    on(HrPositionsActions.loadHRPositionsFailure, (state, { error }) => ({
      ...state,
      loading: false,
      error,
    })),
    // Create
    on(HrPositionsActions.createHRPosition, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(HrPositionsActions.createHRPositionSuccess, (state, { position }) =>
      hrPositionsAdapter.addOne(position, { ...state, saving: false })
    ),
    on(HrPositionsActions.createHRPositionFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Update
    on(HrPositionsActions.updateHRPosition, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(HrPositionsActions.updateHRPositionSuccess, (state, { position }) =>
      hrPositionsAdapter.upsertOne(position, { ...state, saving: false })
    ),
    on(HrPositionsActions.updateHRPositionFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Delete
    on(HrPositionsActions.deleteHRPosition, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(HrPositionsActions.deleteHRPositionSuccess, (state, { id }) =>
      hrPositionsAdapter.removeOne(id, { ...state, saving: false })
    ),
    on(HrPositionsActions.deleteHRPositionFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Clear Error
    on(HrPositionsActions.clearHRPositionsError, (state) => ({
      ...state,
      error: null,
    }))
  ),
});

const { selectAll, selectEntities, selectTotal } = hrPositionsAdapter.getSelectors(
  hrPositionsFeature.selectHrPositionsState
);

export const selectAllHrPositions = selectAll;
export const selectHrPositionEntities = selectEntities;
export const selectHrPositionsTotal = selectTotal;
export const selectHrPositionsMeta = hrPositionsFeature.selectMeta;
export const selectHrPositionsLoading = hrPositionsFeature.selectLoading;
export const selectHrPositionsSaving = hrPositionsFeature.selectSaving;
export const selectHrPositionsError = hrPositionsFeature.selectError;
export const selectHrPositionsLoaded = hrPositionsFeature.selectLoaded;
