import dynamic from 'next/dynamic'
import ImageSkeleton from '@/core/components/elements/Skeleton/ImageSkeleton'
import PopularProductSkeleton from '@/lib/home/components/Skeleton/PopularProductSkeleton'
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 { useQuery } from 'react-query'
import odooApi from '@/core/api/odooApi'
import Image from 'next/image'
import CustomerReviews from '@/lib/review/components/CustomerReviews'
import DelayRender from '@/core/components/elements/DelayRender/DelayRender'

const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout'))
const FlashSale = dynamic(() => import('@/lib/flashSale/components/FlashSale'))
const HeroBanner = dynamic(() => import('@/lib/home/components/HeroBanner'), {
  loading: () => <ImageSkeleton />
})
const PreferredBrand = dynamic(() => import('@/lib/home/components/PreferredBrand'), {
  loading: () => <PopularProductSkeleton />
})
const PopularProduct = dynamic(() => import('@/lib/home/components/PopularProduct'), {
  loading: () => <PopularProductSkeleton />
})
const CategoryHomeId = dynamic(() => import('@/lib/home/components/CategoryHomeId'), {
  loading: () => <PopularProductSkeleton />
})

export default function Home() {
  const fetchSecondHeroBanner = async () => await odooApi('GET', '/api/v1/banner?type=index-a-2')
  const secondHeroBanner = useQuery('secondHeroBanner', fetchSecondHeroBanner)

  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 h-[360px]' ref={wrapperRef} onLoad={handleOnLoad}>
            <div className='w-2/12'>
              {secondHeroBanner.isFetched && (
                <Image
                  src={secondHeroBanner.data[0].image}
                  width={512}
                  height={1024}
                  alt={secondHeroBanner.data[0].name}
                  className='object-cover object-center h-full'
                />
              )}
            </div>
            <div className='w-7/12 px-1' ref={bannerRef}>
              <HeroBanner />
            </div>
            <div className='w-3/12'>
              <DelayRender delay={200}>
                <PopularProduct />
              </DelayRender>
            </div>
          </div>

          <div className='my-16 flex flex-col gap-y-16'>
            <DelayRender delay={300}>
              <PreferredBrand />
            </DelayRender>
            <DelayRender delay={400}>
              <FlashSale />
            </DelayRender>
            <DelayRender delay={500}>
              <CategoryHomeId />
            </DelayRender>
            <DelayRender delay={600}>
              <CustomerReviews />
            </DelayRender>
          </div>
        </div>
      </DesktopView>

      <MobileView>
        <HeroBanner />
        <div className='flex flex-col gap-y-12 my-6'>
          <DelayRender delay={300}>
            <PreferredBrand />
          </DelayRender>
          <DelayRender delay={600}>
            <FlashSale />
          </DelayRender>
          <DelayRender delay={900}>
            <PopularProduct />
          </DelayRender>
          <DelayRender delay={1200}>
            <CategoryHomeId />
          </DelayRender>
          <DelayRender delay={1500}>
            <CustomerReviews />
          </DelayRender>
        </div>
      </MobileView>
    </BasicLayout>
  )
}
