'use client'

import React from 'react'
import SelectedProduct from '../SelectedProduct'
import BoatNotFound from '../BoatNotFound'
import { Button } from '@/components/shadcn/ui/button'
import FastboatTicketCardSkeleton from '@/components/skeletons/FastboatTicketCardSkeleton'
import { FastboatProduct } from '@/lib/types/fastboat'
import { SearchParams } from '@/components/search-form/types'
import BoatRouteDialog from '@/components/modal/BoatRouteDialog'

interface ChooseBoatProps {
  searchParams: SearchParams
  chooseBoatSteps: {
    step: string
    content: React.ReactNode
  }[]
  currentChooseBoatStep: number
  setCurrentChooseBoatStep: React.Dispatch<React.SetStateAction<number>>
  selectedDepartureProduct: FastboatProduct | null
  selectedReturnProduct: FastboatProduct | null
  boatFound: boolean
  loadingBoats: boolean
  setSearchFormOpened: React.Dispatch<React.SetStateAction<boolean>>
}

const ChooseBoat: React.FC<ChooseBoatProps> = ({
  searchParams,
  chooseBoatSteps,
  currentChooseBoatStep,
  setCurrentChooseBoatStep,
  selectedDepartureProduct,
  selectedReturnProduct,
  boatFound,
  loadingBoats,
  setSearchFormOpened,
}) => {
  return (
    <BoatRouteDialog>
      <div>
        <section className="w-full mx-auto grid grid-cols-12 sm:gap-6">
          <div className="col-span-12 lg:col-span-4 xl:col-span-3 space-y-4 flex-col sm:flex-row flex lg:flex-col gap-4 items-start w-full">
            <SelectedProduct
              trip="ONE_WAY"
              product={selectedDepartureProduct}
              date={searchParams?.depart_date}
              onChangeSelection={() => setCurrentChooseBoatStep(0)}
            />

            {searchParams.trip === 'RETURN' && (
              <SelectedProduct
                trip="RETURN"
                product={selectedReturnProduct}
                date={searchParams?.return_date}
                onChangeSelection={() => setCurrentChooseBoatStep(1)}
              />
            )}
          </div>

          {boatFound ? (
            <div className="col-span-12 lg:col-span-8 xl:col-span-9 space-y-4 mt-10 md:mt-0">
              {chooseBoatSteps[currentChooseBoatStep].content}
            </div>
          ) : loadingBoats ? (
            <div className="col-span-12 lg:col-span-8 xl:col-span-9 space-y-4 mt-10 md:mt-0">
              {Array.from({ length: 3 }).map((_, index) => (
                <FastboatTicketCardSkeleton key={index} />
              ))}
            </div>
          ) : (
            <div className="col-span-12 lg:col-span-8 xl:col-span-9 mt-10 md:mt-0 bg-gray-100 rounded-lg p-8 h-fit">
              <BoatNotFound />
              <Button
                className="mt-4"
                variant="black"
                type="button"
                onClick={() => {
                  setSearchFormOpened(true)
                  window.scrollTo({
                    top: 0,
                    behavior: 'smooth',
                  })
                }}
              >
                CHANGE SEARCH
              </Button>
            </div>
          )}
        </section>
      </div>
    </BoatRouteDialog>
  )
}

export default ChooseBoat
