/*
 *
 * Create reducer
 *
 */

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

const initialState = fromJS({
  createTemplateInProgress: false,
  response: {},
  uploadedAssetData: {},
});

function createReducer(state = initialState, action) {
  switch (action.type) {
    case types.DEFAULT_ACTION:
      return state;
    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('response', action.data)
        .set('createTemplateError', (action.statusCode !== undefined && action.statusCode > 300))
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case types.CREATE_TEMPLATE_FAILURE:
      return state
        .set('createTemplateInProgress', false)
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case types.CLEAR_CREATE_RESPONSE_REQUEST:
      return state.set('response', {});
    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(action.templateType !== undefined ? `uploadedAssetData${action.templateType}` : 'uploadedAssetData', action.data);
    case types.UPLOAD_ASSET_FAILURE:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false);
    case types.CLEAR_ASSET:
      return state
        .delete(action.templateType !== undefined ? `uploadedAssetData${action.templateType}` : 'uploadedAssetData');
      // FOR EDIT
    case types.EDIT_TEMPLATE_REQUEST:
      return state
        .set('editTemplateInProgress', true)
        .set('editTemplateError', false)
        .set('editTemplateErrorMessage', fromJS(""));
    case types.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 types.EDIT_TEMPLATE_FAILURE:
      return state
        .set('editTemplateInProgress', false)
        .set('editTemplateError', true)
        .set('editTemplateErrorMessage', 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', action.data);
    case types.GET_TEMPLATE_DETAILS_FAILURE:
      return state.set('getTemplateDetailsInProgress', false);
    case types.CLEAR_EDIT_RESPONSE_REQUEST:
      return state.set('editResponse', {});
    case types.CLEAR_DATA:
      return state
        .delete('uploadedAssetData')
        .delete('metaEntities')
        .delete('templateDetails');
    case types.GET_LINE_TEMPLATES_LIST_REQUEST:
      return state
        .set('fetchingDefaultTemplates', true);
    case types.GET_LINE_TEMPLATES_LIST_SUCCESS:
      return state
        .set('fetchingDefaultTemplates', false)
        .set('lineTemplates', fromJS(action.data));
    case types.GET_LINE_TEMPLATES_LIST_FAILURE:
      return state
        .set('fetchingDefaultTemplates', false)
        .set('errorFetchingDefaultTempalte', true);
    case types.GET_STICKER_RESPONSE:
      return state
        .set('lineStickers', action.result)
        .set('lineStickersError', action.error)
    default:
      return state;
  }
}

export default createReducer;
