/*
 *
 * Faceboook reducer
 *
 */

import { fromJS } from 'immutable';
import {
  SET_MARKETING_OBJECTIVES_SUCCESS,
  SET_MARKETING_OBJECTIVES_FAILURE,
  UPLOAD_FB_AD_ASSET_REQUEST,
  UPLOAD_FB_AD_ASSET_SUCCESS,
  UPLOAD_FB_AD_ASSET_FAILURE,
  CREATE_FACEBOOK_AD_TEMPLATE_REQUEST,
  CREATE_FACEBOOK_AD_TEMPLATE_SUCCESS,
  CREATE_FACEBOOK_AD_TEMPLATE_FAILURE,
  CLEAR_FACEBOOK_AD_ASSET,
  EDIT_FACEBOOK_AD_TEMPLATE_REQUEST,
  EDIT_FACEBOOK_AD_TEMPLATE_SUCCESS,
  EDIT_FACEBOOK_AD_TEMPLATE_FAILURE,
  GET_FACEBOOK_AD_TEMPLATE_DETAILS_REQUEST,
  GET_FACEBOOK_AD_TEMPLATE_DETAILS_SUCCESS,
  GET_FACEBOOK_AD_TEMPLATE_DETAILS_FAILURE,
  CLEAR_FACEBOOK_AD_EDIT_RESPONSE_REQUEST,
  CLEAR_FACEBOOK_AD_CREATE_RESPONSE_REQUEST,
  CLEAR_FACEBOOK_AD_DATA,
  GET_ORG_OU_LIST_REQUEST,
  GET_ORG_OU_LIST_FAILURE,
  GET_ORG_OU_LIST_SUCCESS,
} from './constants';

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

function facebookReducer(state = initialState, action) {
  const { type, payload, templateType} = action;
  switch (type) {
    case SET_MARKETING_OBJECTIVES_SUCCESS: {
      return state.set('marketinObjectivesData', payload).set('marketinObjectivesError', null);
    }
    case SET_MARKETING_OBJECTIVES_FAILURE: {
      return state.set('marketinObjectivesData', []).set('marketinObjectivesError', payload);
    }
    case UPLOAD_FB_AD_ASSET_REQUEST:
      return state
              .set('uploadAssetSuccess', false)
              .set(templateType !== undefined ? `uploadAssetError${templateType}` : 'uploadAssetError', false, )
              .set(templateType !== undefined ? `assetUploading${templateType}` : 'assetUploading', true);
    case UPLOAD_FB_AD_ASSET_SUCCESS:
      return state
              .set('uploadAssetSuccess', (action.statusCode !== undefined && action.statusCode !== '' && action.statusCode < 300))
              .set(templateType !== undefined ? `uploadAssetError${templateType}` : 'uploadAssetError', false, )
              .set(templateType !== undefined ? `assetUploading${templateType}` : 'assetUploading', false)
              .set(templateType !== undefined ? `uploadedAssetData${templateType}` : 'uploadedAssetData', action.data);
    case UPLOAD_FB_AD_ASSET_FAILURE:
      return state
              .set('uploadAssetSuccess', false)
              .set(templateType !== undefined ? `uploadAssetError${templateType}` : 'uploadAssetError', true, )
              .set(templateType !== undefined ? `assetUploading${templateType}` : 'assetUploading', false);
    case CREATE_FACEBOOK_AD_TEMPLATE_REQUEST:
      return state
              .set('createTemplateInProgress', true)
              .set('createTemplateError', false)
              .set('createTemplateErrorMessage', fromJS(""));
    case CREATE_FACEBOOK_AD_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 CREATE_FACEBOOK_AD_TEMPLATE_FAILURE:
      return state
              .set('createTemplateInProgress', false)
              .set('createTemplateError', true)
              .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case CLEAR_FACEBOOK_AD_ASSET:
      return state
                .delete(templateType !== undefined ? `uploadedAssetData${templateType}` : 'uploadedAssetData');
            // FOR EDIT
    case EDIT_FACEBOOK_AD_TEMPLATE_REQUEST:
      return state
              .set('editTemplateInProgress', true)
              .set('editTemplateError', false)
              .set('editTemplateErrorMessage', fromJS(""));
    case EDIT_FACEBOOK_AD_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 EDIT_FACEBOOK_AD_TEMPLATE_FAILURE:
      return state
                .set('editTemplateInProgress', false)
                .set('editTemplateError', true)
                .set('editTemplateErrorMessage', fromJS(action.errorMsg));
    case GET_FACEBOOK_AD_TEMPLATE_DETAILS_REQUEST:
      return state.set('getTemplateDetailsInProgress', true);
    case GET_FACEBOOK_AD_TEMPLATE_DETAILS_SUCCESS:
      return state.set('getTemplateDetailsInProgress', false)
                .set('templateDetails', action.data);
    case GET_FACEBOOK_AD_TEMPLATE_DETAILS_FAILURE:
    case CLEAR_FACEBOOK_AD_EDIT_RESPONSE_REQUEST:
      return state.set('editResponse', {});
    case CLEAR_FACEBOOK_AD_CREATE_RESPONSE_REQUEST:
      return state.set('response', {});
    case CLEAR_FACEBOOK_AD_DATA:
      return state
                .delete('uploadedAssetData')
                .delete('metaEntities')
                .delete('templateDetails');
    case GET_ORG_OU_LIST_REQUEST: {
      return state.set('orgOuData', fromJS({ status: 'REQUEST' }));
    }
    case GET_ORG_OU_LIST_SUCCESS: {
      return state.set(
        'orgOuData',
        fromJS({ status: 'SUCCESS', data: action.payload }),
      );
    }
    case GET_ORG_OU_LIST_FAILURE: {
      return state.set('orgOuData', fromJS({ status: 'FAILURE' }));
    }
    default:
      return state;
  }
}

export default facebookReducer;
