import { useEffect } from 'react';
import { CloseIcon } from '@/components/Icons';

interface ImageLightboxProps {
    images: string[];
    activeIndex: number | null;
    title: string;
    onClose: () => void;
    onChangeIndex: (index: number) => void;
}

export default function ImageLightbox({
    images,
    activeIndex,
    title,
    onClose,
    onChangeIndex,
}: ImageLightboxProps) {
    useEffect(() => {
        if (activeIndex === null) {
            return;
        }

        function handleKeyDown(event: KeyboardEvent) {
            if (event.key === 'Escape') {
                onClose();
            } else if (event.key === 'ArrowRight' && images.length > 1) {
                onChangeIndex(
                    (activeIndex! - 1 + images.length) % images.length,
                );
            } else if (event.key === 'ArrowLeft' && images.length > 1) {
                onChangeIndex((activeIndex! + 1) % images.length);
            }
        }

        document.addEventListener('keydown', handleKeyDown);
        return () => document.removeEventListener('keydown', handleKeyDown);
    }, [activeIndex, images.length, onChangeIndex, onClose]);

    if (activeIndex === null || !images[activeIndex]) {
        return null;
    }

    return (
        <div
            className="fixed inset-0 z-50 flex items-center justify-center bg-slate-950/85 p-3 backdrop-blur-xs sm:p-6"
            onClick={onClose}
            role="dialog"
            aria-modal="true"
            aria-label={title}
        >
            <div
                className="relative w-full max-w-4xl overflow-hidden rounded-2xl bg-white p-3 shadow-2xl sm:p-4"
                onClick={(e) => e.stopPropagation()}
            >
                <div className="mb-3 flex items-center justify-between gap-3">
                    <span className="truncate text-xs font-bold text-slate-700 sm:text-sm">
                        {title}{' '}
                        {images.length > 1 &&
                            `(${(activeIndex + 1).toLocaleString('fa-IR')} از ${images.length.toLocaleString('fa-IR')})`}
                    </span>
                    <button
                        type="button"
                        onClick={onClose}
                        aria-label="بستن تصویر"
                        className="inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-slate-100 text-slate-700 transition hover:bg-slate-200"
                    >
                        <CloseIcon className="h-4 w-4" />
                    </button>
                </div>

                <img
                    src={images[activeIndex]}
                    alt={`${title} - ${(activeIndex + 1).toLocaleString('fa-IR')}`}
                    className="max-h-[75vh] w-full rounded-xl bg-slate-100 object-contain"
                />

                {images.length > 1 && (
                    <div className="mt-3 flex items-center justify-between gap-2">
                        <button
                            type="button"
                            onClick={() =>
                                onChangeIndex(
                                    (activeIndex - 1 + images.length) %
                                        images.length,
                                )
                            }
                            className="rounded-xl border border-slate-200 bg-slate-50 px-4 py-2 text-xs font-bold text-slate-700 transition hover:bg-slate-100"
                        >
                            تصویر قبلی
                        </button>
                        <button
                            type="button"
                            onClick={() =>
                                onChangeIndex((activeIndex + 1) % images.length)
                            }
                            className="rounded-xl border border-slate-200 bg-slate-50 px-4 py-2 text-xs font-bold text-slate-700 transition hover:bg-slate-100"
                        >
                            تصویر بعدی
                        </button>
                    </div>
                )}
            </div>
        </div>
    );
}
