import { fromJS } from 'immutable';
import {
  UPLOAD_RCS_ASSET_REQUEST,
  UPLOAD_RCS_ASSET_SUCCESS,
  UPLOAD_RCS_ASSET_FAILURE,
  CLEAR_RCS_ASSET,
  CLEAR_RCS_MEDIA_ASSET,
  CREATE_RCS_TEMPLATE_REQUEST,
  CREATE_RCS_TEMPLATE_SUCCESS,
  CREATE_RCS_TEMPLATE_FAILURE,
  CLEAR_CREATE_RESPONSE_REQUEST,
  GET_RCS_TEMPLATE_DETAILS_REQUEST,
  GET_RCS_TEMPLATE_DETAILS_SUCCESS,
  GET_RCS_TEMPLATE_DETAILS_FAILURE,
  EDIT_RCS_TEMPLATE_REQUEST,
  EDIT_RCS_TEMPLATE_SUCCESS,
  EDIT_RCS_TEMPLATE_FAILURE,
  CLEAR_EDIT_RESPONSE_REQUEST,
} from './constants';

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

function rcsReducer(state = initialState, action) {
  const { type, statusCode, data, errorMsg } = action;
  switch (type) {
    case UPLOAD_RCS_ASSET_REQUEST:
      return state.set('uploadAssetSuccess', false).set('assetUploading', true);
    case UPLOAD_RCS_ASSET_SUCCESS:
      return state
        .set(
          'uploadAssetSuccess',
          statusCode !== undefined && statusCode !== '' && statusCode < 300,
        )
        .set('assetUploading', false)
        .set(`uploadedAssetData${action.index || 0}`, data);
    case UPLOAD_RCS_ASSET_FAILURE:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false)
        .set('uploadAssetError', true);
    case CLEAR_RCS_ASSET:
      return state.delete('uploadedAssetData');
    case CLEAR_RCS_MEDIA_ASSET:
      return state.delete(`uploadedAssetData${action.index || 0}`);
    case CREATE_RCS_TEMPLATE_REQUEST:
      return state
        .set('createTemplateInProgress', true)
        .set('createTemplateError', false)
        .set('createTemplateErrorMessage', fromJS(''));
    case CREATE_RCS_TEMPLATE_SUCCESS:
      return state
        .set('createTemplateInProgress', false)
        .set('response', data)
        .set(
          'createTemplateError',
          statusCode !== undefined && statusCode > 300,
        )
        .set('createTemplateErrorMessage', fromJS(errorMsg));
    case CREATE_RCS_TEMPLATE_FAILURE:
      return state
        .set('createTemplateInProgress', false)
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS(errorMsg));
    case CLEAR_CREATE_RESPONSE_REQUEST:
      return state.set('response', {});
    // FOR EDIT
    case GET_RCS_TEMPLATE_DETAILS_REQUEST:
      return state.set('getTemplateDetailsInProgress', true);
    case GET_RCS_TEMPLATE_DETAILS_SUCCESS:
      return state
        .set('getTemplateDetailsInProgress', false)
        .set('templateDetails', data);
    case GET_RCS_TEMPLATE_DETAILS_FAILURE:
      return state.set('getTemplateDetailsInProgress', false);
    // FOR EDIT
    case EDIT_RCS_TEMPLATE_REQUEST:
      return state
        .set('editTemplateInProgress', true)
        .set('editTemplateError', false)
        .set('editTemplateErrorMessage', fromJS(''));
    case EDIT_RCS_TEMPLATE_SUCCESS:
      return state
        .set('editTemplateInProgress', false)
        .set('editResponse', data)
        .set('editTemplateError', statusCode !== undefined && statusCode > 300)
        .set('editTemplateErrorMessage', fromJS(errorMsg));
    case EDIT_RCS_TEMPLATE_FAILURE:
      return state
        .set('editTemplateInProgress', false)
        .set('editTemplateError', true)
        .set('editTemplateErrorMessage', fromJS(errorMsg));
    case CLEAR_EDIT_RESPONSE_REQUEST:
      return state.set('templateDetails', {});
    default:
      return state;
  }
}

export default rcsReducer;
