<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Reproductor Acestream by TokyoGhoull</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
        #status { color: green; margin: 10px 0; }
        #error { color: red; margin: 10px 0; }
        .button { background-color: #4CAF50; color: white; padding: 8px; cursor: pointer; border: none; }
        .button:hover { background-color: #45a049; }
        .result-item { margin: 5px 0; }
        .link { display: block; margin: 5px 0; color: #0066cc; text-decoration: none; }
        .input-group { margin: 15px 0; }
        .input-label { display: block; margin-bottom: 5px; font-weight: bold; }
    </style>
</head>
<body>
    <h1>Reproductor Acestream by TokyoGhoull</h1>
    
    <div class="input-group">
        <div class="input-label">Reproducir desde Infohash</div>
        <input type="text" id="infohash-input" placeholder="Ingresa el infohash" size="40">
        <button class="button" onclick="fetchContentId()">Reproducir desde Hash</button>
    </div>

    <div class="input-group">
        <div class="input-label">Reproducir desde ID</div>
        <input type="text" id="content-id-input" placeholder="Ingresa el ID directamente" size="40">
        <button class="button" onclick="playDirectContent()">Reproducir desde ID</button>
    </div>
    
    <div id="status"></div>
    <div id="error"></div>
    <div id="players-list"></div>

    <script>
        async function fetchContentId() {
            const infohash = document.getElementById("infohash-input").value.trim();
            if (!infohash) {
                showError("Por favor, ingresa un infohash válido.");
                return;
            }

            const url = `http://127.0.0.1:6878/server/api?api_version=3&method=get_content_id&infohash=${infohash}`;
            try {
                const response = await fetch(url);
                const data = await response.json();

                if (data.error || !data.result?.content_id) {
                    showError("No se pudo obtener el content_id para el infohash proporcionado.");
                    return;
                }

                const contentId = data.result.content_id;
                showStatus("Content ID obtenido: " + contentId);
                fetchAvailablePlayers(contentId);
            } catch (error) {
                showError("Error al obtener el content_id: " + error.message);
            }
        }

        async function playDirectContent() {
            const contentId = document.getElementById("content-id-input").value.trim();
            if (!contentId) {
                showError("Por favor, ingresa un ID válido.");
                return;
            }
            
            showStatus("Usando ID directo: " + contentId);
            fetchAvailablePlayers(contentId);
        }

        async function fetchAvailablePlayers(contentId) {
            const url = `http://127.0.0.1:6878/server/api?api_version=3&method=get_available_players&content_id=${contentId}`;
            try {
                const response = await fetch(url);
                const data = await response.json();

                if (data.error || !data.result?.players?.length) {
                    showNoPlayersAvailable(contentId);
                    return;
                }

                const players = data.result.players;
                displayPlayers(players, contentId);
            } catch (error) {
                showError("Error al obtener reproductores: " + error.message);
            }
        }

        function displayPlayers(players, contentId) {
            const playersDiv = document.getElementById("players-list");
            playersDiv.innerHTML = "<h2>Reproductores Disponibles</h2>";

            players.forEach(player => {
                const playerDiv = document.createElement("div");
                playerDiv.className = "result-item";
                playerDiv.innerHTML = `
                    <span>${player.name}</span>
                    <button class="button" onclick="openInPlayer('${contentId}', '${player.id}')">Abrir en ${player.name}</button>
                `;
                playersDiv.appendChild(playerDiv);
            });
        }

        function showNoPlayersAvailable(contentId) {
            const playersDiv = document.getElementById("players-list");
            playersDiv.innerHTML = `
                <h2>No se encontraron reproductores disponibles</h2>
                <a href="acestream://${contentId}" class="link" target="_blank">acestream://${contentId}</a>
                <a href="http://127.0.0.1:6878/ace/getstream?id=${contentId}" class="link" target="_blank">http://127.0.0.1:6878/ace/getstream?id=${contentId}</a>
            `;
        }

        async function openInPlayer(contentId, playerId) {
            const url = `http://127.0.0.1:6878/server/api?method=open_in_player&player_id=${playerId}&content_id=${contentId}`;
            try {
                const response = await fetch(url);
                const data = await response.json();

                if (!data.error) {
                    showStatus("Contenido abierto en el reproductor " + playerId);
                } else {
                    showError("Error al abrir el contenido en el reproductor.");
                }
            } catch (error) {
                showError("Error al abrir en el reproductor: " + error.message);
            }
        }

        function showStatus(message) {
            document.getElementById("status").textContent = message;
            document.getElementById("error").textContent = "";
        }

        function showError(message) {
            document.getElementById("error").textContent = message;
            document.getElementById("status").textContent = "";
        }
    </script>
</body>
</html>