/*
 *
 * Create reducer
 *
 */

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

const initialState = fromJS({
  getAllTemplatesInProgress: false,
  templates: [],
});

function createReducer(state = initialState, action) {
  switch (action.type) {
    case types.DEFAULT_ACTION:
      return state;
    case types.UPLOAD_ASSET_REQUEST:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', true);
    case types.UPLOAD_ASSET_SUCCESS:
      if (action.uploadId === 'image-value') {
        return state
        .set('uploadAssetSuccess', (action.statusCode !== undefined && action.statusCode !== '' && action.statusCode < 300))
        .set('assetUploading', false)
        .setIn(['uploadedAssetData', action.contentId], action.data);
      } else if (action.uploadId === 'content-image-value') {
        return state
        .set('uploadContentAssetSuccess', (action.statusCode !== undefined && action.statusCode !== '' && action.statusCode < 300))
        .set('assetUploading', false)
        .set('uploadedContentAssetData', action.data);
      }
      break;
    case types.UPLOAD_ASSET_FAILURE:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false);
    case types.CLEAR_CONTENT_ASSET:
      return state
        .delete('uploadedContentAssetData')
        .delete('uploadedAssetData');
    case types.SAVE_TEMPLATE_REQUEST:
      return state
        .set('saveTemplateErrorMessage', undefined)
        .set('savingTemplate', true);
    case types.SAVE_TEMPLATE_SUCCESS:
      return state
        .set('saveTemplateSuccess', (action.statusCode !== undefined && action.statusCode !== '' && action.statusCode < 300))
        .set('savingTemplate', false)
        .set('saveTemplateRes', action.data)
        .set('saveTemplateErrorMessage', action.errorMsg);
    case types.SAVE_TEMPLATE_FAILURE:
      return state
        .set('saveTemplateSuccess', false)
        .set('savingTemplate', false);
    case types.GET_ALL_TEMPLATES_REQUEST:
      return state
        .set('getAllTemplatesInProgress', true).set('templateError', {});
    case types.GET_ALL_TEMPLATES_SUCCESS:
      const { data, isReset } = action;
      if (isReset) {
        return state
          .set('getAllTemplatesInProgress', false)
          .set('pagination', true)
          .set('templates', data ? data.templates : [])
          .set('isSearch', data ? data.search : false);
      }
      return state
        .set('getAllTemplatesInProgress', false)
        .set('pagination', data ? Boolean(data.templates.length) : true)
        .set('templates', data ? unionBy(state.get('templates'), data.templates, '_id') : state.get('templates'))
        .set('isSearch', data ? data.search : false);
    case types.GET_ALL_TEMPLATES_FAILURE:
      return state.set('getAllTemplatesInProgress', false).set('templateError', action.error);
    default:
      return state;
  }
  return state;
}

export default createReducer;
