import { createFeature, createReducer, on } from '@ngrx/store';
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { UserListItem } from '../../models/user.model';
import { PaginationMeta } from '../../models/api.model';
import { UsersActions } from './users.actions';

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

export const usersAdapter: EntityAdapter<UserListItem> = createEntityAdapter<UserListItem>();

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

export const usersFeature = createFeature({
  name: 'users',
  reducer: createReducer(
    initialState,
    // Load
    on(UsersActions.loadUsers, (state, action) =>
      state.loaded && !action.forceReload ? state : {
        ...state,
        loading: true,
        error: null,
      }
    ),
    on(UsersActions.loadUsersSuccess, (state, { users, meta }) =>
      usersAdapter.setAll(users, { ...state, meta, loading: false, loaded: true })
    ),
    on(UsersActions.loadUsersFailure, (state, { error }) => ({
      ...state,
      loading: false,
      error,
    })),
    // Create
    on(UsersActions.createUser, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(UsersActions.createUserSuccess, (state, { user }) =>
      usersAdapter.addOne(user, { ...state, saving: false })
    ),
    on(UsersActions.createUserFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Update
    on(UsersActions.updateUser, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(UsersActions.updateUserSuccess, (state, { user }) =>
      usersAdapter.upsertOne(user, { ...state, saving: false })
    ),
    on(UsersActions.updateUserFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Delete
    on(UsersActions.deleteUser, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(UsersActions.deleteUserSuccess, (state, { id }) =>
      usersAdapter.removeOne(id, { ...state, saving: false })
    ),
    on(UsersActions.deleteUserFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Clear Error
    on(UsersActions.clearUsersError, (state) => ({
      ...state,
      error: null,
    }))
  ),
});

const { selectAll, selectEntities, selectTotal } = usersAdapter.getSelectors(usersFeature.selectUsersState);

export const selectAllUsers = selectAll;
export const selectUserEntities = selectEntities;
export const selectUsersTotal = selectTotal;
export const selectUsersMeta = usersFeature.selectMeta;
export const selectUsersLoading = usersFeature.selectLoading;
export const selectUsersSaving = usersFeature.selectSaving;
export const selectUsersError = usersFeature.selectError;
export const selectUsersLoaded = usersFeature.selectLoaded;
