<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Metflix</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; }
    </style>
</head>
<body>
    <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 => {
                    console.log('Raw JSON response:', 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({
                file: videoUrl,
                image: 'https://i.postimg.cc/SSs7KjWS/20240818-144752-1.jpg',  
                title: 'King Metflix!',  
                displaytitle: true,
                aspectratio: "16:9",
                primary: "html5",
                autostart: false,
                preload: "auto"
            });

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

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

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