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 { useEffect, useRef, useState } from 'react'
import { NextSeo } from 'next-seo'
import Seo from '@/core/components/Seo'
import { useQuery } from 'react-query'
import odooApi from '@/core/api/odooApi'
import Image from '@/core/components/elements/Image/Image'

const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout'))

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-b-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-3/12'>
              {secondHeroBanner.isFetched && (
                <Image src={secondHeroBanner.data[0].image} alt={secondHeroBanner.data[0].name} />
              )}
            </div>
            <div className='w-6/12 px-1' ref={bannerRef}>
              <HeroBanner />
            </div>
            <div className='w-3/12'>
              <PopularProduct />
            </div>
          </div>

          <div className='my-16 flex flex-col gap-y-10'>
            <PreferredBrand />
            <CategoryHomeId />
          </div>
        </div>
      </DesktopView>

      <MobileView>
        <HeroBanner />
        <div className='flex flex-col gap-y-6 my-6'>
          <PreferredBrand />
          <PopularProduct />
          <CategoryHomeId />
        </div>
      </MobileView>
    </BasicLayout>
  )
}
