<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PROXIMITY PLAY</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/shaka-player/4.12.5/shaka-player.ui.min.js" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/shaka-player/4.12.5/controls.min.css" crossorigin="anonymous">
    <!--<link rel="stylesheet" href="/ps/ps_data.css" crossorigin="anonymous">       -->
    <style>
        .hidden {
            opacity: 0;
            transition: opacity 0.3s;
        }
        #myModal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            display: none; /* Hidden by default */
            justify-content: center;
            align-items: center;
            z-index: 9999;
        }
        .modal-content {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            text-align: center;
        }
        .modal-buttons button {
            margin: 5px;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .modal-buttons button#joinChannel {
            background-color: #28a745;
            color: white;
        }
        .modal-buttons button#alreadyJoined {
            background-color: #dc3545;
            color: white;
        }
    </style>
</head>
<body>
    <div id="playerContainer" data-shaka-player-container style="position: relative; width: 100%; height: 100%;">
        <video 
            data-shaka-player 
            id="video" 
            style="width: 100%; height: 100%;" 
            autoplay 
           
        </video>
    </div>

    <!-- Modal for Telegram -->
    <div id="myModal" class="modal">
        <div class="modal-content">
            <span class="close" id="closeModal">&times;</span>
            <h2>Join Our Telegram Channel</h2>
            <p>Stay updated with the latest streams and news.</p>
            <div class="modal-buttons">
                <button id="joinChannel">JOIN NOW</button>
                <button id="alreadyJoined">Close</button>
            </div>
        </div>
    </div>

    <script>
    var ConfiguracionCanales = {

            "USANETWORK": {
                url: "https://fsly.stream.peacocktv.com/Content/CMAF_OL1-CTR-4s/Live/channel(usa-west)/master.mpd",
                k1: "252a671825ba31ec8433f978c32ccf6d",
                k2: "ee560759ecc8d2274df2e63fcef56915"
            },
             "WillowExtra": {
                url: "https://cors-anywhere.ammoapps.com/https://dfwlive-v1-c1p3-sponsored.akamaized.net/Content/HLS.cps/Live/channel(WILLXT-3292.dfw.1080)/index.m3u8",
                k1: "1f52934d28a25c51a5dd577a650dcece",
                k2: "323a46b02a31172c2475406e56fd3be6"
            },
            "UNIVERSO": {
                url: "https://fsly.stream.peacocktv.com/Content/CMAF_OL1-CTR-4s/Live/channel(universo-east)/master.mpd",
                k1: "232d71bb013d39ffb92ee8c576fb7e8e",
                k2: "cc5cf3b7928fb9e0a1ee6a8b566f0a8e"
            },
            "ASTRO_FOOTBALL": {
                url: "https://linearjitp-playback.astro.com.my/dash-wv/linear/2506/default_ott.mpd",
                k1: "79f4028730acca9ab8b00f26158ddb10",
                k2: "91febe843c08c7cc523efd827292e40e"
            },
             "WS": {
                url: "https://ottb.live.cf.ww.aiv-cdn.net/iad-nitro/live/clients/dash/enc/f60kqesunw/out/v1/a435ed7a00f947deb4369b46d8f2fb70/cenc.mpd",
                k1: "1779c27b9d077a3ba0c9cc1bb9a94b9f",
                k2: "cc5cf3b7928fb9e0a1ee6a8b566f0a8e"
            }
            
    }
    function getParameterByName(name) {
            name = name.replace(/[\[\]]/g, "\\$&");
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                results = regex.exec(window.location.href);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
        }

    var id = getParameterByName('id');
        var config = ConfiguracionCanales[id];
        console.log(config);
        // Shaka Player Initialization
        document.addEventListener("DOMContentLoaded", async () => {
            try {
                const videoElement = document.getElementById("video");
                const ui = videoElement.ui || new shaka.ui.Overlay(
                    new shaka.Player(videoElement),
                    document.getElementById("playerContainer"),
                    videoElement
                );
                const controls = ui.getControls();
                const player = controls.getPlayer();

                // UI Configuration
                const configuration = {
                    controlPanelElements: ["play_pause", "time_and_duration", "spacer", "quality", "picture_in_picture", "fullscreen"],
                    seekBarColors: {
                        base: "rgba(128, 128, 128, 0.5)",
                        buffered: "rgba(255, 255, 255, 0.7)",
                        played: "rgb(66, 133, 244)"
                    },
                    volumeBarColors: {
                        base: "rgba(66, 133, 244, 0.5)",
                        level: "rgb(t66, 133, 244)"
                    }
                };

                ui.configure(configuration);

                // DRM Configuration
                player.configure({
                    drm: {
                        clearKeys: {
                            [config.k1]:config.k2
                        }
                    }
                });

                // Load Video Stream
                const manifestUrl = config.url;
                await player.load(manifestUrl);
                console.log("Video loaded!");

                // Hide Controls Timer
                const controlsContainer = controls.getControlsContainer();
                let hideControlsTimeout;

                function showControlsTemporarily() {
                    clearTimeout(hideControlsTimeout);
                    controlsContainer.classList.remove("hidden");
                    hideControlsTimeout = setTimeout(() => {
                        controlsContainer.classList.add("hidden");
                    }, 1300);
                }

                document.getElementById("playerContainer").addEventListener("click", showControlsTemporarily);
                controlsContainer.addEventListener("mouseover", showControlsTemporarily);
                controlsContainer.addEventListener("mousemove", showControlsTemporarily);

                showControlsTemporarily();

                player.addEventListener("error", (error) => console.error("Player error:", error));
            } catch (error) {
                console.error("Error:", error);
                alert("Failed to load video: " + error.message);
            }

            // Modal Logic
            const modal = document.getElementById("myModal");
            const closeModal = document.getElementById("closeModal");
            const joinChannel = document.getElementById("joinChannel");
            const alreadyJoined = document.getElementById("alreadyJoined");

            // Open Modal Automatically (Optional: Add a delay)
            modal.style.display = "flex";

            // Close Modal Events
            closeModal.addEventListener("click", () => modal.style.display = "none");
            joinChannel.addEventListener("click", () => {
                window.open("https://t.me/live_cricket_24", "_blank");
                modal.style.display = "none";
            });
            alreadyJoined.addEventListener("click", () => modal.style.display = "none");
        });
    </script>
</body>
</html>
