import Button from '@/components/Button'
import { type FC, useState, useEffect } from 'react'

interface OrderActionsProps {
    onPrint: () => void
    showSendToKitchenButton: boolean
    showCancelButton: boolean
    isPreparing: boolean
    isCourierDelivery: boolean
    isDineInOrCarDelivery: boolean
    isCustomerPickup: boolean
    isCanceled: boolean
    isChangingStatus: boolean
    onSendToKitchen: () => void
    onDeliverToCourier: () => void
    onDeliverToWaiter: () => void
    onDeliverToReceptionist: () => void
    onCancelOrder: () => void
}

const OrderActions: FC<OrderActionsProps> = ({
    onPrint,
    showSendToKitchenButton,
    showCancelButton,
    isPreparing,
    isCourierDelivery,
    isDineInOrCarDelivery,
    isCustomerPickup,
    isCanceled,
    isChangingStatus,
    onSendToKitchen,
    onDeliverToCourier,
    onDeliverToWaiter,
    onDeliverToReceptionist,
    onCancelOrder
}) => {
    const [loadingAction, setLoadingAction] = useState<string | null>(null)

    useEffect(() => {
        if (!isChangingStatus) {
            setLoadingAction(null)
        }
    }, [isChangingStatus])

    const handleSendToKitchen = () => {
        setLoadingAction('sendToKitchen')
        onSendToKitchen()
    }

    const handleDeliverToCourier = () => {
        setLoadingAction('deliverToCourier')
        onDeliverToCourier()
    }

    const handleDeliverToWaiter = () => {
        setLoadingAction('deliverToWaiter')
        onDeliverToWaiter()
    }

    const handleDeliverToReceptionist = () => {
        setLoadingAction('deliverToReceptionist')
        onDeliverToReceptionist()
    }

    const handleCancelOrder = () => {
        setLoadingAction('cancelOrder')
        onCancelOrder()
    }

    return (
        <div className='mt-6 flex flex-col gap-3'>
            <Button
                label='چاپ'
                onClick={onPrint}
                className='bg-slate-700 hover:bg-slate-800'
            />

            {!isCanceled && showSendToKitchenButton && (
                <Button
                    label='ارسال به آشپزخانه'
                    onClick={handleSendToKitchen}
                    isloading={isChangingStatus && loadingAction === 'sendToKitchen'}
                    className='bg-orange-600 hover:bg-orange-700'
                />
            )}

            {!isCanceled && isPreparing && isCourierDelivery && (
                <Button
                    label='تحویل به پیک'
                    onClick={handleDeliverToCourier}
                    isloading={isChangingStatus && loadingAction === 'deliverToCourier'}
                    className='bg-blue-600 hover:bg-blue-700'
                />
            )}

            {!isCanceled && isPreparing && isDineInOrCarDelivery && (
                <Button
                    label='تحویل به گارسون'
                    onClick={handleDeliverToWaiter}
                    isloading={isChangingStatus && loadingAction === 'deliverToWaiter'}
                    className='bg-purple-600 hover:bg-purple-700'
                />
            )}

            {!isCanceled && isPreparing && isCustomerPickup && (
                <Button
                    label='تحویل به رسپشنیست'
                    onClick={handleDeliverToReceptionist}
                    isloading={isChangingStatus && loadingAction === 'deliverToReceptionist'}
                    className='bg-indigo-600 hover:bg-indigo-700'
                />
            )}

            {!isCanceled && showCancelButton && (
                <Button
                    label='لغو سفارش'
                    onClick={handleCancelOrder}
                    isloading={isChangingStatus && loadingAction === 'cancelOrder'}
                    className='bg-transparent border border-red-500 text-red-500 hover:bg-red-50'
                />
            )}
        </div>
    )
}

export default OrderActions

