import { createFeature, createReducer, on } from '@ngrx/store';
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { Product } from '../../models/product.model';
import { PaginationMeta } from '../../models/api.model';
import { ProductsActions } from './products.actions';

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

export const productsAdapter: EntityAdapter<Product> = createEntityAdapter<Product>();

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

export const productsFeature = createFeature({
  name: 'products',
  reducer: createReducer(
    initialState,
    // Load
    on(ProductsActions.loadProducts, (state, action) =>
      state.loaded && !action.forceReload ? state : {
        ...state,
        loading: true,
        error: null,
      }
    ),
    on(ProductsActions.loadProductsSuccess, (state, { products, meta }) =>
      productsAdapter.setAll(products, { ...state, meta, loading: false, loaded: true })
    ),
    on(ProductsActions.loadProductsFailure, (state, { error }) => ({
      ...state,
      loading: false,
      error,
    })),
    // Create
    on(ProductsActions.createProduct, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(ProductsActions.createProductSuccess, (state, { product }) =>
      productsAdapter.addOne(product, { ...state, saving: false })
    ),
    on(ProductsActions.createProductFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Update
    on(ProductsActions.updateProduct, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(ProductsActions.updateProductSuccess, (state, { product }) =>
      productsAdapter.upsertOne(product, { ...state, saving: false })
    ),
    on(ProductsActions.updateProductFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Delete
    on(ProductsActions.deleteProduct, (state) => ({
      ...state,
      saving: true,
      error: null,
    })),
    on(ProductsActions.deleteProductSuccess, (state, { id }) =>
      productsAdapter.removeOne(id, { ...state, saving: false })
    ),
    on(ProductsActions.deleteProductFailure, (state, { error }) => ({
      ...state,
      saving: false,
      error,
    })),
    // Clear Error
    on(ProductsActions.clearProductsError, (state) => ({
      ...state,
      error: null,
    }))
  ),
});

const { selectAll, selectEntities, selectTotal } = productsAdapter.getSelectors(productsFeature.selectProductsState);

export const selectAllProducts = selectAll;
export const selectProductEntities = selectEntities;
export const selectProductsTotal = selectTotal;
export const selectProductsMeta = productsFeature.selectMeta;
export const selectProductsLoading = productsFeature.selectLoading;
export const selectProductsSaving = productsFeature.selectSaving;
export const selectProductsError = productsFeature.selectError;
export const selectProductsLoaded = productsFeature.selectLoaded;
