"use client";
/**
 * @author: Hemal
 * File: HomeBaseSection.tsx
 * Purpose: This file contains the HomeBaseSection component.
 * It displays a list of home base items.
 * It also handles navigation to the list a home page.
 */
import { Amenity } from "@/src/types/api/property.type";
import React, { useEffect, useState } from "react";
import { createPortal } from "react-dom";
import AmenitiesAccordion from "../AmenitiesAccordion";
import CommonPopup from "../CommonPopup";
import CommonTabs from "../CommonTabs";
import styles from "./HomeBaseSection.module.scss";

// Define the props type for the HomeBaseSection
interface HomeBaseSectionProps {
  type?: number;
  data: {
    title: string;
    description?: string;
    popupDescription?: string;
    items: Amenity;
    deluxeInfo?: {
      label: string;
      description: string;
    };
    type?: string;
    data?: any
    locationLabel?: string;
  };
  bgColor: string;
  AmenitiesShow?: boolean;
  isCoLiving?: boolean;
  isBedroom?: boolean;
}

const HomeBaseSection: React.FC<any> = ({ type, data, isCoLiving = false, isBedroom = false, bgColor, AmenitiesShow = false }) => {
  const [itemsToShow, setItemsToShow] = useState(3);
  const visibleItems = data.items;

  // View All Hombase Amenities Modal
  const [HomebaseamenitiesPopup, setHomebaseamenitiesPopup] = useState(false);

  const [allAmenitiesList, setAllAmenitiesList] = useState(null);
  const [privateAmenitiesData, setPrivateAmenitiesData] = useState([]);

  // Handle the amenities click
  const handleAmenities = (data) => {
    setAllAmenitiesList(data);
    setHomebaseamenitiesPopup(true);
  };

  useEffect(() => {
    const updateItemsToShow = () => {
      const width = window && window.innerWidth;

      if (isBedroom) {
        setItemsToShow(2);
        return;
      }

      if (width < 768) {
        setItemsToShow(2);
      } else if (width < 1200) {
        setItemsToShow(3);
      } else if (width < 1440) {
        setItemsToShow(3);
      } else if (width < 1620) {
        setItemsToShow(4);
      } else if (width < 1800 || width > 1800) {
        setItemsToShow(5);
      }
    };

    updateItemsToShow(); // Set on initial load
    window.addEventListener("resize", updateItemsToShow);
    return () => window.removeEventListener("resize", updateItemsToShow);
  }, []);

  return (
    <>
      {data?.type && data?.type === 'workspace' ?
        <>
          {data?.data?.data && data?.data?.data?.map((item: any, index: number) => {
            return (
              <div className="HomeBaseSection mb-6" key={index}>
                <div className="titleWraper flex items-start justify-between gap-2 mb-4">
                  <div className="flex items-center gap-2.5 w-full flex-wrap">
                    <div className="flex gap-2">
                      {isBedroom ?
                        <h6 className={`!mb-0 !font-fw600`}>{`${data.title} ${data?.data?.data?.length > 1 ? index + 1 : ''}`}</h6>
                        :
                        <h6 className={`!mb-0 !font-fw600`}>{`${data.title} ${data?.data?.data?.length > 1 ? index + 1 : ''}`}</h6>
                      }
                      {type === 2 && data?.deluxeInfo?.label && !isBedroom && !isCoLiving && (
                        <span
                          className={`bg-secondaryColor flex md:justify-center justify-start items-center rounded-[200px] px-2 py-1 relative`}
                        >
                          <p className="fs-14 !font-fw500 !mb-0 !text-white">
                            {data.deluxeInfo.label}
                          </p>
                          <div className="tooltipMainBox">
                            <span className="tooltipSpan ml-1">
                              <i className="Icon IconInfo !bg-white"></i>
                            </span>
                            <div className="hiddenTooltip orangetooltip">
                              <p className={`!mb-[5px] !text-white`}>
                                <b className="fs-16 !font-fw600">
                                  {data.deluxeInfo.label}
                                </b>
                              </p>
                              <p className={`!mb-0 !text-white`}>
                                {data.deluxeInfo.description}
                              </p>
                            </div>
                          </div>
                        </span>
                      )}
                    </div>
                    {item?.workspaceIsInData?.title && !isBedroom && !isCoLiving && (
                      <span
                        className={`xsm:ml-auto flex !bg-[#FF7E671A] md:justify-center justify-start items-center gap-1 rounded-[200px] py-2 px-3`}
                      >
                        <i className={`Icon IconLocation !bg-primaryColor w-[20px] h-[20px]`}></i>
                        <p className="fs-16 !font-fw500 !mb-0 text-grey344Color">{item?.workspaceIsInData?.title}</p>
                      </span>
                    )}
                  </div>
                </div>
                <div className={`${bgColor} rounded-2xl`}>
                  {!AmenitiesShow && <div
                    className={`grid gap-3 ${isBedroom ? 'grid-cols-3 md:grid-cols-3 xl:grid-cols-3 xxl:grid-cols-3 xxxl:grid-cols-3' : 'grid-cols-3 md:grid-cols-4 xl:grid-cols-4 xxl:grid-cols-5 xxxl:grid-cols-6'}  auto-rows-auto`}
                  >
                    {item?.allAmenitiesData?.slice(0, itemsToShow)?.map((item: any, index: number) => (
                      <div
                        key={index}
                        className={`text-center rounded-xl p-3 flex flex-col items-center gap-2`}
                      >
                        <span className="flex justify-center items-center rounded-full w-[40px] h-[40px] shrink-0 p-1">
                          <i
                            className="Icon shrink-0 !bg-primaryColor !w-[25px] !h-[25px]"
                            style={{
                              WebkitMaskImage: item?.icon ? `url(${item?.icon})` : 'none',
                              maskImage: item?.icon ? `url(${item?.icon})` : 'none',
                            }}
                          ></i>
                        </span>
                        <p className="fs-16 !font-fw500 mb-0 text-grey344Color line-clamp-2 text-center">{item?.title}</p>
                      </div>
                    ))}
                    {(item?.private?.length > 0 || item?.allAmenitiesData?.length > itemsToShow) && (
                      <div onClick={() => { setPrivateAmenitiesData(item?.private || []), handleAmenities(item?.allAmenitiesData || []) }} className={`text-center rounded-xl px-2 py-3 flex items-center justify-center gap-2 cursor-pointer bg-bgColor`}>
                        <p className="fs-18 !font-fw700 mb-0 text-secondaryColor line-clamp-2">+ {'More'}</p>
                      </div>
                    )}
                  </div>}
                </div>
                {type == 1 || type == 2 ? null : (
                  <>
                    {data.description && <div className={`mt-6`}>
                      <h6 className="!mb-3 !font-fw600">{"About this place"}</h6>
                      <div className={`p-4 rounded-2xl bg-whiteFBColor break-content`}>
                        <p className={`fs-16 mb-0 text-grey344Color !font-normal`}>
                          {data.description}
                        </p>
                      </div>
                    </div>}
                  </>
                )}
              </div>
            )
          })
          }
        </>
        :
        <div className="HomeBaseSection mb-6">
          <div className="titleWraper flex items-center justify-between gap-2 mb-4">
            <div className="flex items-center gap-2.5 w-full flex-wrap">
              <div className="flex items-center gap-2">
                <h6 className={`!mb-0 !font-fw600`}>{data.title}</h6>
                {type === 2 && data?.deluxeInfo?.label && (
                  <span
                    className={`bg-secondaryColor flex md:justify-center justify-start items-center rounded-[200px] px-2 py-1 relative`}
                  >
                    <p className="fs-14 !font-fw500 !mb-0 !text-white">
                      {data.deluxeInfo.label}
                    </p>
                    <div className="tooltipMainBox">
                      <span className="tooltipSpan ml-1">
                        <i className="Icon IconInfo !bg-white"></i>
                      </span>
                      <div className="hiddenTooltip orangetooltip">
                        <p className={`!mb-[5px] !text-white`}>
                          <b className="fs-16 !font-fw600">
                            {data.deluxeInfo.label}
                          </b>
                        </p>
                        <p className={`!mb-0 !text-white`}>
                          {data.deluxeInfo.description}
                        </p>
                      </div>
                    </div>
                  </span>
                )}

              </div>
              {type === 2 && data.locationLabel && (
                <span
                  className={`xsm:ml-auto flex !bg-[#FF7E671A] md:justify-center justify-start items-center gap-1 rounded-[200px] py-2 px-3`}
                >
                  <i className={`Icon IconLocation !bg-primaryColor w-[20px] h-[20px]`}></i>
                  <p className="fs-16 !font-fw500 !mb-0 text-grey344Color">{data.locationLabel}</p>
                </span>
              )}
            </div>
          </div>
          {/* Tabs Design */}
          {AmenitiesShow && <div className="hidden md:block w-full">
            <CommonTabs AmenitiesTabData={data?.data} isCoLiving={isCoLiving} />
          </div>}
          {/* Tabs Design End */}

          {/* Mobile Amenities Accordion Start */}
          {AmenitiesShow && <div className="block md:hidden w-full">
            <AmenitiesAccordion AmenitiesTabData={data?.data} isCoLiving={isCoLiving} />
          </div>}
          <div className={`${bgColor} rounded-2xl`}>
            <div
              className={`grid gap-3 grid-cols-3 md:grid-cols-4 xl:grid-cols-4 xxl:grid-cols-5 xxxl:grid-cols-6 auto-rows-auto`}
            >
              {visibleItems?.slice(0, itemsToShow)?.map((item, index) => (
                <div
                  key={index}
                  className={`rounded-xl md:p-3 flex flex-col items-center gap-2`}
                >
                  <div className="flex justify-center items-center rounded-full w-[40px] h-[40px] shrink-0">
                    <i
                      className="Icon shrink-0 !bg-primaryColor !w-[25px] !h-[25px]"
                      style={{
                        WebkitMaskImage: item?.icon ? `url(${item?.icon})` : 'none',
                        maskImage: item?.icon ? `url(${item?.icon})` : 'none',
                      }}
                    ></i>
                  </div>
                  <p className="fs-16 !text-center !font-fw500 mb-0 text-grey344Color line-clamp-2">{item?.title}</p>
                </div>
              ))}
              {visibleItems?.length > itemsToShow && (
                <div onClick={() => handleAmenities(visibleItems || [])} className={`text-center rounded-xl px-2 py-3 flex items-center justify-center gap-2 cursor-pointer bg-bgColor`}>
                  <p className="fs-18 !font-fw600 mb-0 text-secondaryColor line-clamp-2">+ {visibleItems.length - itemsToShow}</p>
                </div>
              )}
            </div>
          </div>
          {type == 1 || type == 2 ? null : (
            <>
              {data.description && <div className={`mt-6`}>
                <h6 className="!mb-3 !font-fw600">{"About this place"}</h6>
                <div className={`p-4 rounded-2xl bg-whiteFBColor`}>
                  <p className={`fs-16 mb-0 text-grey344Color !font-normal`}>
                    {data.description}
                  </p>
                </div>
              </div>}
            </>
          )}
        </div>
      }

      {HomebaseamenitiesPopup && (
        // Edit Homebase Popup
        createPortal(
          <CommonPopup
            confirmationModal={false}
            setModal={setHomebaseamenitiesPopup}
            title={data?.title || "Home base"}
            paragraph={data?.popupDescription || ""}
            component={
              <>
                {isCoLiving && privateAmenitiesData?.length > 0 && <h6 className="!mb-4 !font-fw600">{'Private'}</h6>}
                <div className={`grid gap-3 grid-cols-3 ${styles.homebasePopup}`}>
                  {privateAmenitiesData?.length > 0 &&
                    privateAmenitiesData?.map((item: any) => (
                      <div
                        key={item?.amenitiesId}
                        className={`${styles.singleHomeBaseBox} ${isCoLiving ? styles.coLiving : styles.coLiving} text-center rounded-xl px-2 py-3 flex flex-col items-center gap-2`}
                      >
                        <span className="flex justify-center items-center rounded-full w-[40px] h-[40px] shrink-0.5">
                          <i
                            className="Icon w-[24px] h-[20px] !bg-white"
                            style={{
                              WebkitMaskImage: item?.icon ? `url(${item?.icon})` : 'none',
                              maskImage: item?.icon ? `url(${item?.icon})` : 'none',
                            }}
                          ></i>
                        </span>
                        <p className="fs-14 !text-center mb-0 !font-fw500 line-clamp-2">
                          {item?.title}
                        </p>
                      </div>
                    ))}
                </div>
                {isCoLiving && <h6 className={`!mb-4 !font-fw600 ${privateAmenitiesData?.length > 0 ? '!mt-4' : ''}`}>{'Shared'}</h6>}
                <div className={`grid gap-3 grid-cols-3 ${styles.homebasePopup}`}>
                  {allAmenitiesList?.map((item: any, index: number) => (
                    <div
                      key={index}
                      className={`${styles.singleHomeBaseBox} ${isCoLiving ? styles.coLiving : styles.coLiving} text-center rounded-xl px-2 py-3 flex flex-col items-center gap-2`}
                    >
                      <span className="flex justify-center items-center rounded-full w-[40px] h-[40px] shrink-0.5">
                        <i
                          className="Icon w-[24px] h-[20px] !bg-white"
                          style={{
                            WebkitMaskImage: item?.icon ? `url(${item?.icon})` : 'none',
                            maskImage: item?.icon ? `url(${item?.icon})` : 'none',
                          }}
                        ></i>
                      </span>
                      <p className="fs-14 !text-center mb-0 !font-fw500 line-clamp-2">
                        {item?.title}
                      </p>
                    </div>
                  ))}
                </div>
              </>
            }
            footer={false}
          // onSubmit={() => setHomebaseamenitiesPopup(false)}
          // primaryBtnText="Okay"
          />,
          document.body
        )
      )}
    </>
  );
};

export default HomeBaseSection;
