<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>EMBED FCLOUD ENCRYPTED</title>
    <script src="https://ssl.p.jwpcdn.com/player/v/8.18.4/jwplayer.js"></script>
    <script type="text/javascript">
        jwplayer.key = "W7zSm81+mmIsg7F+fyHRKhF3ggLkTqtGMhvI92kbqf/ysE99";
    </script>
    <style type="text/css" media="screen">
        html, body { padding: 0; margin: 0; height: 100%; }
        #megaplay { width: 100% !important; height: 100% !important; overflow: hidden; background-color: #000; }

        /* Estilo del botón de transmisión con imagen personalizada */
        .cast-button-container {
            position: absolute;
            top: 10px;
            right: 10px;
            z-index: 1000;
        }

        #castButton {
            width: 60px;
            height: 60px;
            background-image: url('https://i.postimg.cc/hPqVLD1S/55bfe889c5a81.png');
            background-size: cover;
            background-color: transparent;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <!-- Botón de transmitir -->
    <div class="cast-button-container">
        <button id="castButton" onclick="handleLogoClick()"></button>
    </div>

    <div id="megaplay"></div>
    <script type="text/javascript">
        function getQueryParameter(name) {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get(name);
        }

        function fetchVideoUrl(code) {
            const encodedCode = encodeURIComponent(code);
            const url = `https://corsproxy.io/?https://pelistart.42web.io/extractorwish.php?url=${encodedCode}`;
            
            return fetch(url)
                .then(response => response.json())
                .then(data => {
                    if (data && data.link) {
                        return data.link;
                    } else {
                        throw new Error('Video URL not found in the JSON response');
                    }
                })
                .catch(error => {
                    console.error('Error fetching video URL:', error);
                    return null;
                });
        }

        function setupPlayer(videoUrl) {
            var playerInstance = jwplayer("megaplay");
            playerInstance.setup({
                sources: [{
                    file: videoUrl,
                    type: "application/x-mpegURL",
                    label: "Video"
                }],
                aspectratio: "16:9",
                primary: "html5",
                autostart: true,
                preload: "auto"
            });

            playerInstance.on("setupError", function() {
                alert("Server Error! Please contact us to fix it asap. Thank you!");
            });

            playerInstance.on("error", function() {
                alert("Server Error! Please contact us to fix it asap. Thank you!");
            });

            window.playerInstance = playerInstance; // Guardar el reproductor en una variable global
        }

        // Función para manejar el clic en el logo
        function handleLogoClick() {
            const videoURL = window.playerInstance.getPlaylistItem().sources[0].file;
            const secureURL = encodeURIComponent(videoURL) + "&secure_uri=true";
            window.location.href = "wvc-x-callback://open?url=" + secureURL;
            setTimeout(function() {
                window.location.href = "market://launch?id=com.instantbits.cast.webvideo";
            }, 1000);
        }

        const code = getQueryParameter('code');
        if (code) {
            fetchVideoUrl(code).then(videoUrl => {
                if (videoUrl) {
                    setupPlayer(videoUrl);
                } else {
                    console.error('No video URL available.');
                }
            });
        } else {
            console.error('No code parameter found in the URL.');
        }
    </script>
</body>
</html>
