import React from 'react'
import { LuChevronLeft, LuChevronRight } from 'react-icons/lu'
import { ButtonHTMLAttributes } from 'react'
import { useStepperContext } from './Index'
import { cn } from '@/lib/utils/tw-merge'
import { Button } from 'flowbite-react'
import buttonTheme from '@/lib/themes/button'

interface NavigationProps {
  className?: string
  showStepCount?: boolean
  prevLabel?: string
  nextLabel?: string
  variant?: 'light' | 'blue' | 'dark'
  disabled?: boolean
  enablePrev?: boolean
  prevIcon?: React.ReactNode
  nextIcon?: React.ReactNode
  prevButtonType?: ButtonHTMLAttributes<HTMLButtonElement>['type']
  nextButtonType?: ButtonHTMLAttributes<HTMLButtonElement>['type']
  isSticky?: boolean
  prevButtonHandler?: () => void
  nextButtonHandler?: () => void
}

const Navigation = ({
  className,
  showStepCount = true,
  prevLabel = 'Previous',
  nextLabel = 'Next',
  variant = 'light',
  prevButtonType = 'button',
  nextButtonType = 'button',
  enablePrev = false,
  disabled = false,
  isSticky = false,
  prevButtonHandler,
  nextButtonHandler,
  prevIcon = <LuChevronLeft className="mr-1 h-4 w-4" />,
  nextIcon = <LuChevronRight className="ml-1 h-4 w-4" />,
}: NavigationProps) => {
  const { currentStep, totalSteps, isFirstStep, goToPrevious, goToNext } =
    useStepperContext()

  return (
    <div
      className={cn(
        'flex items-center max-[475px]:flex-col sm:flex-row justify-between mt-6 bg-white dark:bg-gray-800 border-t border-gray-200 dark:border-gray-700 p-4',
        className,
        {
          'sticky bottom-0 z-5': isSticky,
        },
      )}
    >
      {showStepCount ? (
        <div className="text-sm text-gray-500">
          Step {currentStep + 1} of {totalSteps}
        </div>
      ) : (
        <div />
      )}

      <div className="flex max-[475px]:space-x-0 space-x-2 space-y-2 mt-4 sm:mt-0 max-[475px]:flex-wrap sm:flex-nowrap">
        {/* Prev Button */}
        <Button
          theme={buttonTheme.button}
          type={prevButtonType}
          color={variant}
          size="sm"
          className="max-[475px]:w-full sm:w-fit"
          onClick={() => {
            if (prevButtonHandler) {
              prevButtonHandler()
            } else {
              goToPrevious()
            }
          }}
          disabled={enablePrev ? !enablePrev : isFirstStep}
        >
          {prevIcon}
          {prevLabel}
        </Button>

        {/* Next Button */}
        <Button
          type={nextButtonType}
          theme={buttonTheme.button}
          color={variant === 'light' ? 'dark' : variant}
          size="sm"
          className="max-[475px]:w-full sm:w-fit"
          onClick={() => {
            if (nextButtonHandler) {
              nextButtonHandler()
            } else {
              goToNext()
            }
          }}
          disabled={disabled}
        >
          {nextLabel}
          {nextIcon}
        </Button>
      </div>
    </div>
  )
}

Navigation.displayName = 'Stepper.Navigation'

export default Navigation
