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

// Best Seller 상품 목록 조회 IF-LGSP-303
export const getBestSeller = (callback) => (dispatch, getState) => {
  const onSuccess = (response) => {
    console.log("getBestSeller onSuccess", response.data);

    dispatch({
      type: types.GET_BEST_SELLER,
      payload: response.data.data,
    });
    dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));

    if (callback) {
      callback();
    }
  };

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

    if (callback) {
      callback();
    }
  };

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

// Detail 옵션상품 정보 조회 IF-LGSP-319
export const getProductGroup = (props) => (dispatch, getState) => {
  const { patnrId, prdtId } = props;

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

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

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

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_PRODUCT_GROUP,
    { patnrId, prdtId },
    {},
    onSuccess,
    onFail
  );
};

// Detail 옵션상품 정보 조회 IF-LGSP-320
export const getProductOption = (props) => (dispatch, getState) => {
  const { patnrId, prdtId } = props;

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

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

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

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_PRODUCT_OPTION,
    { patnrId, prdtId },
    {},
    onSuccess,
    onFail
  );
};
export const getProductOptionId = (id) => (dispatch) => {
  dispatch({ type: types.GET_PRODUCT_OPTION_ID, payload: id });
};
export const clearProductDetail = () => ({
  type: types.CLEAR_PRODUCT_DETAIL,
});

export const getProductImageLength =
  ({ imageLength }) =>
  (dispatch) => {
    dispatch({
      type: types.GET_PRODUCT_IMAGE_LENGTH,
      payload: imageLength,
    });
  };

export const getVideoIndicatorFocus = (focused) => (dispatch) => {
  dispatch({
    type: types.GET_VIDEO_INDECATOR_FOCUS,
    payload: focused,
  });
};
