/**
 * HuSNS - 视频播放器模块
 * 处理视频缩略图生成和播放器功能
 */

var HuSNS = window.HuSNS || {};

/**
 * 生成所有视频缩略图
 */
HuSNS.generateVideoThumbnails = function() {
    var videoThumbnails = document.querySelectorAll('.moment-video-thumbnail, .post-video-thumbnail');
    videoThumbnails.forEach(function(container) {
        var videoUrl = container.getAttribute('data-video');
        var posterImg = container.querySelector('.moment-video-poster, .post-video-poster');
        
        if (!videoUrl || !posterImg) return;
        
        var video = document.createElement('video');
        video.src = videoUrl;
        video.crossOrigin = 'anonymous';
        
        video.addEventListener('loadedmetadata', function() {
            video.currentTime = 0.5;
        });
        
        video.addEventListener('seeked', function() {
            var canvas = document.createElement('canvas');
            canvas.width = video.videoWidth || 320;
            canvas.height = video.videoHeight || 180;
            var ctx = canvas.getContext('2d');
            ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
            
            posterImg.src = canvas.toDataURL('image/png');
            posterImg.style.background = 'transparent';
        });
        
        video.addEventListener('error', function() {
            posterImg.src = '';
            posterImg.style.background = '#f0f0f0';
        });
    });
};

/**
 * 打开视频播放器
 * @param {string} videoUrl - 视频URL
 */
HuSNS.openVideoPlayer = function(videoUrl) {
    // 先关闭已存在的播放器
    var existingOverlay = document.querySelector('.video-player-overlay');
    if (existingOverlay) {
        existingOverlay.remove();
    }

    // 创建播放器弹窗
    var overlay = document.createElement('div');
    overlay.className = 'video-player-overlay';
    overlay.innerHTML = 
        '<div class="video-player-container">' +
            '<video class="video-player-fullscreen" src="' + videoUrl + '" controls autoplay playsinline></video>' +
            '<div class="video-player-close" onclick="HuSNS.closeVideoPlayer()">' +
                '<span class="iconify" data-icon="mdi:close" style="color: white; font-size: 24px;"></span>' +
            '</div>' +
        '</div>';
    
    document.body.appendChild(overlay);
    document.body.style.overflow = 'hidden';
    
    // 点击视频切换全屏
    var video = overlay.querySelector('.video-player-fullscreen');
    video.addEventListener('click', function() {
        if (document.fullscreenElement) {
            document.exitFullscreen();
        } else {
            video.requestFullscreen().catch(function(err) {
                console.log('全屏失败:', err);
            });
        }
    });
    
    // ESC键关闭
    var handleEsc = function(e) {
        if (e.key === 'Escape') {
            HuSNS.closeVideoPlayer();
            document.removeEventListener('keydown', handleEsc);
        }
    };
    document.addEventListener('keydown', handleEsc);
};

/**
 * 关闭视频播放器
 */
HuSNS.closeVideoPlayer = function() {
    var overlay = document.querySelector('.video-player-overlay');
    if (overlay) {
        var video = overlay.querySelector('.video-player-fullscreen');
        if (video) {
            video.pause();
        }
        overlay.remove();
        document.body.style.overflow = '';
    }
};

// 页面加载完成后生成视频缩略图
document.addEventListener('DOMContentLoaded', function() {
    HuSNS.generateVideoThumbnails();
});

// 动态列表刷新后调用
window.onMomentsLoaded = function() {
    HuSNS.generateVideoThumbnails();
};

// 全局 openVideoPlayer 函数，供模板调用
window.openVideoPlayer = function(videoUrl) {
    if (window.HuSNS && HuSNS.openVideoPlayer) {
        HuSNS.openVideoPlayer(videoUrl);
    }
};

// 全局暴露
window.HuSNS = HuSNS;