<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Video Viewer - {{fileName}}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: var(--vscode-editor-background);
            color: var(--vscode-editor-foreground);
            height: 100vh;
            overflow: hidden;
        }

        .container {
            display: flex;
            flex-direction: column;
            height: 100vh;
            padding: 20px;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid var(--vscode-panel-border);
        }

        .title {
            font-size: 18px;
            font-weight: 600;
        }

        .controls {
            display: flex;
            gap: 10px;
            align-items: center;
            padding: 5px;
            background: var(--vscode-panel-background);
            border-radius: 8px;
            border: 1px solid var(--vscode-panel-border);
            margin-bottom: 20px;
        }

        .file-info {
            display: flex;
            align-items: center;
            gap: 15px;
            font-size: 12px;
            color: var(--vscode-descriptionForeground);
            padding: 5px;
            background: var(--vscode-panel-background);
            border-radius: 6px;
            border: 1px solid var(--vscode-panel-border);
        }

        .file-info-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 2px;
        }

        .file-info-label {
            font-size: 10px;
            opacity: 0.7;
        }

        .file-info-value {
            font-family: 'Monaco', 'Menlo', monospace;
            font-weight: 500;
        }

        .control-group {
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .control-group label {
            font-size: 12px;
            color: var(--vscode-descriptionForeground);
        }

        .btn {
            background: var(--vscode-button-background);
            color: var(--vscode-button-foreground);
            border: none;
            padding: 6px 12px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            transition: background 0.2s;
        }

        .btn:hover {
            background: var(--vscode-button-hoverBackground);
        }

        .btn:disabled {
            background: var(--vscode-button-secondaryBackground);
            color: var(--vscode-button-secondaryForeground);
            cursor: not-allowed;
        }

        .btn.active {
            background: var(--vscode-button-prominentBackground);
            color: var(--vscode-button-prominentForeground);
        }

        .main-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .video-container {
            flex: 1;
            background: var(--vscode-editor-background);
            border: 1px solid var(--vscode-panel-border);
            border-radius: 8px;
            padding: 20px;
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .video-wrapper {
            position: relative;
            transition: transform 0.2s ease;
            transform-origin: center center;
        }

        #video {
            max-width: 100%;
            max-height: 100%;
            border-radius: 8px;
            background: #000;
        }

        .progress-container {
            width: 100%;
            padding: 0 5px;
            display: flex;
            align-items: center;
            gap: 8px;
            flex: 1;
        }

        .progress-bar-wrapper {
            flex: 1;
            height: 8px;
            background: var(--vscode-input-background);
            border-radius: 4px;
            cursor: pointer;
            position: relative;
            overflow: visible;
        }

        .progress-bar-wrapper:hover {
            height: 12px;
        }

        .progress-bar-wrapper:hover .progress-bar-handle {
            opacity: 1;
        }

        .progress-bar-filled {
            height: 100%;
            background: var(--vscode-button-background);
            border-radius: 4px;
            pointer-events: none;
            position: relative;
        }

        .progress-bar-handle {
            width: 14px;
            height: 14px;
            background: var(--vscode-button-foreground);
            border: 2px solid var(--vscode-button-background);
            border-radius: 50%;
            position: absolute;
            right: -7px;
            top: 50%;
            transform: translateY(-50%);
            opacity: 0;
            transition: opacity 0.15s;
            pointer-events: none;
        }

        .progress-bar-wrapper.dragging .progress-bar-handle {
            opacity: 1;
        }

        .zoom-controls {
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .zoom-label {
            font-family: 'Monaco', 'Menlo', monospace;
            font-size: 12px;
            min-width: 40px;
            text-align: center;
            color: var(--vscode-descriptionForeground);
        }

        .loading {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100%;
            font-size: 16px;
            color: var(--vscode-descriptionForeground);
        }

        .error {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100%;
            color: var(--vscode-errorForeground);
            text-align: center;
            padding: 20px;
        }

        .controls {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 5px;
            background: var(--vscode-panel-background);
            border-radius: 8px;
            border: 1px solid var(--vscode-panel-border);
        }

        .time-display {
            font-family: 'Monaco', 'Menlo', monospace;
            font-size: 14px;
            color: var(--vscode-editor-foreground);
            min-width: 120px;
        }

        .volume-control {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .volume-slider {
            width: 80px;
        }

        .speed-control {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .speed-select {
            background: var(--vscode-input-background);
            color: var(--vscode-input-foreground);
            border: 1px solid var(--vscode-input-border);
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <div class="title">🎬 {{fileName}}</div>
        </div>

        <div class="main-content">
            <div class="file-info" id="fileInfo" style="display: none;">
                <div class="file-info-item">
                    <div class="file-info-label">Duration</div>
                    <div class="file-info-value" id="durationInfo">--</div>
                </div>
                <div class="file-info-item">
                    <div class="file-info-label">Resolution</div>
                    <div class="file-info-value" id="resolutionInfo">--</div>
                </div>
                <div class="file-info-item">
                    <div class="file-info-label">Format</div>
                    <div class="file-info-value" id="formatInfo">--</div>
                </div>
                <div class="file-info-item">
                    <div class="file-info-label">File Size</div>
                    <div class="file-info-value" id="fileSizeInfo">--</div>
                </div>
            </div>
            
            <div class="controls">
                <div class="control-group">
                    <button id="playPause" class="btn">▶️</button>
                    <button id="stop" class="btn">⏹️</button>
                </div>
                <div class="control-group">
                    <button id="skipBackward" class="btn">⏪ -10s</button>
                    <button id="skipForward" class="btn">⏩ +10s</button>
                </div>
                <div class="control-group">
                    <label>Speed:</label>
                    <select id="playbackSpeed" class="speed-select">
                        <option value="0.25">0.25x</option>
                        <option value="0.5">0.5x</option>
                        <option value="0.75">0.75x</option>
                        <option value="1" selected>1x</option>
                        <option value="1.25">1.25x</option>
                        <option value="1.5">1.5x</option>
                        <option value="2">2x</option>
                        <option value="4">4x</option>
                    </select>
                </div>
                <div class="zoom-controls">
                    <label>Zoom:</label>
                    <button id="zoomOut" class="btn">−</button>
                    <span id="zoomLabel" class="zoom-label">100%</span>
                    <button id="zoomIn" class="btn">+</button>
                    <button id="zoomFit" class="btn">Fit</button>
                </div>
            </div>
            <div class="controls">
                <span class="time-display" id="currentTime">0:00</span>
                <div class="progress-container">
                    <div class="progress-bar-wrapper" id="progressBar">
                        <div class="progress-bar-filled" id="progressFilled" style="width: 0%;">
                            <div class="progress-bar-handle"></div>
                        </div>
                    </div>
                </div>
                <span class="time-display" id="totalTime">0:00</span>
            </div>
            <div class="video-container" id="videoContainer">
                <div id="loading" class="loading">Loading video...</div>
                <div id="error" class="error" style="display: none;"></div>
                <div id="videoWrapper" class="video-wrapper" style="display: none;">
                    <video id="video">
                        <source src="{{videoSrc}}" type="{{mimeType}}">
                        Your browser does not support the video tag.
                    </video>
                </div>
            </div>


        </div>
    </div>

    <script>
        const vscode = acquireVsCodeApi();
        
        // Video state
        let isPlaying = false;
        let originalDuration = 0;
        let videoFormat = '';
        let fileSize = '';
        let zoomLevel = 1;
        const ZOOM_STEP = 0.25;
        const ZOOM_MIN = 0.25;
        const ZOOM_MAX = 4;
        let isDragging = false;

        // DOM elements
        const video = document.getElementById('video');
        const videoWrapper = document.getElementById('videoWrapper');
        const videoContainer = document.getElementById('videoContainer');
        const loadingDiv = document.getElementById('loading');
        const errorDiv = document.getElementById('error');

        const fileInfoDiv = document.getElementById('fileInfo');
        const durationInfoDiv = document.getElementById('durationInfo');
        const resolutionInfoDiv = document.getElementById('resolutionInfo');
        const formatInfoDiv = document.getElementById('formatInfo');
        const fileSizeInfoDiv = document.getElementById('fileSizeInfo');
        const playPauseBtn = document.getElementById('playPause');
        const stopBtn = document.getElementById('stop');
        const skipBackwardBtn = document.getElementById('skipBackward');
        const skipForwardBtn = document.getElementById('skipForward');
        const playbackSpeedSelect = document.getElementById('playbackSpeed');
        const progressBar = document.getElementById('progressBar');
        const progressFilled = document.getElementById('progressFilled');
        const currentTimeDisplay = document.getElementById('currentTime');
        const totalTimeDisplay = document.getElementById('totalTime');
        const zoomInBtn = document.getElementById('zoomIn');
        const zoomOutBtn = document.getElementById('zoomOut');
        const zoomFitBtn = document.getElementById('zoomFit');
        const zoomLabel = document.getElementById('zoomLabel');

        // Initialize the video viewer
        function initVideoViewer() {
            // Set up event listeners
            setupEventListeners();
            
            // Load video
            video.onloadedmetadata = function() {
                originalDuration = video.duration;
                
                // Get video format from file extension
                const fileName = '{{fileName}}';
                const extension = fileName.split('.').pop().toUpperCase();
                videoFormat = extension;
                
                // Get file size from template variable
                fileSize = '{{fileSize}}';
                
                loadingDiv.style.display = 'none';
                videoWrapper.style.display = 'block';
                fileInfoDiv.style.display = 'flex';
                
                updateFileInfo();
                

                
                vscode.postMessage({ command: 'log', text: 'Video loaded successfully' });
            };
            
            video.onerror = function() {
                loadingDiv.style.display = 'none';
                errorDiv.style.display = 'block';
                errorDiv.textContent = 'Error loading video file';
                vscode.postMessage({ command: 'error', text: 'Failed to load video' });
            };
        }

        function setupEventListeners() {
            // Play/Pause button
            playPauseBtn.addEventListener('click', () => {
                if (isPlaying) {
                    video.pause();
                } else {
                    video.play();
                }
            });

            // Stop button
            stopBtn.addEventListener('click', () => {
                video.pause();
                video.currentTime = 0;
            });

            // Skip controls
            skipBackwardBtn.addEventListener('click', () => {
                video.currentTime = Math.max(0, video.currentTime - 10);
            });

            skipForwardBtn.addEventListener('click', () => {
                video.currentTime = Math.min(video.duration, video.currentTime + 10);
            });

            // Playback speed control
            playbackSpeedSelect.addEventListener('change', (e) => {
                const speed = parseFloat(e.target.value);
                video.playbackRate = speed;
            });

            // Progress bar - click to seek
            progressBar.addEventListener('mousedown', (e) => {
                isDragging = true;
                progressBar.classList.add('dragging');
                seekToPosition(e);
            });

            document.addEventListener('mousemove', (e) => {
                if (isDragging) {
                    seekToPosition(e);
                }
            });

            document.addEventListener('mouseup', () => {
                if (isDragging) {
                    isDragging = false;
                    progressBar.classList.remove('dragging');
                }
            });

            // Zoom controls
            zoomInBtn.addEventListener('click', () => {
                setZoom(Math.min(ZOOM_MAX, zoomLevel + ZOOM_STEP));
            });

            zoomOutBtn.addEventListener('click', () => {
                setZoom(Math.max(ZOOM_MIN, zoomLevel - ZOOM_STEP));
            });

            zoomFitBtn.addEventListener('click', () => {
                setZoom(1);
            });

            // Mouse wheel zoom on video container
            videoContainer.addEventListener('wheel', (e) => {
                if (e.ctrlKey || e.metaKey) {
                    e.preventDefault();
                    const delta = e.deltaY > 0 ? -ZOOM_STEP : ZOOM_STEP;
                    setZoom(Math.max(ZOOM_MIN, Math.min(ZOOM_MAX, zoomLevel + delta)));
                }
            }, { passive: false });

            // Video events
            video.addEventListener('play', () => {
                isPlaying = true;
                playPauseBtn.textContent = '⏸️';
            });

            video.addEventListener('pause', () => {
                isPlaying = false;
                playPauseBtn.textContent = '▶️';
            });

            video.addEventListener('ended', () => {
                isPlaying = false;
                playPauseBtn.textContent = '▶️';
            });

            video.addEventListener('timeupdate', () => {
                if (!isDragging) {
                    updateProgress();
                }
                currentTimeDisplay.textContent = formatTime(video.currentTime);
            });

            video.addEventListener('loadeddata', () => {
                totalTimeDisplay.textContent = formatTime(video.duration);
            });

            // Keyboard shortcuts
            document.addEventListener('keydown', (e) => {
                switch(e.key) {
                    case ' ':
                        e.preventDefault();
                        playPauseBtn.click();
                        break;
                    case 'ArrowLeft':
                        e.preventDefault();
                        skipBackwardBtn.click();
                        break;
                    case 'ArrowRight':
                        e.preventDefault();
                        skipForwardBtn.click();
                        break;
                    case 'Home':
                        e.preventDefault();
                        video.currentTime = 0;
                        break;
                    case 'End':
                        e.preventDefault();
                        video.currentTime = video.duration;
                        break;
                    case '+':
                    case '=':
                        e.preventDefault();
                        zoomInBtn.click();
                        break;
                    case '-':
                        e.preventDefault();
                        zoomOutBtn.click();
                        break;
                    case '0':
                        e.preventDefault();
                        zoomFitBtn.click();
                        break;
                }
            });
        }

        function formatTime(seconds) {
            if (!seconds || isNaN(seconds)) return '0:00';
            const mins = Math.floor(seconds / 60);
            const secs = Math.floor(seconds % 60);
            return `${mins}:${secs.toString().padStart(2, '0')}`;
        }

        function updateProgress() {
            if (video.duration) {
                const percent = (video.currentTime / video.duration) * 100;
                progressFilled.style.width = percent + '%';
            }
        }

        function seekToPosition(e) {
            const rect = progressBar.getBoundingClientRect();
            const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
            const percent = x / rect.width;
            video.currentTime = percent * video.duration;
            progressFilled.style.width = (percent * 100) + '%';
        }

        function setZoom(level) {
            zoomLevel = level;
            videoWrapper.style.transform = `scale(${zoomLevel})`;
            zoomLabel.textContent = Math.round(zoomLevel * 100) + '%';
            // Allow scrolling when zoomed in
            if (zoomLevel > 1) {
                videoContainer.style.overflow = 'auto';
            } else {
                videoContainer.style.overflow = 'hidden';
            }
        }

        function updateFileInfo() {
            const duration = video.duration;
            const width = video.videoWidth;
            const height = video.videoHeight;
            
            if (duration && !isNaN(duration)) {
                const minutes = Math.floor(duration / 60);
                const seconds = Math.floor(duration % 60);
                durationInfoDiv.textContent = `${minutes}:${seconds.toString().padStart(2, '0')}`;
            }
            
            if (width && height) {
                resolutionInfoDiv.textContent = `${width}×${height}`;
            }
            
            formatInfoDiv.textContent = videoFormat;
            fileSizeInfoDiv.textContent = fileSize;
        }

        // Initialize when page loads
        document.addEventListener('DOMContentLoaded', initVideoViewer);

        // Log to VSCode console
        vscode.postMessage({ command: 'log', text: 'Video viewer initialized' });
    </script>
</body>
</html>
