/* eslint-disable no-case-declarations */
import { StoreAction } from 'store/utils/actions';
import produce from 'immer';
import { handleRequest, handleResponse, handleUpdateResponse } from 'store/utils/reducers';
import auth from 'store/auth';
import { readlink } from 'fs';
import actions from './actions';

interface AccountState {
  profile: object;
}

const initialState: AccountState = {
  profile: null,
};

const treatProfile = (item): object => {
  const newItem = { ...item };

  return newItem;
};

const reducer = (state: AccountState = initialState, action: StoreAction): AccountState =>
  produce(
    state,
    (draft): AccountState => {
      switch (action.type) {
        case actions.saveRedirect.constant:
          draft.redirect = action?.payload;
          break;
        case actions.profile.request.constant:
          handleRequest(draft, action, 'profile');
          break;
        case actions.profile.result.constant:
          handleResponse(draft, action, 'profile', treatProfile);
          break;
        case actions.updateProfile.result.constant:
          handleResponse(draft, action, 'profile', treatProfile);
          break;
        case actions.addBillingInfos.result.constant:
          // if (!action.status === 'fulfilled') return;
          // draft.profile.data.paymentInfo = draft.profile.data.paymentInfo || {};
          // draft.profile.data.paymentInfo.billingInfos =
          //   draft.profile.data.paymentInfo.billingInfos || [];
          // draft.profile.data.paymentInfo.billingInfos.push(action.payload);
          break;
        case actions.updateBillingInfos.result.constant:
          // if (!action.status === 'fulfilled') return;
          // if (action.payload._id) {
          //   const index = _.findIndex(draft.profile.data.paymentInfo.billingInfos, function(
          //     address,
          //   ) {
          //     return address._id === action.requestData.id;
          //   });

          //   draft.profile.data.paymentInfo.billingInfos[index] = action.payload;
          // }
          break;
        case actions.deleteBillingInfos.result.constant:
          // if (!action.status === 'fulfilled') return;
          // const i = _.findIndex(draft.profile.data.paymentInfo.billingInfos, function(address) {
          //   return address._id === action.requestData.id;
          // });

          // draft.profile.data.paymentInfo.billingInfos.splice(i, 1);
          break;
        case actions.addFavoriteToAccount.result.constant:
          // draft.profile.data.getFavorites.push(action.payload);
          break;
        case actions.removeFavoriteToAccount.request.constant:
          // const found = _.find(draft.profile.data.getFavorites, favorite => {
          //   return favorite.ressourceId === action.payload.data.ressourceId;
          // });
          // draft.profile.data.getFavorites.splice(found, 1);
          break;
        case actions.addPaymentMethod.result.constant:
          // if (!action.status === 'fulfilled') return;

          // draft.profile.data.paymentInfo = draft.profile.data.paymentInfo || {};
          // draft.profile.data.paymentInfo.paymentMethods =
          //   draft.profile.data.paymentInfo.paymentMethods || [];

          // if (action.payload) {
          //   draft.profile.data.paymentInfo.paymentMethods.push(action.payload);
          // }
          break;
        case actions.deletePaymentMethod.result.constant:
          // if (!action.status === 'fulfilled') return;
          // const index = _.findIndex(draft.profile.data.paymentInfo.paymentMethods, {
          //   _id: action.requestData.id,
          // });

          // if (index >= 0) {
          //   draft.profile.data.paymentInfo.paymentMethods.splice(index, 1);
          // }
          break;
        case actions.updateDefaultPaymentMethod.result.constant:
          // change old default
          /* const oldDefault = _.findIndex(draft.profile.data.paymentInfo.paymentMethods, {
            default: true,
          });

          if (oldDefault >= 0)
            draft.profile.data.paymentInfo.paymentMethods[oldDefault].default = false;

          const paymentIndex = _.findIndex(draft.profile.data.paymentInfo.paymentMethods, {
            _id: action.requestData.id,
          });

          console.log('paymentDefault', paymentIndex);

          if (paymentIndex >= 0)
            draft.profile.data.paymentInfo.paymentMethods[paymentIndex] = action.payload; */
          break;
        case auth.actions.logout.result.constant:
          draft.profile = {};
          break;
        default:
      }
    },
  );

export default reducer;
