import { StoreAction } from 'store/utils/actions';
import produce from 'immer';
import { handleRequest, handleResponse } from 'store/utils/reducers';
import auth from 'store/auth';
import _ from 'lodash';
import actions from './actions';
import { resourceUsage } from 'process';

interface ShopState {
  detail: {} | null;
  products: {} | null;
  promos: {} | null;
  vatClass: {} | null;
  vatValues: {} | null;
}

export const initialState: ShopState = {
  detail: null,
  products: null,
  promos: null,
  vatClass: null,
  vatValues: null,
};

const treatList = (item): Record<string, any> => {
  const newItem = { ...item };

  return newItem;
};

const getResourceName = key => {
  const resource = key.split('GetMany').shift();

  if (resource.indexOf('orders') >= 0) return 'orders';
  if (resource.indexOf('promos') >= 0) return 'promos';
  if (resource.indexOf('vatClass') >= 0) return 'vatClass';
  if (resource.indexOf('products') >= 0) return 'products';

  return resource;
};

const reducer = (state: ShopState = initialState, action: StoreAction): ShopState => {
  return produce(
    state,
    (draft): ShopState => {
      let index;
      switch (action.type) {
        case actions.load.request.constant:
          if (action.payload.resource) handleRequest(draft, action, action.payload.resource, true);
          break;
        case actions.load.result.constant: {
          const key = Object.keys(action.payload);
          action.payload = action.payload[key[0]];
          const resource = getResourceName(key[0]);
          handleResponse(draft, action, resource, treatList);
          break;
        }
        case actions.edit.result.constant:
          if (action.payload) {
            draft.detail.data = { ...state.detail.data, ...action.payload };
          }
          break;
        // Load One
        case actions.loadOne.request.constant:
          if (state.detail && state.detail.data) {
            if (action.payload.apiInput.id === state.detail.data._id) {
              draft.loading = true;
            } else {
              handleRequest(draft, action, 'detail');
            }
          } else {
            handleRequest(draft, action, 'detail');
          }
          break;
        case actions.loadOne.result.constant:
          if (state.detail && state.detail.data) {
            if (action.payload._id && action.payload._id === state.detail.data._id) {
              draft.data = { ...state.detail.data, ...action.payload };
              draft.loading = false;
            } else {
              handleResponse(draft, action, 'detail');
            }
          } else {
            handleResponse(draft, action, 'detail');
          }
          break;
        case actions.delete.request.constant:
          if (action.payload.resource) {
            resource = action.payload.resource;
            draft[resource].data = _.filter(state[resource].data, e => e._id !== action.payload.id);
          }
          break;
        // VAT
        case actions.vat.loadValues.request.constant:
          handleRequest(draft, action, 'vatValues');
          break;
        case actions.vat.loadValues.result.constant:
          handleResponse(draft, action, 'vatValues');
          break;
        case actions.vat.resetValues.constant:
          draft.vatValues = null;
          break;
        // Orders
        case actions.order.cancel.result.constant:
          if (state.orders && state.orders.data) {
            index = state.orders.data.findIndex(e => e._id === action.payload._id);
            draft.orders.data.splice(index, 1);
          }
          if (state.detail?.data?._id === action.payload?._id) {
            draft.detail.data = action.payload;
          }
          break;
        case actions.order.reimburse.result.constant:
          if (state.orders && state.orders.data) {
            index = state.orders.data.findIndex(e => e._id === action.payload.id);
            draft.orders.data.splice(index, 1);
          }
          if (state.detail?.data?._id === action.payload?._id) {
            draft.detail.data = action.payload;
          }
          break;
        case actions.order.markPaid.result.constant:
          if (state.orders && state.orders.data) {
            index = state.orders.data.findIndex(e => e._id === action.payload._id);
            if (index !== -1) draft.orders.data[index] = action.payload;
          }
          if (state.detail?.data?._id === action.payload?._id) {
            draft.detail.data = action.payload;
          }
          break;
        case actions.order.markUnPaid.result.constant:
          if (state.orders && state.orders.data) {
            index = state.orders.data.findIndex(e => e._id === action.payload._id);
            if (index !== -1) draft.orders.data[index] = action.payload;
          }
          if (state.detail?.data?._id === action.payload?._id) {
            draft.detail.data = action.payload;
          }
          break;
        case auth.actions.logout.result.constant:
          draft.list = null;
          draft.detail = null;
          break;
        default:
      }
    },
  );
};

export default reducer;
