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 adminConfig from 'config/admin';
import actions from './actions';

interface SettingsState {
  email: {} | null;
  detail: {} | null;
  acInfo: {} | null;
  project: {} | null;
  instagram: {} | null;
}

export const initialState: SettingsState = {
  email: null,
  detail: null,
  acInfo: null,
  project: null,
  instagram: null,
};

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

  return newItem;
};

const adminSettings = adminConfig?.adminSettings;

const findResourceWithMethode = method => {
  for (let index = 0; index < Object.values(adminSettings).length; index++) {
    const element = Object.entries(adminSettings)[index];
    if (element[1]) {
      for (let i = 0; i < Object.values(element[1]).length; i++) {
        const el = Object.values(element[1])[i];
        if (el === method) return element[0];
      }
    }
  }
  return false;
};

const reducer = (state: SettingsState = initialState, action: StoreAction): SettingsState =>
  produce(
    state,
    (draft): SettingsState => {
      let resource;
      let key;
      switch (action.type) {
        // Load
        case actions.load.request.constant:
          if (action.payload.resource) handleRequest(draft, action, action.payload.resource, true);
          break;
        case actions.load.result.constant:
          key = Object.keys(action.payload);
          action.payload = action.payload[key[0]];
          resource = findResourceWithMethode(key[0]);
          handleResponse(draft, action, resource);
          break;
        // Load One
        case actions.loadOne.request.constant:
          handleRequest(draft, action, 'detail', true);
          break;
        case actions.loadOne.result.constant:
          handleResponse(draft, action, 'detail');
          break;
        // Delete
        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;
        // Other
        case actions.acFieldInfo.request.constant:
          handleRequest(draft, action, 'acInfo');
          break;
        case actions.acFieldInfo.result.constant:
          handleResponse(draft, action, 'acInfo');
          break;
        // Project Settings
        case actions.getProjectSettings.request.constant:
          handleRequest(draft, action, 'project');
          break;
        case actions.getProjectSettings.result.constant:
          handleResponse(draft, action, 'project');
          break;
        case actions.addProjectSettings.result.constant:
          if (action.payload && state.project.data) {
            draft.project.data.push(action.payload);
          }
          break;
        case actions.editProjectSettings.result.constant:
          if (action.payload && state.project.data) {
            const index = state.project.data.findIndex(e => e._id === action.payload._id);
            draft.project.data[index] = action.payload;
          }
          break;
        // Integrations
        case actions.integrations.instagram.post.request.constant:
          handleRequest(draft, action, 'instagram');
          break;
        case actions.integrations.instagram.post.result.constant:
          handleResponse(draft, action, 'instagram');
          break;
        case actions.integrations.instagram.crawl.result.constant:
          handleResponse(draft, action, 'instagram');
          break;
        case auth.actions.logout.result.constant:
          draft.email = null;
          break;
        default:
      }
    },
  );

export default reducer;
