/**
 * Created by vivek on 22/5/17.
 */
import { fromJS } from 'immutable';
import _ from 'lodash';
import * as types from './constants';
import initialState from '../../initialState';
import { FAILURE } from '../App/constants';

function capReducer(state = fromJS(initialState.cap), action) {
  switch (action.type) {
    case types.LOGIN_REQUEST:
      return state.set('login_progress', true);
    case types.LOGIN_SUCCESS:
      return state
        .set('login_progress', false)
        .set('token', action.res.token)
        .set('user', fromJS(action.res.user))
        .set('message', '')
        .set('authenticated', true)
        .set('isLoggedIn', true);
    case types.LOGIN_FAILURE:
      return state
        .set('login_progress', false)
        .set('message', action.error.message);
    case types.LOGOUT_REQUEST_V2:
      return state.set('login_progress', false);
    case types.LOGOUT_SUCCESS_V2:
      return state;
    case types.LOGOUT_FAILURE_V2:
      return state;
    case types.SWITCH_ORG_REQUEST:
      return state
        .set('settingProxyOrg', true)
        .set('changeProxyOrgSuccess', false);
    case types.SWITCH_ORG_SUCCESS:
      return state
        .set('orgID', action.orgID)
        .set('settingProxyOrg', false)
        .set('changeProxyOrgSuccess', action.isSuccess);
    case types.SWITCH_ORG_FAILURE:
      return state
        .set('settingProxyOrg', false)
        .set('changeProxyOrgSuccess', false);
    case types.ADD_MESSAGE: {
      const message = {
        messageId: action.message.id,
        title: action.message.title,
        text: action.message.text,
        type: action.message.type,
        hideProgressBar: action.message.hideProgressBar,
      };
      return state
      .set('messages', fromJS(state.get('messages').concat(message)));
    }
    case types.REMOVE_MESSAGE: {
      const messageQueue = state.get('messages').toJS();
      const messageIndex = _.findIndex(messageQueue, { messageId: action.messageIndex });
      messageQueue.splice(messageIndex, 1);
      return state
      .set('messages', fromJS(messageQueue));
    }
    case types.GET_USER_DATA_REQUEST:
      return state
        .set('fetching_userdata', true)
        .set('isLoggedIn', false);
    case types.GET_USER_DATA_SUCCESS:
      return state
        .set('fetching_userdata', false)
        .set('user', fromJS(action.userData))
        .set('currentOrgDetails', fromJS(action.currentOrgDetails))
        .set('isLoggedIn', true)
        .set('orgID', action.currentOrgId);
    case types.GET_USER_DATA_FAILURE:
      return state
        .set('fetching_userdata', false)
        .set('isLoggedIn', false)
        .set('getUserDataStatus', FAILURE)
        .set('getUserDataCode', action.status);
    case types.GET_SCHEMA_FOR_ENTITY_REQUEST:
      return state
        .set('fetchingSchema', true)
        .set('fetchingLiquidTags', false)
        .set('fetchingSchemaError', false);
    case types.GET_SCHEMA_FOR_ENTITY_FAILURE:
      return state
        .set('fetchingSchema', false)
        .set('fetchingSchemaError', true);
    case types.GET_LIQUID_TAGS_REQUEST:
      return state
        .set('fetchingLiquidTags', true);
    case types.GET_LIQUID_TAGS_FAILURE:
      return state
        .set('fetchingLiquidTags', false);
    case types.GET_LIQUID_TAGS_SUCCESS:
      return state
        .set('fetchingLiquidTags', false);
    case types.GET_SCHEMA_FOR_ENTITY_SUCCESS: {
      const stateMeta = state.get("metaEntities");
      return state
        .set('fetchingSchema', false)
        .set('fetchingLiquidTags', false)
        .set('metaEntities', {
          layouts: action.data && action.entityType === 'LAYOUT' ? action.data.metaEntities : stateMeta?.layouts,
          tags: action.data && action.entityType === 'TAG' ? action.data.metaEntities : stateMeta?.tags,
        })
        .set('fetchingSchemaError', false);
    }
    case types.CLEAR_META_ENTITIES:
      return state.set('metaEntities', {
        layouts: [],
        tags: [],
      });
    // eslint-disable-next-line no-case-declarations
    case types.HIDE_TAGS:
      const metaEntities = {tags: {}, layouts: state.get('metaEntities').layouts};
      metaEntities.tags.standard = _.filter(state.get('metaEntities').tags.standard, (tag) => action.tagList.indexOf(tag.definition.value) === -1);
      metaEntities.tags.custom = _.filter(state.get('metaEntities').tags.custom, (tag) => action.tagList.indexOf(tag.name) === -1);
      return state.setIn(['metaEntities'], metaEntities);
    case types.SET_INJECTED_TAGS:
      return state.set("injectedTags", action.injectedTags);
    case types.GET_TOPBAR_MENU_DATA_REQUEST:
      return state.set('topbarMenuData', fromJS({ status: 'request' }));
    case types.GET_TOPBAR_MENU_DATA_SUCCESS:
      return state.set(
          'topbarMenuData',
          fromJS({ status: 'success', data: action.data }),
        );
    case types.GET_TOPBAR_MENU_DATA_FAILURE:
      return state.set(
          'topbarMenuData',
          fromJS({ status: 'failure', error: action.error }),
        );
    case types.CLEAR_TOPBAR_MENU_DATA:
      return state.set('topbarMenuData', fromJS({}));
    case types.SET_LOYALTY_PROMOTION_RELATED_CREATIVE_TAGS_DISPLAY: {
      return state.set('disableLoyaltyPromotionRelatedTags', action.disable)
        .set('tagsToDisable', action.tagsToDisable)
        .set('parentTagsToDisable', action.parentTagsToDisable);
    }
    case types.GET_SUPPORT_VIDEOS_CONFIG_REQUEST:
      return state
        .set('demoVideoAndLinkJSONStatus', types.REQUEST)
        .set('demoVideoAndLinkJSON', {});
    case types.GET_SUPPORT_VIDEOS_CONFIG_SUCCESS:
      return state
        .set('demoVideoAndLinkJSONStatus', types.SUCCESS)
        .set('demoVideoAndLinkJSON', action?.result);
    case types.GET_SUPPORT_VIDEOS_CONFIG_FAILURE:
      return state
        .set('demoVideoAndLinkJSONStatus', types.FAILURE)
        .set('demoVideoAndLinkJSONError', action?.error);
    case types.CREATE_CENTRAL_COMMS_META_ID_REQUEST:
      return state.set('metaDataStatus', types.REQUEST);
    case types.CREATE_CENTRAL_COMMS_META_ID_SUCCESS:
      return state.set('metaDataStatus', types.SUCCESS);
    case types.CREATE_CENTRAL_COMMS_META_ID_FAILURE:
      return state.set('metaDataStatus', types.FAILURE);
    default:
      return state;
  }
}

export default capReducer;
