import React, { memo, useCallback, useEffect, useMemo, useState } from "react";

import { useSelector } from "react-redux";

import Spotlight from "@enact/spotlight";
import SpotlightContainerDecorator from "@enact/spotlight/SpotlightContainerDecorator";

import SectionTitle from "../../../components/SectionTitle/SectionTitle";
import { $L } from "../../../utils/helperMethods";
import css from "./Showroom.module.less";
import ShowroomContents from "./ShowroomContents/ShowroomContents";
import ShowroomNav from "./ShowroomNav/ShowroomNav";

const STRING_CONF = {
  SHOWROOM: "SHOWROOM",
};

const Container = SpotlightContainerDecorator(
  { leaveFor: { right: "" }, enterTo: null },
  "div"
);

const Showroom = ({
  brandShowroomInfo,
  fromGNB,
  fromQuickMenu,
  handleItemFocus,
  order,
  shelfOrder,
  shelfTitle,
  selectedPatnrId,
  selectedPatncNm,
  spotlightId,
}) => {
  const panelInfo = useSelector((state) => state.panels.panels[0]?.panelInfo);

  const [selectedRoomId, setSelectedRoomId] = useState(null);
  const [firstChk, setFirstChk] = useState(0);

  const selectedRoomThemeInfos = useMemo(
    () =>
      brandShowroomInfo //
        .find(({ roomId }) => roomId === selectedRoomId)?.roomThemeInfos,
    [brandShowroomInfo, selectedRoomId]
  );

  useEffect(() => {
    if (!selectedRoomId) {
      if (panelInfo?.roomId) {
        return setSelectedRoomId(panelInfo?.roomId);
      }

      setSelectedRoomId(brandShowroomInfo[0]?.roomId);
    }
  }, [brandShowroomInfo, panelInfo, selectedRoomId]);

  const _handleItemFocus = useCallback(() => {
    if (handleItemFocus) handleItemFocus(spotlightId, shelfOrder);

    const c = Spotlight.getCurrent();
    if (firstChk === 0) {
      if (c) {
        let cAriaLabel = c.getAttribute("aria-label");
        if (cAriaLabel) {
          cAriaLabel = "showroom, Heading 1," + cAriaLabel;
          c.setAttribute("aria-label", cAriaLabel);
        }
      }
      setFirstChk(1);
    } else if (firstChk === 1) {
      if (c) {
        let cAriaLabel = c.getAttribute("aria-label");
        if (cAriaLabel) {
          const newcAriaLabel = cAriaLabel.replace("showroom, Heading 1,", "");
          c.setAttribute("aria-label", newcAriaLabel);
        }
      }
    } else {
      return;
    }
  }, [firstChk, handleItemFocus]);

  return (
    <Container
      className={css.container}
      data-wheel-point
      data-shelf-order={order}
      spotlightId={spotlightId}
    >
      <SectionTitle title={$L(STRING_CONF.SHOWROOM)} data-title="showroom" />
      <ShowroomNav
        brandShowroomInfo={brandShowroomInfo}
        fromGNB={fromGNB}
        fromQuickMenu={fromQuickMenu}
        handleItemFocus={_handleItemFocus}
        selectedPatncNm={selectedPatncNm}
        selectedPatnrId={selectedPatnrId}
        selectedRoomId={selectedRoomId}
        setSelectedRoomId={setSelectedRoomId}
      />
      {selectedRoomThemeInfos &&
        selectedRoomThemeInfos.map(
          (selectedRoomThemeInfoItem, contentsIndex) => (
            <ShowroomContents
              handleItemFocus={_handleItemFocus}
              key={`${spotlightId}-${contentsIndex}`}
              contentsIndex={contentsIndex}
              selectedPatncNm={selectedPatncNm}
              selectedPatnrId={selectedPatnrId}
              selectedRoomId={selectedRoomId}
              selectedRoomThemeInfoItem={selectedRoomThemeInfoItem}
              spotlightId={spotlightId}
              shelfOrder={shelfOrder}
              shelfTitle={shelfTitle}
            />
          )
        )}
    </Container>
  );
};

export default memo(Showroom);
