import dynamic from 'next/dynamic'
import MobileView from '@/core/components/views/MobileView'
import DesktopView from '@/core/components/views/DesktopView'
import { useRef } from 'react'
import Seo from '@/core/components/Seo'
import DelayRender from '@/core/components/elements/DelayRender/DelayRender'
import { HeroBannerSkeleton } from '@/components/skeleton/BannerSkeleton'
import { PopularProductSkeleton } from '@/components/skeleton/PopularProductSkeleton'
import PromotinProgram from '@/lib/promotinProgram/components/HomePage'

const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout'))
const HeroBanner = dynamic(() => import('@/components/ui/HeroBanner'), {
  loading: () => <HeroBannerSkeleton />
})
const HeroBannerSecondary = dynamic(() => import('@/components/ui/HeroBannerSecondary'), {
  loading: () => <HeroBannerSkeleton />
})
const PopularProduct = dynamic(() => import('@/components/ui/PopularProduct'), {
  loading: () => <PopularProductSkeleton />
})

const PreferredBrand = dynamic(() => import('@/lib/home/components/PreferredBrand'))

const FlashSale = dynamic(() => import('@/lib/flashSale/components/FlashSale'))
const BannerSection = dynamic(() => import('@/lib/home/components/BannerSection'))
const CategoryHomeId = dynamic(() => import('@/lib/home/components/CategoryHomeId'))
const CustomerReviews = dynamic(() => import('@/lib/review/components/CustomerReviews'))

export default function Home() {
  const bannerRef = useRef(null)
  const wrapperRef = useRef(null)

  const handleOnLoad = () => {
    wrapperRef.current.style.height =
      bannerRef.current?.querySelector(':first-child')?.clientHeight + 'px'
  }

  return (
    <BasicLayout>
      <Seo
        title='Indoteknik.com: B2B Industrial Supply & Solution'
        description='Temukan pilihan produk B2B Industri &amp; Alat Teknik untuk Perusahaan, UMKM &amp; Pemerintah dengan lengkap, mudah dan transparan.'
        additionalMetaTags={[
          {
            name: 'keywords',
            content:
              'indoteknik, indoteknik.com, toko teknik, toko perkakas, jual genset, jual fogging, jual krisbow, harga krisbow, harga alat safety, harga pompa air'
          }
        ]}
      />

      <DesktopView>
        <div className='container mx-auto'>
          <div className='flex min-h-[400px] h-[460px]' ref={wrapperRef} onLoad={handleOnLoad}>
            <div className='w-2/12'>
              <HeroBannerSecondary />
            </div>
            <div className='w-7/12 px-1' ref={bannerRef}>
              <HeroBanner />
            </div>
            <div className='w-3/12'>
              <DelayRender renderAfter={200}>
                <PopularProduct />
              </DelayRender>
            </div>
          </div>

          <div className='my-16 flex flex-col gap-y-16'>
            <DelayRender renderAfter={400}>
              <PreferredBrand />
            </DelayRender>
            <DelayRender renderAfter={600}>
              <FlashSale />
            </DelayRender>
            <DelayRender renderAfter={600}>
              <PromotinProgram />
            </DelayRender>
            <DelayRender renderAfter={1000}>
              <CategoryHomeId />
              <BannerSection />
            </DelayRender>
            <DelayRender renderAfter={1200}>
              <CustomerReviews />
            </DelayRender>
          </div>
        </div>
      </DesktopView>

      <MobileView>
        <DelayRender renderAfter={200}>
          <HeroBanner />
        </DelayRender>
        <div className='flex flex-col gap-y-12 my-6'>
          <DelayRender renderAfter={400}>
            <PreferredBrand />
          </DelayRender>
          <DelayRender renderAfter={600}>
            <FlashSale />
          </DelayRender>
          <DelayRender renderAfter={600}>
              <PromotinProgram />
            </DelayRender>
          <DelayRender renderAfter={800}>
            <PopularProduct />
          </DelayRender>
          <DelayRender renderAfter={1000}>
            <CategoryHomeId />
            <BannerSection />
          </DelayRender>
          <DelayRender renderAfter={1200}>
            <CustomerReviews />
          </DelayRender>
        </div>
      </MobileView>
    </BasicLayout>
  )
}
