/*
 *
 * Faceboook reducer
 *
 */

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

const asyncUploadCases = createAsyncAssetUploadReducerCases({
  PROCESSING: types.UPLOAD_VIBER_ASSET_PROCESSING,
  COMPLETED: types.UPLOAD_VIBER_ASSET_COMPLETED,
  FAILED: types.UPLOAD_VIBER_ASSET_FAILED,
  TIMEOUT: types.UPLOAD_VIBER_ASSET_TIMEOUT,
});

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

function viberReducer(state = initialState, action) {
  switch (action.type) {
    case types.CREATE_VIBER_TEMPLATE_REQUEST:
      return state
      .set('createTemplateInProgress', true)
      .set('createTemplateError', false)
      .set('createTemplateErrorMessage', fromJS(""));
    case types.CREATE_VIBER_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_VIBER_TEMPLATE_FAILURE:
      return state
      .set('createTemplateInProgress', false)
      .set('createTemplateError', true)
      .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case types.UPLOAD_VIBER_ASSET_REQUEST:
      return state
      .set('uploadAssetSuccess', false)
      .set('assetUploading', true);
    case types.UPLOAD_VIBER_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', fromJS(action.data));
    case types.UPLOAD_VIBER_ASSET_FAILURE:
      return state
      .set('uploadAssetSuccess', false)
      .set('assetUploading', false);
    case asyncUploadCases.PROCESSING:
      return asyncUploadCases.handleProcessing(state, action);
    case asyncUploadCases.COMPLETED:
      return asyncUploadCases.handleCompleted(state, action);
    case asyncUploadCases.FAILED:
      return asyncUploadCases.handleFailed(state, action);
    case asyncUploadCases.TIMEOUT:
      return asyncUploadCases.handleTimeout(state, action);
    case types.CLEAR_VIBER_ASSET:
      return state
      .delete(action.templateType !== undefined ? `uploadedAssetData${action.templateType}` : 'uploadedAssetData')
        .set('assetProcessing', fromJS({}))
        .set('assetUploading', false)
        .set('uploadAssetSuccess', false);
    // FOR EDIT
    case types.EDIT_VIBER_TEMPLATE_REQUEST:
      return state
      .set('editTemplateInProgress', true)
      .set('editTemplateError', false)
      .set('editTemplateErrorMessage', fromJS(""));
    case types.EDIT_VIBER_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_VIBER_TEMPLATE_FAILURE:
      return state
        .set('editTemplateInProgress', false)
        .set('editTemplateError', true)
        .set('editTemplateErrorMessage', fromJS(action.errorMsg));
    case types.GET_VIBER_TEMPLATE_DETAILS_REQUEST:
      return state.set('getTemplateDetailsInProgress', true);
    case types.GET_VIBER_TEMPLATE_DETAILS_SUCCESS:
      return state.set('getTemplateDetailsInProgress', false)
        .set('templateDetails', action.data);
    case types.GET_VIBER_TEMPLATE_DETAILS_FAILURE:
    case types.CLEAR_VIBER_EDIT_RESPONSE_REQUEST:
      return state.set('editResponse', {});
    case types.CLEAR_VIBER_CREATE_RESPONSE_REQUEST:
      return state.set('response', {});
    case types.CLEAR_VIBER_DATA:
      return state
        .delete('uploadedAssetData')
        .delete('metaEntities')
        .delete('templateDetails')
        .set('assetProcessing', fromJS({}))
        .set('assetUploading', false)
        .set('uploadAssetSuccess', false);
    default:
      return state;
  }
}

export default viberReducer;
