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

interface OrderActionsProps {
    showVerifyCashPaymentButton: boolean
    showSendToKitchenButton: boolean
    showCancelButton: boolean
    isPreparing: boolean
    isCourierDelivery: boolean
    isCustomerPickup: boolean
    isCanceled: boolean
    isVerifyingPayment: boolean
    isChangingStatus: boolean
    onVerifyCashPayment: () => void
    onSendToKitchen: () => void
    onDeliverToCourier: () => void
    onDeliverToReceptionist: () => void
    onCancelOrder: () => void
}

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

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

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

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

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

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

    if (isCanceled) return null

    return (
        <div className='mt-6 flex flex-col gap-3'>
            {showVerifyCashPaymentButton && (
                <Button
                    label='پرداخت شده'
                    onClick={onVerifyCashPayment}
                    isloading={isVerifyingPayment}
                    className='bg-green-600 hover:bg-green-700'
                />
            )}

            {showSendToKitchenButton && (
                <Button
                    label='در حال آماده سازی'
                    onClick={handleSendToKitchen}
                    isloading={isChangingStatus && loadingAction === 'sendToKitchen'}
                    className='bg-orange-600 hover:bg-orange-700'
                />
            )}

            {isPreparing && isCourierDelivery && (
                <Button
                    label='ارسال شده است'
                    onClick={handleDeliverToCourier}
                    isloading={isChangingStatus && loadingAction === 'deliverToCourier'}
                    className='bg-blue-600 hover:bg-blue-700'
                />
            )}

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

            {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

