/*
 *
 * Email reducer
 *
 */

import { fromJS } from 'immutable';
import * as types from './constants';


const initialState = fromJS({
  createTemplateInProgress: false,
  createResponse: {},
  isBeeEnabled: null,
  fetchingCmsAccounts: false,
  cmsAccountsLoaded: false,
});

function emailReducer(state = initialState, action) {
  //
  switch (action.type) {
    case types.CREATE_TEMPLATE_REQUEST:
      return state
        .set('createTemplateInProgress', true)
        .set('createTemplateError', false)
        .set('createTemplateErrorMessage', fromJS(""));
    case types.CREATE_TEMPLATE_SUCCESS:
      return state
        .set('createTemplateInProgress', false)
        .set('createResponse', action.data)
        .set('createTemplateErrorMessage', fromJS(action.errorMsg))
        .set('createTemplateError', (action.statusCode !== undefined && action.statusCode > 300));
    case types.CREATE_TEMPLATE_FAILURE:
      return state
        .set('createTemplateInProgress', false)
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case types.DUPLICATE_TEMPLATE_REQUEST:
      return state
        .set('duplicateTemplateInProgress', true)
        .set('duplicateTemplateError', false)
        .set('duplicateTemplateErrorMessage', fromJS(""));
    case types.DUPLICATE_TEMPLATE_SUCCESS:
      return state
        .set('duplicateTemplateInProgress', false)
        .set('duplicateResponse', action.data)
        .set('duplicateTemplateErrorMessage', fromJS(action.errorMsg))
        .set('duplicateTemplateError', (action.statusCode !== undefined && action.statusCode > 300));
    case types.DUPLICATE_TEMPLATE_FAILURE:
      return state
        .set('duplicateTemplateInProgress', false)
        .set('duplicateTemplateError', true)
        .set('duplicateTemplateErrorMessage', fromJS(action.errorMsg));
    case types.GET_TEMPLATE_DETAILS_REQUEST:
      return state
        .set('getTemplateDetailsInProgress', true);
    case types.GET_TEMPLATE_DETAILS_SUCCESS:
      return state
        .set('getTemplateDetailsInProgress', false)
        .set('templateDetails', fromJS(action.data));
    case types.GET_TEMPLATE_DETAILS_FAILURE:
      return state
        .set('getTemplateDetailsInProgress', false);
    case types.GET_ALL_ASSETS_REQUEST:
      return state
        .set('fetchingAllAssets', true)
        .set('fetchAllAssetSuccess', false);
    case types.GET_ALL_ASSETS_SUCCESS:
      if (action.isReset) {
        return state
          .set('assetList', fromJS(action.data.assets))
          .set('fetchingAllAssets', false)
          .set('fetchAllAssetSuccess', true);
      }
      return state
        .set('assetList', fromJS(state.get('assetList').toJS().concat(action.data.assets)))
        .set('fetchingAllAssets', false)
        .set('fetchAllAssetSuccess', true);
    case types.GET_ALL_ASSETS_FAILURE:
      return state
        .set('fetchingAllAssets', false)
        .set('fetchAllAssetSuccess', false);
    case types.GET_CMS_EDITOR_DETAILS_REQUEST:
      return state
        .set('fetchingCmsSettings', true)
        .set('fetchingCmsSettingsError', false);
    case types.GET_CMS_EDITOR_DETAILS_SUCCESS:
      return state
        .set('fetchingCmsSettings', false)
        .set('isBeeEnabled', action.isBeeEnabled)
        .set('CmsSettings', fromJS(action.settings));
    case types.GET_CMS_EDITOR_DETAILS_FAILURE:
      return state
        .set('fetchingCmsSettings', false)
        .set('fetchingCmsSettingsError', true);
    case types.GET_CMS_EDITOR_DATA_REQUEST:
      return state
        .set('fetchingCmsData', true)
        .set('cmsData', '')
        .set('langId', undefined)
        .set('fetchingCmsDataFailed', false);
    case types.RESET_CMS_EDITOR_DATA:
      return state
        .set('cmsData', '');
    case types.GET_CMS_EDITOR_DATA_SUCCESS:
      return state
        .set('fetchingCmsData', false)
        .set('cmsData', action.data)
        .set('langId', action.langId);
    case types.GET_CMS_EDITOR_DATA_FAILURE:
      return state
        .set('fetchingCmsData', false)
        .set('fetchingCmsDataFailed', true);
    case types.GET_CMS_ACCOUNTS_REQUEST:
      return state
        .set('isBeeEnabled', false)
        .set('fetchingCmsAccounts', true)
        .set('cmsAccountsLoaded', false);
    case types.GET_CMS_ACCOUNTS_SUCCESS:
      return state
        .set('isBeeEnabled', action.isBeeEnabled)
        .set('fetchingCmsAccounts', false)
        .set('cmsAccountsLoaded', true);
    case types.GET_CMS_ACCOUNTS_FAILURE:
      return state
        .set('isBeeEnabled', false)
        .set('fetchingCmsAccounts', false)
        .set('cmsAccountsLoaded', true);
    case types.CLEAR_EMAIL_CRUD_RESPONSE_REQUEST:
      return state
        .set('createResponse', fromJS({}));
    case types.UPLOAD_ASSET_REQUEST:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', true);
    case types.UPLOAD_ASSET_SUCCESS:
      return state
      .set('uploadAssetSuccess', (action.statusCode !== undefined && action.statusCode !== '' && action.statusCode < 300))
      .set('assetUploading', false)
      .set('uploadedAssetData', action.data);
    case types.UPLOAD_ASSET_FAILURE:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false);

    case types.CLEAR_ALL_VALUES:
      return state
        .set('createTemplateInProgress', false)
        .set('createResponse', fromJS({}))
        .set('createTemplateError', false)
        .set('getTemplateDetailsInProgress', false)
        .set('templateDetails', fromJS([]))
        .set('fetchingAllAssets', false)
        .set('fetchAllAssetSuccess', false)
        .set('assetList', fromJS([]))
        .set('fetchingCmsSettings', false)
        .set('fetchingCmsSettingsError', false)
        .set('CmsSettings', fromJS({}))
        .set('fetchingCmsData', false)
        .set('duplicateResponse', fromJS({}))
        .set('cmsData', '')
        .set('isBeeEnabled', null);
    case types.TRANSFORM_EMAIL_TEMPLATE_REQUEST:
      return state.set("createTemplateInProgress", true);
    default:
      return state;
  }
}

export default emailReducer;
