let Series = new Map();

// ------------------------------------------------------------------------------
//                                   El Encargado                                    
// ------------------------------------------------------------------------------

let ElEncargado = new Map();
ElEncargado.set("El Encargado 1x1", "https://wishonly.site/e/cqkbkzxb8pzk");
ElEncargado.set("El Encargado 1x2", "https://wishonly.site/e/2jtjebntut5i");
ElEncargado.set("El Encargado 1x3", "https://wishonly.site/e/comf1hbizaik");
ElEncargado.set("El Encargado 1x4", "https://wishonly.site/e/0q4eidg86bw6");
ElEncargado.set("El Encargado 1x5", "https://wishonly.site/e/suy33judyli6");
ElEncargado.set("El Encargado 1x6", "https://wishonly.site/e/rbhaigyam1e8");
ElEncargado.set("El Encargado 1x7", "https://wishonly.site/e/qh23rrqfi2gc");
ElEncargado.set("El Encargado 1x8", "https://wishonly.site/e/txq3f048w48i");
ElEncargado.set("El Encargado 1x9", "https://wishonly.site/e/n73dwytq55hd");
ElEncargado.set("El Encargado 1x10", "https://wishonly.site/e/qhthzs6fqv1p");
ElEncargado.set("El Encargado 1x11", "https://wishonly.site/e/m96bi9teos4e");
ElEncargado.set("El Encargado 2x1", "https://wishonly.site/e/i1klvk361fkc");
ElEncargado.set("El Encargado 2x2", "https://wishonly.site/e/h40rr4kt95ys");
ElEncargado.set("El Encargado 2x3", "https://wishonly.site/e/yev775jrvkdp");
ElEncargado.set("El Encargado 2x4", "https://wishonly.site/e/0sptygc4mp7f");
ElEncargado.set("El Encargado 2x5", "https://wishonly.site/e/x3ul6s2nbx55");
ElEncargado.set("El Encargado 2x6", "https://wishonly.site/e/udvcpg7qi6e5");
ElEncargado.set("El Encargado 2x7", "https://wishonly.site/e/2qtfcrtwjju0");
ElEncargado.set("El Encargado 3x1", "https://wishonly.site/e/wkx47nqjvf9q");
ElEncargado.set("El Encargado 3x2", "https://wishonly.site/e/3922raow52ch");
ElEncargado.set("El Encargado 3x3", "https://wishonly.site/e/52yy077l9jt9");
ElEncargado.set("El Encargado 3x4", "https://wishonly.site/e/l1uiyhdb4i1v");
ElEncargado.set("El Encargado 3x5", "https://wishonly.site/e/9x7g4j4ixsps");
ElEncargado.set("El Encargado 3x6", "https://wishonly.site/e/4govlpqlf95q");
ElEncargado.set("El Encargado 3x7", "https://wishonly.site/e/svmvy7cfy2ji");
// Series.set("El Encargado", ElEncargado);

// ------------------------------------------------------------------------------
//                                   Percy Jackson                                    
// ------------------------------------------------------------------------------


let PercyJackson = new Map();
PercyJackson.set("Percy Jackson 1x1", "https://wishonly.site/e/ill4kdlq61w5");
PercyJackson.set("Percy Jackson 1x2", "https://wishonly.site/e/tga0yk0mclzz");
PercyJackson.set("Percy Jackson 1x3", "https://wishonly.site/e/pauxn5386xn4");
PercyJackson.set("Percy Jackson 1x4", "https://wishonly.site/e/bup5drxcehye");
PercyJackson.set("Percy Jackson 1x5", "https://wishonly.site/e/5kvsrby4xvac");
PercyJackson.set("Percy Jackson 1x6", "https://wishonly.site/e/2ulbs02cwvt2");
PercyJackson.set("Percy Jackson 1x7", "https://wishonly.site/e/zpxkh9ntrkz5");
PercyJackson.set("Percy Jackson 1x8", "https://wishonly.site/e/d161d8rfv2xy");
// Series.set("Percy Jackson y los dioses del Olimpo", PercyJackson);

// ------------------------------------------------------------------------------
//                                   Daryl Dixon                                    
// ------------------------------------------------------------------------------

let DarylDixon = new Map();
DarylDixon.set("Daryl Dixon 1x1", "https://hlswish.com/e/9smbc52j6j21");
DarylDixon.set("Daryl Dixon 1x2", "https://hlswish.com/e/eoxkkf7o0sm3");
DarylDixon.set("Daryl Dixon 1x3", "https://hlswish.com/e/j5xws3wm8gv2");
DarylDixon.set("Daryl Dixon 1x4", "https://hlswish.com/e/1qwun2ga2c8v");
DarylDixon.set("Daryl Dixon 1x5", "https://hlswish.com/e/ox1o4gt3kyw5");
DarylDixon.set("Daryl Dixon 1x6", "https://hlswish.com/e/o2d7htpnz7qq");
DarylDixon.set("Daryl Dixon 2x2", "https://hlswish.com/e/1l3o6ouj5n6i");
DarylDixon.set("Daryl Dixon 2x3", "https://hlswish.com/e/zywye1otycap");
DarylDixon.set("Daryl Dixon 2x4", "https://hlswish.com/e/28w295rou5on");
// Series.set("The Walking Dead - Daryl Dixon", DarylDixon);


// ------------------------------------------------------------------------------
//                                   Cromañón                                    
// ------------------------------------------------------------------------------

let Cromanion = new Map();
Cromanion.set("Cromañón 1x1", "https://filemoon.link/e/pugcv0nfmf4j");
Cromanion.set("Cromañón 1x2", "https://filemoon.link/e/kv45qx2onp3c");
Cromanion.set("Cromañón 1x3", "https://filemoon.link/e/81kd947dx095");
Cromanion.set("Cromañón 1x4", "https://filemoon.link/e/x1t4zc1fuasp");
Cromanion.set("Cromañón 1x5", "https://filemoon.link/e/ppg5bqlk84hq");
Cromanion.set("Cromañón 1x6", "https://filemoon.link/e/xczuy0sq6mi2");
Cromanion.set("Cromañón 1x7", "https://filemoon.link/e/qmyrrxma7e15");
Cromanion.set("Cromañón 1x8", "https://filemoon.link/e/95ffa2nurbei");
// Series.set("Cromañón - La noche del incendio", Cromanion);


// ------------------------------------------------------------------------------
//                                   Envidiosa                                    
// ------------------------------------------------------------------------------

let Envidiosa = new Map();
Envidiosa.set("Envidiosa 1x1", "https://hlswish.com/e/kzwssz3n83ec");
Envidiosa.set("Envidiosa 1x2", "https://hlswish.com/e/xdi56nmsigmo");
Envidiosa.set("Envidiosa 1x3", "https://hlswish.com/e/hrdq8rmfi33i");
Envidiosa.set("Envidiosa 1x4", "https://hlswish.com/e/6i0unw167mot");
Envidiosa.set("Envidiosa 1x5", "https://hlswish.com/e/y0hvr2z3tl4r");
Envidiosa.set("Envidiosa 1x6", "https://hlswish.com/e/9zq09hqveeo2");
Envidiosa.set("Envidiosa 1x7", "https://hlswish.com/e/trsow6ydqdik");
Envidiosa.set("Envidiosa 1x8", "https://hlswish.com/e/wfgtgna20dkw");
Envidiosa.set("Envidiosa 1x9", "https://hlswish.com/e/fa3crd4wbryf");
Envidiosa.set("Envidiosa 1x10", "https://hlswish.com/e/42hi9qcjccik");
Envidiosa.set("Envidiosa 1x11", "https://hlswish.com/e/g3s61754u0ko");
Envidiosa.set("Envidiosa 1x12", "https://hlswish.com/e/5v1mf0tvsx5q");
Series.set("Envidiosa", Envidiosa);


// ------------------------------------------------------------------------------
//                                   Halo                                    
// ------------------------------------------------------------------------------


let Halo = new Map();
Halo.set("Halo 1x1", "https://hlswish.com/e/3aybt1cja0l8");
Halo.set("Halo 1x2", "https://hlswish.com/e/6beyuf0c2fp1");
Halo.set("Halo 1x3", "https://hlswish.com/e/6vcwlgn3c6h5");
Halo.set("Halo 1x4", "https://hlswish.com/e/ug5czaqwbkid");
Halo.set("Halo 1x5", "https://hlswish.com/e/7rzt4nzztpo3");
Halo.set("Halo 1x6", "https://hlswish.com/e/1e042s1yeebk");
Halo.set("Halo 1x7", "https://hlswish.com/e/vko0delxa6pz");
Halo.set("Halo 1x8", "https://hlswish.com/e/2ftagvvr8xdw");
Halo.set("Halo 1x9", "https://hlswish.com/e/uwmhsu3nj5hl");
Halo.set("Halo 2x1", "https://hlswish.com/e/599jbodi9wzx");
Halo.set("Halo 2x2", "https://hlswish.com/e/6v0gv14wd49o");
Halo.set("Halo 2x3", "https://hlswish.com/e/zzx6clby5jdq");
Halo.set("Halo 2x4", "https://hlswish.com/e/twgueu3bmi3x");
Halo.set("Halo 2x5", "https://hlswish.com/e/gi5mi416o832");
Halo.set("Halo 2x6", "https://hlswish.com/e/tgj2hoat4tbh");
Halo.set("Halo 2x7", "https://hlswish.com/e/au51a8r2w60m");
Halo.set("Halo 2x8", "https://hlswish.com/e/qmuweg7zsppo");
Series.set("Halo", Halo);

// ------------------------------------------------------------------------------
//                                   Halo                                    
// ------------------------------------------------------------------------------


let BeastGames = new Map();
BeastGames.set("Beast Games 1x1", "https://iplayerhls.com/e/v1snjkvcu0ev");
BeastGames.set("Beast Games 1x2", "https://iplayerhls.com/e/upyxq783twts");
BeastGames.set("Beast Games 1x3", "https://iplayerhls.com/e/0mxspdwuv9u8");
BeastGames.set("Beast Games 1x4", "https://iplayerhls.com/e/vihyi72agtbe");
BeastGames.set("Beast Games 1x5", "https://iplayerhls.com/e/6upo5y5teyz3");
BeastGames.set("Beast Games 1x6", "https://iplayerhls.com/e/731ndz3p4c1a");
BeastGames.set("Beast Games 1x7", "https://iplayerhls.com/e/vnwmnx04z9ib");
BeastGames.set("Beast Games 1x8", "https://iplayerhls.com/e/admggzxo5b3y");
BeastGames.set("Beast Games 1x9", "https://iplayerhls.com/e/52pjk6kui9lb");
BeastGames.set("Beast Games 1x10", "https://goodstream.one/video/embed/4828o8a6mgzp");
// BeastGames.set("Beast Games 1x1", "https://goodstream.one/embed-fncyk9sb11js.html");
// BeastGames.set("Beast Games 1x2", "https://goodstream.one/embed-u0rnxg2g8voe.html");
// BeastGames.set("Beast Games 1x3", "https://iplayerhls.com/e/0mxspdwuv9u8");
// BeastGames.set("Beast Games 1x4", "https://iplayerhls.com/e/vihyi72agtbe");
// BeastGames.set("Beast Games 1x5", "https://iplayerhls.com/e/6upo5y5teyz3");
// BeastGames.set("Beast Games 1x6", "https://iplayerhls.com/e/731ndz3p4c1a");
// BeastGames.set("Beast Games 1x7", "https://iplayerhls.com/e/vnwmnx04z9ib");
// BeastGames.set("Beast Games 1x8", "https://iplayerhls.com/e/admggzxo5b3y");
// BeastGames.set("Beast Games 1x9", "https://iplayerhls.com/e/52pjk6kui9lb");
// BeastGames.set("Beast Games 1x10", "https://goodstream.one/video/embed/4828o8a6mgzp");
Series.set("Beast Games", BeastGames);


let Dark = new Map();
Dark.set("Dark 1x1",  "https://filemoon.sx/e/nw2e2o2vtjn3");
Dark.set("Dark 1x2",  "https://filemoon.sx/e/lpmtcegstybq");
Dark.set("Dark 1x3",  "https://filemoon.sx/e/iepaqofmx9fq");
Dark.set("Dark 1x4",  "https://filemoon.sx/e/9hb0wl17wl6t");
Dark.set("Dark 1x5",  "https://filemoon.sx/e/vtgaievnefom");
Dark.set("Dark 1x6",  "https://filemoon.sx/e/a7kkwend8gwz");
Dark.set("Dark 1x7",  "https://filemoon.sx/e/ajpc05gkl85t");
Dark.set("Dark 1x8",  "https://filemoon.sx/e/ulf022u57hwz");
Dark.set("Dark 1x9",  "https://filemoon.sx/e/ob3i0chc4ynk");
Dark.set("Dark 1x10", "https://filemoon.sx/e/mhhrfkze74c1");
Dark.set("Dark 2x1",  "https://filemoon.sx/e/4gcns8d33wj1");
Dark.set("Dark 2x2",  "https://filemoon.sx/e/ke9zxxul2aye");
Dark.set("Dark 2x3",  "https://filemoon.sx/e/doz9i9xrgjam");
Dark.set("Dark 2x4",  "https://filemoon.sx/e/fzufvtw9b5as");
Dark.set("Dark 2x5",  "https://filemoon.sx/e/wkj5x28phlve");
Dark.set("Dark 2x6",  "https://filemoon.sx/e/x0zaix1l72sw");
Dark.set("Dark 2x7",  "https://filemoon.sx/e/2hrvdbhbwfn5");
Dark.set("Dark 2x8",  "https://filemoon.sx/e/5my63fjjesgi");
Dark.set("Dark 3x1",  "https://filemoon.sx/e/kggu4844ctbz");
Dark.set("Dark 3x2",  "https://filemoon.sx/e/5n8hvahel0lp");
Dark.set("Dark 3x3",  "https://filemoon.sx/e/bhnnxv0wq5hu");
Dark.set("Dark 3x4",  "https://filemoon.sx/e/3x18bbvw93mc");
Dark.set("Dark 3x5",  "https://filemoon.sx/e/lez02up5gzd9");
Dark.set("Dark 3x6",  "https://filemoon.sx/e/av4zm4ydiuq0");
Dark.set("Dark 3x7",  "https://filemoon.sx/e/aatjx4enehg1");
Dark.set("Dark 3x8",  "https://filemoon.sx/e/1jkc754c2bhk");
Series.set("Dark", Dark);

// ------------------------------------------------------------------------------
//                                   SERIES                                    
// ------------------------------------------------------------------------------

let zonaBotones = document.getElementById('id_botones');

for (let canal of Series.keys()) {

    zonaBotones.innerHTML = zonaBotones.innerHTML +
        `<button type="button" title="${canal}" class="card border m-2 p-2 bg-dark bg-gradient col-5 col-md-2 class_CanalBoton" onclick="mostrarCapitulosSerieElegida('${canal}')" >
            <section class="card-body text-light col-12 text-center">
                <h4 class="card-title">${canal}</h4>
            </section>
            <figure>
                <img class="card-img-top w-100" src="../img/Series/${canal}.webp" loading="lazy" alt="${canal}">
            </figure>
        </button>`;

    mostrarScroll()
}

function mostrarScroll(){
    let animado = document.querySelectorAll(".class_CanalBoton");
    let scrollTop = document.documentElement.scrollTop;
    for (let i = 0; i < animado.length; i++) {
        if(animado[i].offsetTop - 700 < scrollTop) {
            animado[i].style.opacity = 1; 
            animado[i].classList.add("mostrarHaciaArriba");
        }else { 
            animado[i].style.opacity = 0;
            animado[i].classList.remove("mostrarHaciaArriba");
        }
        // if(animado[i].offsetTop - 500 < scrollTop) animado[i].style.display = "flex"; else animado[i].style.display = "none";
    }
}

let tele = document.getElementById("id_PantallaTV");
// let aviso = document.getElementById("id_viendo");
let serieElegida = "";
let capituloElegido = "";
let capituloActual = -1;

///Traer el canal y servidor por URL ---------------------------------
const url = new URL(window.location.href); // Obtiene la URL actual
const stream = url.searchParams.get('stream'); // Intenta obtener el parámetro 'stream'
const server = url.searchParams.get('server'); // Intenta obtener el parámetro 'server'

if (stream !== null && stream.trim() !== '') {
    if(Series.has(stream)) serieElegida = stream;
    else { alert("La serie no existe"); window.location.href = "series.html";}
    
    if(server !== null && server.trim() !== ''){
        capituloElegido = server;
        document.getElementById("id_sapping-canal").value = server;
    }else document.getElementById("id_sapping-canal").value = stream;

    // alert("Serie: " + serieElegida + " Capitulo: " + capituloElegido);
    if(serieElegida.length && capituloElegido === "" ) {
        mostrarCapitulosSerieElegida(serieElegida);
    }
    if(serieElegida.length && capituloElegido != "") {
        mostrarCapitulosSerieElegida(serieElegida);
        capituloElegido = server;
        verCapituloDeSerieElegida(capituloElegido);
    }
}
///--------------------------------------------------------------------

function actualizoLink(){
    url.searchParams.set('stream', serieElegida);
    url.searchParams.set('server', capituloElegido);
    window.history.pushState({}, '', url);
}

function mostrarCapitulosSerieElegida(SerieSeleccionada) {
    
    // document.getElementById("id_series").style.display = "none";
    let zonaSeries = document.getElementById('id_series');
    zonaSeries.innerHTML = "";
    zonaSeries.style.display = "flex";
    
    document.getElementById("id_BuscardorSeries").value = "";
    
    serieElegida = SerieSeleccionada;
    if(Series.has(SerieSeleccionada)){
        actualizoLink();
        document.getElementById('id_sapping').style.display = "flex";
        
        if(window.innerWidth < 700) {
            document.getElementById('id_navSeries').style.height = "80px";
            document.getElementById('id_navSeries').style.display = "column";
            
        }else {
            document.getElementById('id_navSeries').style.height = "40px";
            document.getElementById('id_navSeries').style.display = "flex";
        }
        document.getElementById('id_sapping-canal').innerHTML = serieElegida;
        capituloElegido = "";
        capituloActual = -1;

        actualizoLink();


        let serie = Series.get(SerieSeleccionada);

        // Iterar sobre las entradas del mapa
        for (const [key, value] of serie) {
            // console.log(`Episodio: ${key}, URL: ${value}`);
            zonaSeries.innerHTML = zonaSeries.innerHTML +
                `<button type="button" title="${key}" class="card border m-2 p-0 bg-dark bg-gradient col-5 col-md-3 col-lg-2 class_CapituloBoton" onclick="verCapituloDeSerieElegida('${key}')" >
                    <figure>
                        <img class="card-img-top w-100" src="../img/Series/${serieElegida}/${key}.avif" loading="lazy" alt="${key}">
                    </figure>
                    <section class="card-body text-light col-12 text-center">
                        <h4 class="card-title">${key}</h4>
                    </section>
                </button>`;
        }

    }
    

    const element = document.getElementById("id_series");
    //Me envia hasta donde esta el elemento
    if (element) {
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    }
}

function verCapituloDeSerieElegida(capituloElegidodeSerie) {
    if(window.innerWidth < 700) {
        document.getElementById('id_navSeries').style.height = "80px";
        document.getElementById('id_navSeries').style.display = "column";
        
    }else {
        document.getElementById('id_navSeries').style.height = "40px";
        document.getElementById('id_navSeries').style.display = "flex";
    }


    const serieMap = Series.get(serieElegida);
    const keysArray = Array.from(serieMap.keys());
    
    // alert("Serie: " + serieElegida + " Capitulo: " + capituloElegido);
    
    if(serieMap.has(capituloElegidodeSerie)) {
        capituloActual = keysArray.indexOf(capituloElegidodeSerie);
        capituloElegido = capituloElegidodeSerie;

        tele.src = serieMap.get(capituloElegidodeSerie);
        document.querySelectorAll(".class_CapituloBoton")[capituloActual].focus();
        actualizoLink();
        
        document.getElementById('id_sapping-canal').innerHTML = capituloElegido;
        console.log("Serie: "+serieElegida+"\nCapitulo: "+capituloElegidodeSerie+"\nLink: "+tele.src);
    
        tele.style.display = "block";
        // aviso.style.display = "block";
        // aviso.innerHTML = "Estas viendo " + capituloElegidodeSerie;
        const element = document.getElementById("id_PantallaTV");
        //Me envia hasta donde esta el elemento
        if (element) {
            window.scrollTo({
                // top: element.offsetTop,
                top: 0,
                behavior: 'smooth'
            });
        }
    } else {
        alert("El capítulo no existe");
        capituloElegido = "";
        capituloActual = -1;
        actualizoLink();
    }
    
}


function Buscar_Canal() {
    var value = document.getElementById("id_BuscardorSeries").value.toLowerCase();

    let botonesSeries = document.querySelectorAll(".class_CanalBoton");
    for (let i = 0; i < botonesSeries.length; i++) {
        let botones = botonesSeries[i];
        let buscar = botones.title.toLowerCase();
        if (buscar.indexOf(value) > -1) botones.style.display = "flex";
        else botones.style.display = "none";
    }
}


function hacerSapping() {
    const serieMap = Series.get(serieElegida);
    const keysArray = Array.from(serieMap.keys());
    if(capituloActual > keysArray.length-1){
        capituloActual = 0;
    }
    if(capituloActual < 0){
        capituloActual = keysArray.length-1;
    }

    verCapituloDeSerieElegida(keysArray[capituloActual]);
}

function sapping_izquierda() {
    capituloActual--;
    hacerSapping();

}
function sapping_derecha() {
    capituloActual++;
    hacerSapping();
}

function sapping_reset() {
    if(capituloElegido.length){
        document.querySelectorAll(".class_CapituloBoton")[capituloActual].focus();
        verCapituloDeSerieElegida(capituloElegido);
    } 
}


document.addEventListener('keydown', function (event) {
    if (event.ctrlKey && event.shiftKey && event.key === 'i') event.preventDefault();
    if (event.ctrlKey && event.shiftKey && event.key === 'I') event.preventDefault();
    if (event.ctrlKey && event.shiftKey && event.key === 'c') event.preventDefault();
    if (event.ctrlKey && event.shiftKey && event.key === 'C') event.preventDefault();
    if (event.ctrlKey && event.shiftKey && event.key === 'j') event.preventDefault();
    if (event.ctrlKey && event.shiftKey && event.key === 'J') event.preventDefault();
    if (event.ctrlKey && event.key === 'u') event.preventDefault();
    if (event.ctrlKey && event.key === 'U') event.preventDefault();
    if (event.key === 'F12') event.preventDefault();
  
});


document.addEventListener('DOMContentLoaded', function () {
    // Obtener parámetros de la URL
    const urlParams = new URLSearchParams(window.location.search);
    const isApk = urlParams.get('isApk') === 'true';

    // Elementos del modal
    const apkMessage = document.getElementById('apkMessage');
    const chatIframe = document.getElementById('chatIframe');

    // Mostrar mensaje si es APK
    if (isApk) {
        apkMessage.classList.remove('d-none');
        chatIframe.classList.add('d-none');
    }
});


document.getElementById("id_BuscardorSeries").addEventListener("keyup", Buscar_Canal);
document.getElementById("id_BuscardorSeries").addEventListener("click", Buscar_Canal);

document.getElementById("id_sapping-izquierda").addEventListener("click", sapping_izquierda);
document.getElementById("id_sapping-canal").addEventListener("click", sapping_reset);
document.getElementById("id_sapping-derecha").addEventListener("click", sapping_derecha);

window.addEventListener("scroll",mostrarScroll);