/*
 *
 * Create & Edit reducer
 *
 */

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

const initialState = fromJS({
  createTemplateInProgress: false,
  response: {},
  editTemplateInProgress: false,
  editResponse: {},
  getTemplateDetailsInProgress: false,
  templateDetails: {},
  createTemplateErrorMessage: "",
});

export function mobilePushReducer(state = initialState, action) {
  // Handle null or undefined action
  if (!action || typeof action !== 'object') {
    return state;
  }

  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
        );
    case types.CREATE_TEMPLATE_FAILURE:
      return state
        .set("createTemplateInProgress", false)
        .set("createTemplateError", true)
        .set("createTemplateErrorMessage", fromJS(action.error));
    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.index !== undefined ? `uploadedAssetData${action.index}` : "uploadedAssetData", action.data);
    case types.UPLOAD_ASSET_FAILURE:
      return state
        .set("uploadAssetSuccess", false)
        .set("assetUploading", false);
    case types.CLEAR_ASSET:
      return action.index !== undefined 
        ? state.delete(`uploadedAssetData${action.index}`)
        : state.delete("uploadedAssetData");
    case types.GET_IOS_CTAS:
      return state.set("getIosCtasSucccess", false).set("iosCtasLoading", true);
    case types.GET_IOS_CTAS_SUCCESS:
      return state
        .set(
          "getIosCtasSucccess",
          action.statusCode !== undefined
            && action.statusCode !== ""
            && action.statusCode < 300
        )
        .set("iosCtasLoading", false)
        .set("iosCtasData", action.data);
    case types.GET_IOS_CTAS_FAILURE:
      return state
        .set("getIosCtasSucccess", false)
        .set("iosCtasLoading", false);
    case types.RESET_STORE:
      return state
        .delete("uploadedAssetData")
        .delete("uploadedAssetData0")
        .delete("uploadedAssetData1")
        .delete("iosCtasData");
    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:
    case types.SET_SELECTED_TEMPLATE:
      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("uploadedAssetData0")
        .delete("uploadedAssetData1")
        .delete("metaEntities")
        .delete("templateDetails")
        .delete("iosCtasData");
    case types.GET_WECRM_ACCOUNTS_REQUEST:
      return state
        .set("fetchingWeCrmAccounts", true)
        .set("weCrmAccountFetchingError", false);
    case types.GET_WECRM_ACCOUNTS_SUCCESS:
      return state
        .set("fetchingWeCrmAccounts", false)
        .set("weCrmAccounts", fromJS(action.data));
    case types.GET_WECRM_ACCOUNTS_FAILURE:
      return state
        .set("fetchingWeCrmAccounts", false)
        .set("weCrmAccountFetchingError", true);
    case types.SET_WECHAT_ACCOUNT:
      return state
        .set("selectedWeChatAccount", fromJS(action.weChatAccount))
        .delete("mobilepushTemplates");
    case types.GET_MOBILEPUSH_TEMPLATES_LIST_REQUEST:
      return state.set("fetchingDefaultTemplates", true);
    case types.GET_MOBILEPUSH_TEMPLATES_LIST_SUCCESS:
      return state
        .set("fetchingDefaultTemplates", false)
        .set("mobilepushTemplates", fromJS(action.data));
    case types.GET_MOBILEPUSH_TEMPLATES_LIST_FAILURE:
      return state
        .set("fetchingDefaultTemplates", false)
        .set("errorFetchingDefaultTempalte", true);
    default:
      return state;
  }
}

export default mobilePushReducer;
