<!--
Source Code Copyright © 2025 Ringheim Dissolve Media AB.
All rights reserved.
-->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LED Rollup Preview</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400&display=swap');

        body {
            font-family: 'Roboto', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            height: auto;
            min-height: 100vh;
            margin: 0;
            overflow-x: hidden; /* Prevent horizontal scrolling */
            background-color: #ffffff;
            color: #333333;
        }
        body.loading {
            visibility: hidden;
        }
        .logo {
            width: 150px;
            margin: 30px 0 25px 0; /* Padding above and below the logo */
        }
        p.description {
            font-size: 16px;
            color: #797C7F;
            text-align: center;
            margin-bottom: 20px;
            max-width: 600px;
            padding: 0 20px; /* Padding on left and right sides */
        }
        .container {
            position: relative;
            width: 105%;
            max-width: 1500px;
            aspect-ratio: 16 / 9;
            margin-top: 6px;
            margin-bottom: 25px;
        }
        .background {
            position: absolute;
            width: 100%;
            height: 100%;
            background-size: cover;
            background-position: center;
            z-index: 1;
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        .background.loaded {
            opacity: 1;
        }
        .overlay {
            position: absolute;
            width: 100%;
            height: 100%;
            background-size: cover;
            background-position: center;
            pointer-events: none; /* Prevent overlay from interfering with interactions */
            z-index: 3;
        }
        .preview {
            position: absolute;
            overflow: hidden;
            z-index: 2;
        }

        .preview.screen-1 {
            width: 12.5%;
            height: 74.3%;
            top: 11.1%;
            left: 42.3%;
            transform: perspective(800px) rotateX(0deg) rotateY(0deg) scale(0.953);
        }

        .preview.screen-2 {
            width: 25.1%;
            height: 74.3%;
            top: 11.1%;
            left: 36.2%;
            transform: perspective(800px) rotateX(0deg) rotateY(0deg) scale(0.953);
        }

        .preview.screen-3 {
            width: 37.6%;
            height: 74.3%;
            top: 11.1%;
            left: 30.45%;
            transform: perspective(800px) rotateX(0deg) rotateY(0deg) scale(0.953);
        }

        .preview.screen-4 {
            width: 50.4%;
            height: 74.3%;
            top: 11.1%;
            left: 24.3%;
            transform: perspective(800px) rotateX(0deg) rotateY(0deg) scale(0.953);
        }

        .preview.screen-5 {
            width: 63%;
            height: 74.3%;
            top: 11.1%;
            left: 18.7%;
            transform: perspective(800px) rotateX(0deg) rotateY(0deg) scale(0.953);
        }

        .preview.screen-6 {
            width: 75.9%;
            height: 74.3%;
            top: 11.1%;
            left: 12.5%;
            transform: perspective(800px) rotateX(0deg) rotateY(0deg) scale(0.953);
        }

        canvas {
            position: absolute;
            width: 100%;
            height: 100%;
            object-fit: contain; /* Maintain proportions */
        }
        .upload-form {
            text-align: center;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 30px; /* Increased spacing between elements */
        }
        .upload-form input[type="file"] {
            display: none; /* Hide the original file upload button */
        }
        .upload-form button, select {
            margin-top: 10px;
            padding: 10px 20px;
            font-size: 1rem;
            color: #ffffff;
            background-color: #838383;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .upload-form button:hover, select:hover {
            background-color: #4b4b4b;
        }
        .error {
            font-weight: bold;
            color: #d32f2f;
            font-size: 0.9rem;
            margin-top: 10px;
        }
        .change-background {
            margin-top: 0;
            margin-left: 0px;
            padding: 10px 20px;
            font-size: 1rem;
            color: #ffffff;
            background-color: #838383;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            display: inline-block;
        }
        .change-background:hover {
            background-color: #4b4b4b;
        }
        .footer-text {
            font-family: 'Roboto', sans-serif;
            font-size: 14px;
            color: #D2D3D5;
            text-align: center;
            margin-top: 0px;
        }
        select#screenSelector {
            height: 40px; /* Match height to button height */
            padding: 0 10px; /* Add some inner padding for the text */
            font-size: 1rem; /* Match font size */
            border: none; /* Remove default border */
            border-radius: 5px; /* Rounded corners, like the buttons */
            background-color: #838383; /* Same background color as the buttons */
            color: #ffffff; /* Same text color as the buttons */
            cursor: pointer;
        }
        select#screenSelector:hover {
            background-color: #4b4b4b; /* Matching hover effect */
        }
    </style>
</head>
<body class="loading">
    <img src="logo.png" alt="Logo" class="logo">
    <p class="description">Preview your video on an LED Rollup by clicking 'Select Video'. The video must be in MP4 format and have dimensions <span id="dimensions">256 x 768</span> pixels.</p>
    <div class="upload-form">
        <select id="screenSelector">
            <option value="1" selected>1 Screen</option>
            <option value="2">2 Screens</option>
            <option value="3">3 Screens</option>
            <option value="4">4 Screens</option>
            <option value="5">5 Screens</option>
            <option value="6">6 Screens</option>
        </select>
        <button type="button" id="customUploadButton">Select Video</button>
        <input type="file" id="videoUpload" accept="video/mp4">
        <button class="change-background" id="changeBackground">Change Background</button>
        <div id="errorMessage" class="error"></div>
    </div>
    </div>
    <div class="container">
        <div class="background" id="background" style="background-image: url('scene1.jpg'); opacity: 1;"></div>
        <div class="preview screen-1" id="preview">
            <canvas id="videoCanvas"></canvas>
        </div>
        <div class="overlay" id="overlay" style="background-image: url('stroke1.png');"></div>
    </div>
    <div class="footer-text">LED Rollup, BMF Events AB © <span id="currentYear"></span> All Rights Reserved.<br>Unauthorized use or distribution of this code is prohibited.</div>

    <script>
        const videoUpload = document.getElementById("videoUpload");
        const customUploadButton = document.getElementById("customUploadButton");
        const background = document.getElementById("background");
        const overlay = document.getElementById("overlay");
        const changeBackgroundButton = document.getElementById("changeBackground");
        const screenSelector = document.getElementById("screenSelector");
        const errorMessage = document.getElementById("errorMessage");
        const preview = document.getElementById("preview");
        const canvas = document.getElementById("videoCanvas");
        const ctx = canvas.getContext("2d");

        let video = null;
        let isBoothImage = false; // Track current background state

        const screenConfigs = {
            1: { width: 256, height: 768, scene: "scene1.jpg", booth: "booth1.jpg", stroke: "stroke1.png" },
            2: { width: 512, height: 768, scene: "scene2.jpg", booth: "booth2.jpg", stroke: "stroke2.png" },
            3: { width: 768, height: 768, scene: "scene3.jpg", booth: "booth3.jpg", stroke: "stroke3.png" },
            4: { width: 1024, height: 768, scene: "scene4.jpg", booth: "booth4.jpg", stroke: "stroke4.png" },
            5: { width: 1280, height: 768, scene: "scene5.jpg", booth: "booth5.jpg", stroke: "stroke5.png" },
            6: { width: 1536, height: 768, scene: "scene6.jpg", booth: "booth6.jpg", stroke: "stroke6.png" }
        };

        let currentScreen = 1;

        document.getElementById('currentYear').textContent = new Date().getFullYear();

        function preloadMedia(resources, callback) {
            let loadedCount = 0;

            resources.forEach(src => {
                const img = new Image();
                img.src = src;
                img.onload = () => {
                    loadedCount++;
                    if (loadedCount === resources.length) {
                        callback();
                    }
                };
                img.onerror = () => {
                    console.warn(`Error loading: ${src}`);
                    loadedCount++;
                    if (loadedCount === resources.length) {
                        callback();
                    }
                };
            });
        }

        window.addEventListener("DOMContentLoaded", () => {
            const resources = Object.values(screenConfigs).flatMap(config => [
                config.scene,
                config.stroke,
                config.booth
            ]);

            preloadMedia(resources, () => {
                // Once all media are preloaded, show the page
                document.body.classList.remove("loading");
            });
        });

        screenSelector.addEventListener("change", (e) => {
            currentScreen = parseInt(e.target.value);
            const config = screenConfigs[currentScreen];

            background.classList.remove("loaded");
            const imgScene = new Image();
            imgScene.src = config.scene;
            imgScene.onload = () => {
                background.style.backgroundImage = `url('${imgScene.src}')`;
                background.classList.add("loaded");
            };

            overlay.style.backgroundImage = `url('${config.stroke}')`;
            changeBackgroundButton.dataset.boothImage = config.booth;
            document.getElementById("dimensions").textContent = `${config.width} x ${config.height}`;

            preview.className = `preview screen-${currentScreen}`;

            if (video) {
                video.pause();
                video.src = "";
                video.remove();
                video = null;
            }

            errorMessage.textContent = "";
            videoUpload.value = "";
            ctx.clearRect(0, 0, canvas.width, canvas.height);
        });

        customUploadButton.addEventListener("click", () => {
            videoUpload.click();
        });

        changeBackgroundButton.addEventListener("click", () => {
            const config = screenConfigs[currentScreen];
            const newBackground = isBoothImage ? config.scene : config.booth;
            const img = new Image();
            img.src = newBackground;
            img.onload = () => {
                background.style.backgroundImage = `url('${img.src}')`;
                isBoothImage = !isBoothImage; // Toggle the state
            };
        });

videoUpload.addEventListener("change", (event) => {
    const file = event.target.files[0];
    if (file) {
        const config = screenConfigs[currentScreen];
        const videoElement = document.createElement("video");

        videoElement.src = URL.createObjectURL(file);
        videoElement.loop = true; // Loop the video
        videoElement.addEventListener("loadedmetadata", () => {
            if (
                videoElement.videoWidth === config.width &&
                videoElement.videoHeight === config.height
            ) {
                errorMessage.textContent = "";
                video = videoElement;
                syncCanvasWithPreview();
                renderVideoWithLowResolution(video, canvas, ctx, config.width, config.height);
            } else {
                errorMessage.textContent = `Error: The video must have dimensions ${config.width} x ${config.height} pixels.`;
            }
        });
    }
});
        function syncCanvasWithPreview() {
            canvas.width = preview.clientWidth;
            canvas.height = preview.clientHeight;
        }

        function renderVideoWithLowResolution(videoElement, canvas, ctx, width, height) {
            function drawFrame() {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                ctx.drawImage(videoElement, 0, 0, width, height, 0, 0, canvas.width, canvas.height);
                requestAnimationFrame(drawFrame);
            }

            videoElement.play();
            drawFrame();
        }
    </script>
</body>
</html>
