import { URLS } from "../api/apiConfig";
import { TAxios } from "../api/TAxios";
import { types } from "./actionTypes";

// 이벤트 정보 조회 IF-LGSP-070
export const getWelcomeEventInfo =
  (onSuccessCallback, onFailCallback) => (dispatch, getState) => {
    const onSuccess = (response) => {
      console.log("getWelcomeEventInfo onSuccess ", response.data);

      dispatch({
        type: types.GET_WELCOME_EVENT_INFO,
        payload: response.data.data,
        retCode: response.data.retCode,
      });

      if (onSuccessCallback) {
        onSuccessCallback(response.data);
      }
    };

    const onFail = (error) => {
      console.error("getWelcomeEventInfo onFail ", error);
      if (onFailCallback) {
        onFailCallback(error);
      }
    };

    TAxios(
      dispatch,
      getState,
      "get",
      URLS.GET_WELCOME_EVENT_INFO,
      {},
      {},
      onSuccess,
      onFail
    );
  };

// 이벤트(쿠폰) 지급 요청 (IF-LGSP-071)
export const setEventIssueReq = (params) => (dispatch, getState) => {
  const { evntTpCd, evntId, mbphNo, cntryCd } = params;
  const onSuccess = (response) => {
    console.log("setEventIssueReq onSuccess ", response.data);

    dispatch({
      type: types.SET_EVENT_ISSUE_REQ,
      payload: response.data.data,
      retCode: response.data.retCode,
    });
  };

  const onFail = (error) => {
    console.error("setEventIssueReq onFail ", error);
  };

  TAxios(
    dispatch,
    getState,
    "post",
    URLS.SET_EVENT_ISSUE_REQ,
    {},
    { evntTpCd, evntId, mbphNo, cntryCd },
    onSuccess,
    onFail
  );
};

// 이벤트 지급 대상 여부 조회 (IF-LGSP-073)
export const getEventIssuedStaus = (params) => (dispatch, getState) => {
  const { evntTpCd, evntId } = params;
  const onSuccess = (response) => {
    console.log("getEventIssuedStaus onSuccess ", response.data);

    dispatch({
      type: types.GET_EVENT_ISSUED_STATUS,
      payload: response.data.data,
      retCode: response.data.retCode,
    });
  };

  const onFail = (error) => {
    console.error("getEventIssuedStaus onFail ", error);
  };

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_EVENT_ISSUED_STATUS,
    { evntTpCd, evntId },
    {},
    onSuccess,
    onFail
  );
};

//  이벤트 팝업 클릭 저장 (IF-LGSP-084)
export const setEventPopClickInfo = (params) => (dispatch, getState) => {
  const { evntApplcnFlag, evntId } = params;

  const onSuccess = (response) => {
    console.log("setEventPopClickInfo onSuccess ", response.data);

    dispatch({
      type: types.SET_EVENT_POP_CLICK_INFO,
      payload: response.data.data,
      retCode: response.data.retCode,
    });

    dispatch(clearEventInfo());
  };

  const onFail = (error) => {
    console.error("setEventPopClickInfo onFail ", error);
  };

  TAxios(
    dispatch,
    getState,
    "post",
    URLS.SET_EVENT_POP_CLICK_INFO,
    {},
    { evntApplcnFlag, evntId },
    onSuccess,
    onFail
  );
};

export const skipClickInfo = (status) => ({
  type: types.SKIP_CLICK_INFO,
  payload: status + 1,
});

export const clearEventInfo = () => ({
  type: types.CLEAR_EVENT_INFO,
});

export const clearCurationCoupon = () => ({
  type: types.CLEAR_CURATION_COUPON,
});

export const clearEventIssuedStatusSuccess = () => ({
  type: types.CLEAR_EVENT_ISSUED_STATUS_SUCCEESS,
});
