import { fromJS } from 'immutable';
import {
  INAPP_TEMPLATE_CREATE_REQUEST,
  INAPP_TEMPLATE_CREATE_SUCCESS,
  INAPP_TEMPLATE_CREATE_FAILURE,
  CLEAR_CREATE_RESPONSE_REQUEST,
  GET_INAPP_TEMPLATE_DETAILS_REQUEST,
  GET_INAPP_TEMPLATE_DETAILS_SUCCESS,
  GET_INAPP_TEMPLATE_DETAILS_FAILURE,
  RESET_EDIT_TEMPLATE,
  UPLOAD_INAPP_ASSET_REQUEST,
  UPLOAD_INAPP_ASSET_SUCCESS,
  UPLOAD_INAPP_ASSET_FAILURE,
  CLEAR_INAPP_ASSET,
  INAPP_EDIT_TEMPLATE_REQUEST,
  INAPP_EDIT_TEMPLATE_SUCCESS,
  INAPP_EDIT_TEMPLATE_FAILURE,
  GET_BEE_POPUP_BUILDER_TOKEN_REQUEST,
  GET_BEE_POPUP_BUILDER_TOKEN_SUCCESS,
  GET_BEE_POPUP_BUILDER_TOKEN_FAILURE,
} from './constants';

const initialState = fromJS({
  uploadedAssetData: {},
  createTemplateInProgress: false,
  editTemplateInProgress: false,
  beePopupBuilderTokenFetching: false,
  beePopupBuilderToken: {},
  beePopupBuilderTokenError: false,
  beePopupBuilderTokenErrorMessage: '',
});

function inAppReducer(state = initialState, action) {
  switch (action.type) {
    case INAPP_TEMPLATE_CREATE_REQUEST:
      return state
        .set('createTemplateInProgress', true)
        .set('createTemplateError', false)
        .set('createTemplateErrorMessage', fromJS(''));
    case INAPP_TEMPLATE_CREATE_SUCCESS:
      return state
        .set('createTemplateInProgress', false)
        .set('response', action.data)
        .set(
          'createTemplateError',
          action.statusCode !== undefined && action.statusCode > 300,
        )
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case INAPP_TEMPLATE_CREATE_FAILURE:
      return state
        .set('createTemplateInProgress', false)
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    // FOR EDIT
    case INAPP_EDIT_TEMPLATE_REQUEST:
      return state
        .set('editTemplateInProgress', true)
        .set('editTemplateError', false)
        .set('editTemplateErrorMessage', fromJS(''));
    case INAPP_EDIT_TEMPLATE_SUCCESS:
      return state
        .set('editTemplateInProgress', false)
        .set('editResponse', action.data)
        .set('editTemplateError', action.statusCode !== undefined && action.statusCode > 300)
        .set('editTemplateErrorMessage', fromJS(action.errorMsg));
    case INAPP_EDIT_TEMPLATE_FAILURE:
      return state
        .set('editTemplateInProgress', false)
        .set('editTemplateError', true)
        .set('editTemplateErrorMessage', fromJS(action.errorMsg));
    case CLEAR_CREATE_RESPONSE_REQUEST:
      return state.set('response', {});
    // FOR EDIT
    case GET_INAPP_TEMPLATE_DETAILS_REQUEST:
      return state.set('getTemplateDetailsInProgress', true);
    case GET_INAPP_TEMPLATE_DETAILS_SUCCESS:
      return state
        .set('getTemplateDetailsInProgress', false)
        .set('templateDetails', action.data);
    case GET_INAPP_TEMPLATE_DETAILS_FAILURE:
      return state.set('getTemplateDetailsInProgress', false);
    case RESET_EDIT_TEMPLATE:
      return state.set('templateDetails', {});
    case UPLOAD_INAPP_ASSET_REQUEST:
      return state.set('uploadAssetSuccess', false).set('assetUploading', true);
    case UPLOAD_INAPP_ASSET_SUCCESS:
      return state
        .set(
          'uploadAssetSuccess',
          action.statusCode !== undefined
            && action.statusCode !== ''
            && action.statusCode < 300,
        )
        .set('assetUploading', false)
        .set(
          action.templateType !== undefined
            ? `uploadedAssetData${action.templateType}`
            : 'uploadedAssetData',
          action.data,
        );
    case UPLOAD_INAPP_ASSET_FAILURE:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false);
    case CLEAR_INAPP_ASSET:
      return state.delete(
        action.templateType !== undefined
          ? `uploadedAssetData${action.templateType}`
          : 'uploadedAssetData',
      );
    case GET_BEE_POPUP_BUILDER_TOKEN_REQUEST:
      return state.set('beePopupBuilderTokenFetching', true);
    case GET_BEE_POPUP_BUILDER_TOKEN_SUCCESS:
      return state
        .set('beePopupBuilderTokenFetching', false)
        .set('beePopupBuilderToken', action.data);
    case GET_BEE_POPUP_BUILDER_TOKEN_FAILURE:
      return state
        .set('beePopupBuilderTokenFetching', false)
        .set('beePopupBuilderTokenError', true)
        .set('beePopupBuilderTokenErrorMessage', fromJS(action.error));
    default:
      return state;
  }
}

export default inAppReducer;
