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

// Featured Brands 정보 조회 IF-LGSP-304
export const getBrandList = () => (dispatch, getState) => {
  dispatch(changeAppStatus({ showLoadingPanel: { show: true, type: "wait" } }));

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

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

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

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

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

// Featured Brands LAYOUT (shelf) 정보 조회 IF-LGSP-305
export const getBrandLayoutInfo = (props) => (dispatch, getState) => {
  const { patnrId } = props;

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

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

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

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

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

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

// Featured Brands Live 채널 정보 조회 IF-LGSP-306
export const getBrandLiveChannelInfo = (props) => (dispatch, getState) => {
  const { patnrId } = props;

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

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

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

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

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

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

export const getBrandChanInfo = (props) => (dispatch, getState) => {
  const { patnrId } = props;

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

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

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

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

// Featured Brands Today's Deals 정보 조회 IF-LGSP-307
export const getBrandTSVInfo = (props) => (dispatch, getState) => {
  const { patnrId } = props;

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

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

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

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

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

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

// Featured Brand Recommended Show 정보 조회 IF-LGSP-308
export const getBrandRecommendedShowInfo = (props) => (dispatch, getState) => {
  const { catCd, patnrId } = props;

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

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

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

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

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

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

// Featured Brands Featured Creators 정보 조회 IF-LGSP-309
export const getBrandCreatorsInfo = (props) => (dispatch, getState) => {
  const { hstNm, patnrId } = props;

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

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

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

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

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

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

// Featured Brands Series 정보 조회 IF-LGSP-310
export const getBrandSeriesInfo = (props) => (dispatch, getState) => {
  const { patnrId, seriesId } = props;

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

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

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

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

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

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

// Featured Brands 카테고리별 상품 조회 IF-LGSP-311
export const getBrandCategoryInfo = (props) => (dispatch, getState) => {
  const { catCdLv1, catCdLv2, patnrId } = props;

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

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

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

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

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

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_BRAND_CATEGORY_INFO,
    { catCdLv1, catCdLv2, patnrId },
    {},
    onSuccess,
    onFail
  );
};

export const getBrandCategoryProductInfo = (props) => (dispatch, getState) => {
  const { catCdLv1, catCdLv2, patnrId } = props;

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

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

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

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

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

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_BRAND_CATEGORY_INFO,
    { catCdLv1, catCdLv2, patnrId },
    {},
    onSuccess,
    onFail
  );
};

// Featured Brand Best Seller 상품 목록 조회 IF-LGSP-312
export const getBrandBestSeller = (props) => (dispatch, getState) => {
  const { patnrId } = props;

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

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

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

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

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

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

// Featured Brands Showroom 조회 IF-LGSP-372
export const getBrandShowroom = (props) => (dispatch, getState) => {
  const { patnrId } = props;

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

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

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

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

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

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

// Featured Brands Recently Aired 조회 IF-LGSP-373
export const getBrandRecentlyAired = (props) => (dispatch, getState) => {
  const { patnrId } = props;

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

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

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

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

export const setBrandLiveChannelUpcoming = (props) => (dispatch, getState) => {
  const { showId, strtDt } = props;

  const storedBrandLiveChannelUpcoming =
    getState().brand.brandLiveChannelInfoData.data.brandLiveChannelUpcoming;

  const brandLiveChannelUpcoming = storedBrandLiveChannelUpcoming //
    .map((item) => {
      if (item.showId === showId && item.strtDt === strtDt) {
        item.alamDispFlag = item.alamDispFlag === "Y" ? "N" : "Y";
      }

      return item;
    });

  dispatch({
    type: types.SET_BRAND_LIVE_CHANNEL_UPCOMING,
    payload: {
      ...getState().brand.brandLiveChannelInfoData,
      data: {
        ...getState().brand.brandLiveChannelInfoData.data,
        brandLiveChannelUpcoming,
      },
    },
  });
};

export const setBrandChanInfo = (props) => (dispatch, getState) => {
  const { showId, strtDt } = props;

  const storedBrandLiveChanInfo =
    getState().brand.brandLiveChannelInfoData.data.brandChanInfo;

  const brandChanInfo = storedBrandLiveChanInfo.map((item) => {
    if (item.showId === showId && item.strtDt === strtDt) {
      item.alamDispFlag = item.alamDispFlag === "Y" ? "N" : "Y";
    }

    return item;
  });

  dispatch({
    type: types.SET_BRAND_CHAN_INFO,
    payload: {
      ...getState().brand.brandLiveChannelInfoData,
      data: {
        ...getState().brand.brandLiveChannelInfoData.data,
        brandChanInfo,
      },
    },
  });
};
