import { URLS } from "../api/apiConfig";
import { TAxios } from "../api/TAxios";
import { types } from "./actionTypes";
import { changeAppStatus, showError } from "./commonActions";

// 회원 체크아웃 정보 조회 IF-LGSP-345
export const getMyInfoCheckoutInfo =
  (props, callback) => (dispatch, getState) => {
    const { mbrNo, dirPurcSelYn, cartList } = props;

    dispatch(
      // changeAppStatus({ showLoadingPanel: { show: true, type: "wait" } })
      changeAppStatus({ isLoading: true })
    );

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

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

      if (callback) callback(response.data);
    };

    const onFail = (error) => {
      console.error("getMyInfoCheckoutInfo OnFail: ", error);
    };

    TAxios(
      dispatch,
      getState,
      "post",
      URLS.GET_CHECKOUT_INFO,
      {},
      { mbrNo, dirPurcSelYn, cartList },
      onSuccess,
      onFail
    );
  };

// 회원 CheckOut 상품 주문 IF-LGSP-346
export const insertMyInfoCheckoutOrder =
  (props, callback) => (dispatch, getState) => {
    const {
      mbrNo,
      bilAddrSno,
      dlvrAddrSno,
      pinCd,
      orderProductCoupontUse,
      orderProductQtyInfo,
    } = props;

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

      if (response.data.retCode === 0) {
        dispatch({
          type: types.INSERT_MY_INFO_CHECKOUT_ORDER,
          payload: response.data.data,
        });

        if (callback) callback(response);
      } else {
        dispatch(
          showError(
            response.data.retCode,
            response.data.retMsg,
            true,
            response.data.retDetailCode
          )
        );
      }

      dispatch(
        changeAppStatus({
          showLoadingPanel: { show: false, showMessage: false },
        })
      );
    };

    const onFail = (error) => {
      console.error("insertMyInfoCheckoutOrder onFail: ", error);
      dispatch(
        changeAppStatus({
          showLoadingPanel: { show: false, showMessage: false },
        })
      );
    };

    TAxios(
      dispatch,
      getState,
      "post",
      URLS.INSERT_MY_INFO_CHECKOUT_ORDER,
      {},
      {
        mbrNo,
        bilAddrSno,
        dlvrAddrSno,
        pinCd,
        orderProductCoupontUse,
        orderProductQtyInfo,
      },
      onSuccess,
      onFail
    );
  };

export const getCheckoutTotalAmt =
  (params, callback) => (dispatch, getState) => {
    const {
      mbrNo,
      dirPurcSelYn,
      bilAddrSno,
      dlvrAddrSno,
      orderProductCoupontUse,
    } = params;

    dispatch(changeAppStatus({ isLoading: false }));

    dispatch(
      changeAppStatus({ showLoadingPanel: { show: true, type: "wait" } })
    );

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

      if (response.data.retCode === 0) {
        dispatch({
          type: types.GET_CHECKOUT_TOTAL_AMT,
          payload: response.data.data,
        });

        if (callback) callback(response.data);
      } else {
        dispatch(
          showError(
            response.data.retCode,
            response.data.retMsg,
            true,
            response.data.retDetailCode
          )
        );
      }

      dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));
    };

    const onFail = (error) => {
      console.error("getCheckoutTotalAmt onFail: ", error);

      dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));
    };

    TAxios(
      dispatch,
      getState,
      "post",
      URLS.GET_CHECKOUT_TOTAL_AMT,
      {},
      { mbrNo, dirPurcSelYn, bilAddrSno, dlvrAddrSno, orderProductCoupontUse },
      onSuccess,
      onFail
    );
  };

export const updateSelectedShippingAddr = (dlvrAddrSno) => ({
  type: types.UPDATE_SELECTED_SHIPPING_ADDR,
  payload: dlvrAddrSno,
});

export const updateSelectedBillingAddr = (bilAddrSno) => ({
  type: types.UPDATE_SELECTED_BILLING_ADDR,
  payload: bilAddrSno,
});

export const updateSelectedCoupon = (productId, coupon) => ({
  type: types.UPDATE_SELECTED_COUPON,
  payload: { productId, coupon },
});

export const resetCheckoutData = () => ({
  type: types.CHECKOUT_DATA_RESET,
});
