import { types } from '../actions/actionTypes';
import { panel_names } from '../utils/Config';

const initialState = {
  termsData: {},
  menuData: {},
  layoutData: {},
  themeCurationInfoData: {},
  themeCurationDetailInfoData: {},
  themeCurationHotelDetailData: {},
  homeTopDisplayInfo: {},
  bannerData: {
    data: {},
    status: "idle",
  },
  defaultFocus: null,
  enterThroughGNB: false,
  themeCurationInfoDataCode: null,
  bannerIndices: {},
  themeMenuShelfInfoData: null,
  homeInfo: null,
  curationId: "",
  curationTitle: "",
  playerControl: {
    ownerId: null,
    videoInfo: null,
  },
  termsIdMap: {}, // added new property to initialState
  optionalTermsAvailable: false, // 선택약관 존재 여부
  persistentVideoInfo: null, // 영구재생 비디오 정보
};

export const homeReducer = (state = initialState, action) => {
  switch (action.type) {
    case types.GET_HOME_TERMS:
      return {
        ...state,
        termsData: action.payload,
      };

    case types.SET_TERMS_ID_MAP:
      return {
        ...state,
        termsIdMap: action.payload,
      };

    case types.SET_OPTIONAL_TERMS_AVAILABILITY:
      return {
        ...state,
        optionalTermsAvailable: action.payload,
      };

    case types.GET_HOME_MENU: {
      let menuItems = [];

      const menuData = action.payload.data;
      if (menuData?.gnb) {
        const info = menuData.gnb.map((item) => {
          return {
            menuNm: item.menuNm,
            menuId: item.menuId,
          };
        });

        for (let i = 0; i < info.length; i++) {
          const title = info[i].menuNm;
          const menuId = info[i].menuId;
          menuItems.push({
            title: title,
            menuId: menuId,
          });
        }
      }
      return {
        ...state,
        menuData: action.payload,
        menuItems: menuItems,
      };
    }

    case types.GET_HOME_LAYOUT:
      return {
        ...state,
        layoutData: action.payload,
      };

    case types.GET_HOME_MAIN_CONTENTS: {
      const homeTopDisplayInfo = action.payload?.homeTopDisplayInfos?.[0] ?? {};
      const bannerData = action.payload?.homeMainContentsBannerInfos?.[0] ?? {};

      const curationTitle =
        action.payload?.homeMainContentsBannerInfos?.[0].curtNm;
      const curationId =
        action.payload?.homeMainContentsBannerInfos?.[0].curationId;

      if (bannerData.bannerInfos) {
        for (let i = 0; i < bannerData.bannerInfos.length; i++) {
          const info = bannerData.bannerInfos[i];
          if (info.shptmDspyTpNm === "Random") {
            const dataLength = info.bannerDetailInfos.length;
            info.randomIndex = Math.floor(Math.random() * dataLength);
          }
        }
      }
      return {
        ...state,
        homeTopDisplayInfo: homeTopDisplayInfo,
        bannerData: bannerData,
        curationId,
        curationTitle,
      };
    }
    case types.GET_THEME_CURATION_INFO:
      return {
        ...state,
        themeCurationInfoData: action.payload,
        themeCurationInfoDataCode: action.retCode,
      };
    case types.GET_THEME_CURATION_DETAIL_INFO: {
      const productInfo = action.payload.themeInfo[0].productInfos;

      return {
        ...state,
        themeCurationDetailInfoData: productInfo,
        productData: action.payload,
      };
    }

    case types.GET_THEME_HOTEL_DETAIL_INFO: {
      const hotelInfos = action.payload.hotelInfo.hotelInfos;

      return {
        ...state,
        themeCurationHotelDetailData: hotelInfos,
        hotelData: action.payload,
      };
    }

    case types.GET_THEME_MENU_SHELF_INFO: {
      return {
        ...state,
        themeMenuShelfInfoData: action.payload.themeMenuInfo,
      };
    }

    case types.CLEAR_THEME_MENU_SHELF_INFO: {
      return {
        ...state,
        themeMenuShelfInfoData: null,
      };
    }

    case types.UPDATE_HOME_INFO: {
      return {
        ...state,
        homeInfo: {
          ...action.payload,
          panelInfo: {
            ...state.homeInfo?.panelInfo,
            ...action.payload?.panelInfo,
          },
        },
      };
    }

    case types.RESET_HOME_INFO: {
      return {
        ...state,
        homeInfo: {},
      };
    }

    case types.CLEAR_THEME_DETAIL: {
      return {
        ...state,
        themeCurationHotelDetailData: null,
        themeCurationDetailInfoData: [],
        productData: null,
        hotelData: null,
      };
    }

    case types.SET_DEFAULT_FOCUS: {
      return {
        ...state,
        defaultFocus: action.payload,
      };
    }

    case types.CHECK_ENTER_THROUGH_GNB: {
      return {
        ...state,
        enterThroughGNB: action.payload,
      };
    }

    case types.SET_BANNER_INDEX: {
      const { bannerId, index } = action.payload;

      if (bannerId) {
        return {
          ...state,
          bannerIndices: {
            ...state.bannerIndices,
            [bannerId]: index,
          },
        };
      }
    }

    // 🔽 [수정] 새로운 플레이어 제어 로직
    case types.SET_PLAYER_CONTROL: {
      return {
        ...state,
        playerControl: {
          ownerId: action.payload.ownerId,
          videoInfo: action.payload.videoInfo,
        },
      };
    }

    case types.CLEAR_PLAYER_CONTROL: {
      return {
        ...state,
        playerControl: {
          ownerId: null,
          videoInfo: null,
        },
      };
    }

    case types.SET_PERSISTENT_VIDEO_INFO: {
      return {
        ...state,
        persistentVideoInfo: action.payload,
      };
    }
    // 🔼 [수정]

    default:
      return state;
  }
};
