import { useState, useEffect, useCallback } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { motion, AnimatePresence } from 'framer-motion';
import { X, ChevronLeft, ChevronRight, Download } from 'lucide-react';
import { RowsPhotoAlbum } from 'react-photo-album';
import 'react-photo-album/rows.css';

function AlbumGallery() {
    const { name } = useParams();
    const navigate = useNavigate();
    const [album, setAlbum] = useState(null);
    const [photos, setPhotos] = useState([]);
    const [loading, setLoading] = useState(true);
    const [lightbox, setLightbox] = useState({ open: false, index: 0 });
    const [imageLoaded, setImageLoaded] = useState(false);
    const [slideDirection, setSlideDirection] = useState(0);

    // URL을 썸네일/원본 버전으로 변환하는 헬퍼
    const getThumbUrl = (url) => {
        // https://s3.../photo/01.webp → https://s3.../photo/thumb_400/01.webp
        const parts = url.split('/');
        const filename = parts.pop();
        return [...parts, 'thumb_400', filename].join('/');
    };

    const getOriginalUrl = (url) => {
        const parts = url.split('/');
        const filename = parts.pop();
        return [...parts, 'original', filename].join('/');
    };

    // 이미지 dimensions 로드
    const loadImageDimensions = (url) => {
        return new Promise((resolve) => {
            const img = new Image();
            img.onload = () => resolve({ width: img.naturalWidth, height: img.naturalHeight });
            img.onerror = () => resolve({ width: 3, height: 4 }); // 기본 3:4 비율
            img.src = url;
        });
    };

    useEffect(() => {
        fetch(`/api/albums/by-name/${name}`)
            .then(res => res.json())
            .then(async data => {
                setAlbum(data);
                const allPhotos = [];
                
                if (data.conceptPhotos && typeof data.conceptPhotos === 'object') {
                    Object.entries(data.conceptPhotos).forEach(([concept, photos]) => {
                        photos.forEach(p => allPhotos.push({ 
                            thumbUrl: getThumbUrl(p.url),
                            originalUrl: getOriginalUrl(p.url),
                            title: concept,
                            members: p.members ? p.members.split(', ') : []
                        }));
                    });
                }
                
                // 모든 이미지 dimensions 로드
                const photosWithDimensions = await Promise.all(
                    allPhotos.map(async (photo) => {
                        const dims = await loadImageDimensions(photo.thumbUrl);
                        return { ...photo, width: dims.width, height: dims.height };
                    })
                );
                
                setPhotos(photosWithDimensions);
                setLoading(false);
            })
            .catch(error => {
                console.error('앨범 데이터 로드 오류:', error);
                setLoading(false);
            });
    }, [name]);

    // 라이트박스 열기
    const openLightbox = (index) => {
        setImageLoaded(false);
        setLightbox({ open: true, index });
    };

    // 라이트박스 닫기
    const closeLightbox = useCallback(() => {
        setLightbox(prev => ({ ...prev, open: false }));
    }, []);

    // 이전/다음 이미지
    const goToPrev = useCallback(() => {
        if (photos.length <= 1) return;
        setImageLoaded(false);
        setSlideDirection(-1);
        setLightbox(prev => ({
            ...prev,
            index: (prev.index - 1 + photos.length) % photos.length
        }));
    }, [photos.length]);

    const goToNext = useCallback(() => {
        if (photos.length <= 1) return;
        setImageLoaded(false);
        setSlideDirection(1);
        setLightbox(prev => ({
            ...prev,
            index: (prev.index + 1) % photos.length
        }));
    }, [photos.length]);

    // 다운로드
    const downloadImage = useCallback(async () => {
        const photo = photos[lightbox.index];
        if (!photo) return;
        
        try {
            const response = await fetch(photo.originalUrl);
            const blob = await response.blob();
            const url = window.URL.createObjectURL(blob);
            const link = document.createElement('a');
            link.href = url;
            link.download = `fromis9_${album?.title || 'photo'}_${String(lightbox.index + 1).padStart(2, '0')}.webp`;
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            window.URL.revokeObjectURL(url);
        } catch (error) {
            console.error('다운로드 오류:', error);
        }
    }, [photos, lightbox.index, album?.title]);

    // 키보드 이벤트
    useEffect(() => {
        if (!lightbox.open) return;

        const handleKeyDown = (e) => {
            switch (e.key) {
                case 'ArrowLeft': goToPrev(); break;
                case 'ArrowRight': goToNext(); break;
                case 'Escape': closeLightbox(); break;
                default: break;
            }
        };

        window.addEventListener('keydown', handleKeyDown);
        return () => window.removeEventListener('keydown', handleKeyDown);
    }, [lightbox.open, goToPrev, goToNext, closeLightbox]);

    // 프리로딩
    useEffect(() => {
        if (!lightbox.open || photos.length <= 1) return;
        
        const prevIdx = (lightbox.index - 1 + photos.length) % photos.length;
        const nextIdx = (lightbox.index + 1) % photos.length;
        
        [prevIdx, nextIdx].forEach(idx => {
            const img = new Image();
            img.src = photos[idx].originalUrl;
        });
    }, [lightbox.open, lightbox.index, photos]);

    if (loading) {
        return (
            <motion.div 
                initial={{ opacity: 0 }}
                animate={{ opacity: 1 }}
                className="py-16 flex justify-center items-center min-h-[60vh]"
            >
                <div className="animate-spin rounded-full h-12 w-12 border-4 border-primary border-t-transparent"></div>
            </motion.div>
        );
    }

    return (
        <>
        <motion.div 
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            transition={{ duration: 0.3 }}
            className="py-16"
        >
            <div className="container mx-auto px-4">
                {/* 브레드크럼 스타일 헤더 */}
                <div className="mb-8">
                    <div className="flex items-center gap-2 text-sm text-gray-500 mb-2">
                        <button 
                            onClick={() => navigate('/album')}
                            className="hover:text-primary transition-colors"
                        >
                            앨범
                        </button>
                        <span>/</span>
                        <button 
                            onClick={() => navigate(`/album/${name}`)}
                            className="hover:text-primary transition-colors"
                        >
                            {album?.title}
                        </button>
                        <span>/</span>
                        <span className="text-gray-700">컨셉 포토</span>
                    </div>
                    <h1 className="text-3xl font-bold">컨셉 포토</h1>
                    <p className="text-gray-500 mt-1">{photos.length}장의 사진</p>
                </div>

                {/* Justified 갤러리 - react-photo-album */}
                <RowsPhotoAlbum
                    photos={photos.map((photo, idx) => ({
                        src: photo.thumbUrl,
                        width: photo.width || 300,
                        height: photo.height || 400,
                        key: idx.toString()
                    }))}
                    targetRowHeight={300}
                    spacing={8}
                    onClick={({ index }) => openLightbox(index)}
                    componentsProps={{
                        container: { style: { cursor: 'pointer' } },
                        image: { 
                            loading: 'lazy',
                            style: { borderRadius: '8px', transition: 'transform 0.3s' }
                        }
                    }}
                />
            </div>
        </motion.div>

        {/* 라이트박스 */}
        <AnimatePresence>
        {lightbox.open && (
            <motion.div 
                initial={{ opacity: 0 }}
                animate={{ opacity: 1 }}
                exit={{ opacity: 0 }}
                transition={{ duration: 0.2 }}
                className="fixed inset-0 bg-black/95 z-50 flex items-center justify-center"
            >
                {/* 상단 버튼들 */}
                <div className="absolute top-6 right-6 flex gap-3 z-10">
                    <button 
                        className="text-white/70 hover:text-white transition-colors"
                        onClick={downloadImage}
                    >
                        <Download size={28} />
                    </button>
                    <button 
                        className="text-white/70 hover:text-white transition-colors"
                        onClick={closeLightbox}
                    >
                        <X size={32} />
                    </button>
                </div>

                {/* 카운터 */}
                <div className="absolute top-6 left-6 text-white/70 text-sm z-10">
                    {lightbox.index + 1} / {photos.length}
                </div>

                {/* 이전 버튼 */}
                {photos.length > 1 && (
                    <button 
                        className="absolute left-6 text-white/70 hover:text-white transition-colors z-10"
                        onClick={goToPrev}
                    >
                        <ChevronLeft size={48} />
                    </button>
                )}

                {/* 로딩 스피너 */}
                {!imageLoaded && (
                    <div className="absolute inset-0 flex items-center justify-center">
                        <div className="animate-spin rounded-full h-12 w-12 border-4 border-white border-t-transparent"></div>
                    </div>
                )}

                {/* 이미지 */}
                <motion.img 
                    key={lightbox.index}
                    src={photos[lightbox.index]?.originalUrl}
                    alt="확대 이미지"
                    className={`max-w-[90vw] max-h-[85vh] object-contain rounded-lg transition-opacity duration-200 ${imageLoaded ? 'opacity-100' : 'opacity-0'}`}
                    onLoad={() => setImageLoaded(true)}
                    initial={{ x: slideDirection * 100 }}
                    animate={{ x: 0 }}
                    transition={{ duration: 0.25, ease: 'easeOut' }}
                />

                {/* 다음 버튼 */}
                {photos.length > 1 && (
                    <button 
                        className="absolute right-6 text-white/70 hover:text-white transition-colors z-10"
                        onClick={goToNext}
                    >
                        <ChevronRight size={48} />
                    </button>
                )}

                {/* 하단 점 인디케이터 */}
                <div className="absolute bottom-6 flex gap-1.5 flex-wrap justify-center max-w-[80vw]">
                    {photos.map((_, i) => (
                        <button
                            key={i}
                            className={`w-2 h-2 rounded-full transition-colors ${i === lightbox.index ? 'bg-white' : 'bg-white/40'}`}
                            onClick={() => {
                                setImageLoaded(false);
                                setLightbox({ ...lightbox, index: i });
                            }}
                        />
                    ))}
                </div>
            </motion.div>
        )}
        </AnimatePresence>
        </>
    );
}

export default AlbumGallery;
