import { ChevronLeft, ChevronRight, Plus } from 'lucide-react'
import { useRef } from 'react'
import { useLocale } from '@meshkee/dashboard-ui'
import { useT } from '../i18n/useT'
import type { WebsiteSliderSlide } from '../types/websiteSlider'
import { Tooltip } from './Tooltip'
import { WebsiteSliderSlideCard } from './WebsiteSliderSlideCard'
import styles from './WebsiteSliderGallery.module.css'

interface WebsiteSliderGalleryProps {
  slides: WebsiteSliderSlide[]
  onAddSlide: () => void
  onRemoveSlide: (slide: WebsiteSliderSlide) => void
}

export function WebsiteSliderGallery({
  slides,
  onAddSlide,
  onRemoveSlide,
}: WebsiteSliderGalleryProps) {
  const t = useT()
  const { locale } = useLocale()
  const isFa = locale === 'fa'
  const trackRef = useRef<HTMLDivElement>(null)
  const showNav = slides.length > 0

  function scrollBy(direction: -1 | 1) {
    const track = trackRef.current
    if (!track) return
    const amount = Math.max(track.clientWidth * 0.75, 280)
    track.scrollBy({ left: direction * amount, behavior: 'smooth' })
  }

  const addCard = (
    <div className={`${styles.slideSlot} ${styles.slideSlotAdd}`}>
      <Tooltip label={t('website.sliders.addSlide')}>
        <button
          type="button"
          className={styles.addCard}
          onClick={onAddSlide}
          aria-label={t('website.sliders.addSlide')}
        >
          <span className={styles.addIcon}>
            <Plus size={28} />
          </span>
        </button>
      </Tooltip>
    </div>
  )

  const slideCards = slides.map((slide) => (
    <div key={slide.id} className={styles.slideSlot}>
      <WebsiteSliderSlideCard slide={slide} onRemove={() => onRemoveSlide(slide)} />
    </div>
  ))

  return (
    <section className={styles.section}>
      {showNav && (
        <div className={styles.header}>
          <div className={styles.navPair} dir="ltr">
            <Tooltip label={t('website.scrollLeft')}>
              <button
                type="button"
                className={styles.navBtn}
                onClick={() => scrollBy(-1)}
                aria-label={t('website.scrollLeft')}
              >
                <ChevronLeft size={18} />
              </button>
            </Tooltip>
            <Tooltip label={t('website.scrollRight')}>
              <button
                type="button"
                className={styles.navBtn}
                onClick={() => scrollBy(1)}
                aria-label={t('website.scrollRight')}
              >
                <ChevronRight size={18} />
              </button>
            </Tooltip>
          </div>
        </div>
      )}

      <div className={styles.trackWrap}>
        <div className={styles.track} ref={trackRef} dir={isFa ? 'rtl' : 'ltr'}>
          {slideCards}
          {addCard}
        </div>
      </div>
    </section>
  )
}
