/**
 * gallery.js — Supernova Gallery Page Script (Figma Template Interactive Logic)
 */
(function () {
    'use strict';

    var currentPhotoIndex = 0;

    function getPhotos() {
        return window.galleryPhotos || [];
    }

    // 2. LIGHTBOX MODAL (Full-Screen Photo Viewer Window)
    function openLightbox(input) {
        var lb = document.getElementById('gal-lightbox');
        if (!lb) return;

        var img = document.getElementById('lb-img');
        var allPhotos = getPhotos();

        if (typeof input === 'number') {
            currentPhotoIndex = input;
        } else if (typeof input === 'string') {
            var normInput = decodeURIComponent(input);
            var inputFile = normInput.split('/').pop();
            var foundIdx = -1;
            for (var i = 0; i < allPhotos.length; i++) {
                var normSrc = decodeURIComponent(allPhotos[i].src);
                var srcFile = normSrc.split('/').pop();
                if (normSrc === normInput || (srcFile && srcFile === inputFile)) {
                    foundIdx = i;
                    break;
                }
            }
            if (foundIdx !== -1) {
                currentPhotoIndex = foundIdx;
            } else {
                if (img) {
                    img.src = input;
                    img.alt = 'Supernova Photo';
                }
            }
        }

        renderLightbox();
        lb.classList.remove('hidden');
        lb.classList.add('active');
        lb.style.display = 'flex';
        document.body.style.overflow = 'hidden';
    }

    function closeLightbox() {
        var lb = document.getElementById('gal-lightbox');
        if (!lb) return;
        lb.classList.add('hidden');
        lb.classList.remove('active');
        lb.style.display = 'none';
        document.body.style.overflow = '';
    }

    function renderLightbox() {
        var allPhotos = getPhotos();
        var photo = allPhotos[currentPhotoIndex];
        if (!photo) return;

        var img = document.getElementById('lb-img');
        var counter = document.getElementById('lb-counter');
        var title = document.getElementById('lb-title');
        var desc = document.getElementById('lb-desc');

        if (img && photo && photo.src) {
            img.src = photo.src;
            img.alt = photo.title || 'Supernova 2.0 Photo';
        }
        if (counter) {
            var padCur = (currentPhotoIndex + 1 < 10 ? '0' : '') + (currentPhotoIndex + 1);
            var padTot = (allPhotos.length < 10 ? '0' : '') + allPhotos.length;
            counter.textContent = padCur + ' / ' + padTot;
        }
        if (title) title.textContent = photo.title || '';
        if (desc)  desc.textContent  = photo.desc  || '';
    }

    function lbPrev() {
        var allPhotos = getPhotos();
        if (!allPhotos.length) return;
        currentPhotoIndex = (currentPhotoIndex - 1 + allPhotos.length) % allPhotos.length;
        renderLightbox();
        var photo = allPhotos[currentPhotoIndex];
        var img = document.getElementById('lb-img');
        if (photo && img) {
            img.src = photo.src;
        }
    }

    function lbNext() {
        var allPhotos = getPhotos();
        if (!allPhotos.length) return;
        currentPhotoIndex = (currentPhotoIndex + 1) % allPhotos.length;
        renderLightbox();
        var photo = allPhotos[currentPhotoIndex];
        var img = document.getElementById('lb-img');
        if (photo && img) {
            img.src = photo.src;
        }
    }

    // Keyboard controls (Escape to close, Arrow keys to navigate)
    document.addEventListener('keydown', function (e) {
        var lb = document.getElementById('gal-lightbox');
        if (lb && !lb.classList.contains('hidden')) {
            if (e.key === 'Escape') {
                closeLightbox();
            } else if (e.key === 'ArrowLeft') {
                lbPrev();
            } else if (e.key === 'ArrowRight') {
                lbNext();
            }
        }
    });

    window.openLightbox  = openLightbox;
    window.closeLightbox = closeLightbox;
    window.lbPrev        = lbPrev;
    window.lbNext        = lbNext;

    // 3. VIDEO HIGHLIGHTS SLIDER
    function initVideoSlider() {
        var container = document.getElementById('gal-video-slider-container');
        var prevBtn   = document.getElementById('gal-vid-prev');
        var nextBtn   = document.getElementById('gal-vid-next');

        if (!container) return;

        if (prevBtn) {
            prevBtn.addEventListener('click', function () {
                container.scrollBy({ left: -360, behavior: 'smooth' });
            });
        }
        if (nextBtn) {
            nextBtn.addEventListener('click', function () {
                container.scrollBy({ left: 360, behavior: 'smooth' });
            });
        }
    }

    // 4. VIDEO MODAL (HTML5 Video & iFrame Support)
    function openVideoModal(url, title) {
        var vm = document.getElementById('gal-video-modal');
        var iframe = document.getElementById('vid-modal-iframe');
        var player = document.getElementById('vid-modal-player');
        var source = document.getElementById('vid-modal-source');
        var titleEl = document.getElementById('vid-modal-title');

        if (!vm) return;
        if (titleEl) titleEl.textContent = title || 'SUPERNOVA 2025 OFFICIAL AFTERMOVIE';

        if (url && (url.endsWith('.mp4') || url.endsWith('.webm') || url.includes('/assets/videos/'))) {
            if (iframe) { iframe.src = ''; iframe.classList.add('hidden'); }
            if (player && source) {
                source.src = url;
                player.load();
                player.classList.remove('hidden');
                player.play().catch(function () {});
            }
        } else {
            if (player) { player.pause(); player.classList.add('hidden'); }
            if (iframe) {
                iframe.src = url || '';
                iframe.classList.remove('hidden');
            }
        }

        vm.classList.remove('hidden');
        vm.classList.add('flex');
        document.body.style.overflow = 'hidden';
    }

    function closeVideoModal() {
        var vm = document.getElementById('gal-video-modal');
        var iframe = document.getElementById('vid-modal-iframe');
        var player = document.getElementById('vid-modal-player');

        if (!vm) return;
        if (iframe) { iframe.src = ''; iframe.classList.add('hidden'); }
        if (player) { player.pause(); player.classList.add('hidden'); }

        vm.classList.add('hidden');
        vm.classList.remove('flex');
        document.body.style.overflow = '';
    }

    window.openVideoModal  = openVideoModal;
    window.closeVideoModal = closeVideoModal;

    // 5. KEYBOARD NAVIGATION
    document.addEventListener('keydown', function (e) {
        var lb = document.getElementById('gal-lightbox');
        var vm = document.getElementById('gal-video-modal');

        var lbOpen = lb && !lb.classList.contains('hidden');
        var vmOpen = vm && !vm.classList.contains('hidden');

        if (lbOpen) {
            if (e.key === 'Escape')     closeLightbox();
            if (e.key === 'ArrowLeft')  lbPrev();
            if (e.key === 'ArrowRight') lbNext();
        }
        if (vmOpen && e.key === 'Escape') {
            closeVideoModal();
        }
    });

    // INIT ON DOM READY
    document.addEventListener('DOMContentLoaded', function () {
        initVideoSlider();

        if (window.lucide && typeof window.lucide.createIcons === 'function') {
            window.lucide.createIcons();
        }
    });

})();
