const Sounds = {
    coinpickup : new Audio("sounds/pickupCoin.wav"),
    jump_sound : new Audio("sounds/jump.wav"),
    explosion : new Audio("sounds/explosion.wav"),
    win : new Audio("sounds/victory_sJDDywi.mp3"),
    lost : new Audio("sounds/ps2_start_up.mp3"),
    click : new Audio("sounds/click.wav")
}

const playerwidth = 98;
const playerstart = 1;

var base = 200;
var playercorr;
var speed = 10;
var velocity = 15;
var y0 = base;
var v0 = 0;
var gravity = 1;
var moving = false;
var salto;
var fine = false;
var id_interval,id;
var punt = 0;
var pausa = false;

document.addEventListener('DOMContentLoaded', e => {
    if(sessionStorage.getItem("current") != null)
        playercorr = JSON.parse(sessionStorage.getItem("current"));
    else
    {
        alert("BIG ERRORE. ESPLOSIONE IN CORSO...");
        location.href = "LoginPage.html";
    }
});

function Inizio()
{
    punt = 0;
    var player = document.createElement("div");
    var pointdisplay = document.createElement("div");
    pointdisplay.id = "display_punti";
    pointdisplay.innerHTML = "<h1>Punteggio: <span>"+punt+"</span></h1>";

    player.id = "player";
    player.style.transform = "rotateY(0deg)";
    player.style.bottom = base + "px";
    player.style.left = PosizionaCentroPagina() + "px";
    player.style.backgroundImage = playercorr.skin;

    document.getElementById("background").style.left = "0px";

    document.addEventListener("keydown", Muoviti);

    document.addEventListener("keydown", JumpJump);

    document.addEventListener("keydown", Impostazioni);

    document.addEventListener("keyup", (e) => {
        if(e.key == "a" || e.key == "d" || e.key == 'ArrowRight' || e.key == 'ArrowLeft')
        {
            clearInterval(id);
            moving = false;
            console.log('fermo ${e.key}')
        }
    });

    document.body.appendChild(player);
    document.body.appendChild(pointdisplay);
    LeggiLivelloProf(1);
}

function PosizionaCentroPagina()
{
    return Math.floor((parseInt(window.innerWidth) - 100) / 2) - (Math.floor((parseInt(window.innerWidth) - 100) / 2) % 5);
}

function Muoviti(e)
{
    if(e.key == "d" || e.key == 'ArrowRight' || e.key == "D") 
    {
        if(!moving) {
            var p = document.getElementById("player");
            if(p.style.transform != "rotateY(0deg)")
                p.style.transform = "rotateY(0deg)";
            moving = true;
            id = setInterval(Muovi, 10, "d");
        }
    }
    else if(e.key == "a" || e.key=='ArrowLeft' || e.key == "A")
    {
        if(!moving) {
            var p = document.getElementById("player");
            if(p.style.transform != "rotateY(180deg)")
                p.style.transform = "rotateY(180deg)";
            moving = true;
            id = setInterval(Muovi, 10, "a");
        }
    }
}

function JumpJump(e)
{
    if(e.key == "w" || e.key == " " || e.key=='ArrowUp') {
        if(!salto)
        {
            Sounds.jump_sound.currentTime = 0;
            Sounds.jump_sound.play();
            velocity=16;
            v0=16;
            y0=parseInt(document.getElementById("player").style.bottom);
            Jump();
        }
    }
}

function Jump() {
    pl = document.getElementById("player");

    pl.style.bottom = (parseInt(pl.style.bottom) + velocity) + 'px';

    velocity -= gravity;

    MonetaMoneta();

    if((parseInt(pl.style.bottom) >= y0 && !OstacoloSotto()) || velocity > 0)
    {
        salto = true;
        y0 = OstacoloSottoPos();
        requestAnimationFrame(Jump);
    }
    else
    {
        cancelAnimationFrame(Jump);
        y0 = OstacoloSottoPos();
        pl.style.bottom = y0 + "px";
        velocity = v0;
        salto = false;
    }
    
}


function Muovi(dir)
{
    var pav = document.getElementsByClassName("pavimento");
    var ost = document.getElementsByClassName("movibile");
    var ostacoli = document.getElementsByClassName("ostacolo");

    if(dir == "d") {
        if(!PlayerOstacolatoDX(ostacoli)) {
            PavimentiNuovi(true, pav);
            MuoviDX(ost,5);
            Gravita();
            MonetaMoneta();
            IlPiantaMina();
        }
    }
    else {
        if(!PlayerOstacolatoSX(ostacoli)) {
            PavimentiNuovi(false, pav)
            MuoviSX(ost,5);
            Gravita();
            MonetaMoneta();
            IlPiantaMina();
        }
    }
}

function PavimentiNuovi(dx,pav) {
    if(dx) {
        for(let i=0; i<pav.length; i++) {
            pav[i].style.left = (parseInt(pav[i].style.left)-5)+"px";
            if(parseInt(pav[i].style.left) < 0 && pav.length < 2)
                CreaPavimento(parseInt(pav[i].style.left) + 3000);
            if(parseInt(pav[i].style.left) < -3000)
                document.body.removeChild(pav[i]);
        }
    }
    else {
        for(let i=0; i<pav.length; i++) {
            pav[i].style.left = (parseInt(pav[i].style.left)+5)+"px";
            if(parseInt(pav[i].style.left) >= 0 && pav.length < 2)
                CreaPavimento(parseInt(pav[i].style.left)-3000);
            if(parseInt(pav[i].style.left) > parseInt(window.innerWidth))
                document.body.removeChild(pav[i]);
        }
    }
}

function MuoviDX(ost,step) {
    for(let i=0; i<ost.length; i++)
        ost[i].style.left = (parseInt(ost[i].style.left)-step)+"px";
    //var b = document.getElementById("background");
    //b.style.left = (parseInt(b.style.left) - 1) + "px";
}

function MuoviSX(ost,step) {
    for(let i=0;i<ost.length;i++)
        ost[i].style.left=(parseInt(ost[i].style.left)+step)+"px";

    //var b = document.getElementById("background");
    //b.style.left = (parseInt(b.style.left) + 1) + "px";
}

function Gravita() {
    if(!OstacoloSotto() && y0 > base && !salto)
    {
        v0=0;
        velocity=0;
        y0-=100;
        Jump();
    }
}

function PlayerOstacolatoDX(ost)
{
    var player = document.getElementById("player");
    x = parseInt(player.style.left) + playerstart;
    x2 = parseInt(player.style.left) + playerwidth;
    yp = parseInt(player.style.bottom);

    for(let i=0;i<ost.length;i++) {
        xost = parseInt(ost[i].style.left);
        xost2 = parseInt(ost[i].style.left) + 100;
        yost = parseInt(ost[i].style.bottom);
        yost2 = parseInt(ost[i].style.bottom) + 100;

        if((x2 >= xost && x2 < xost2) 
        &&((yost < yp && yp < yost2) 
        || (yost > yp && yp > yost2)
        || (yost == yp && yost2 == yp + 100))) {
            return ost[i];
        }
    }
    return false;
}

function PlayerOstacolatoSX(ost)
{
    var player = document.getElementById("player");
    x = parseInt(player.style.left) + playerstart;
    x2 = parseInt(player.style.left) + playerwidth;
    yp = parseInt(player.style.bottom);

    for(let i=0;i<ost.length;i++) {
        xost = parseInt(ost[i].style.left) + 100;
        yost = parseInt(ost[i].style.bottom);
        yost2 = parseInt(ost[i].style.bottom) + 100;

        if((xost > x && xost < x2)
        &&((yost < yp && yp < yost2) 
        || (yost > yp && yp > yost2)
        || (yost == yp && yost2 == yp + 100))) {
            return ost[i];
        }
    }
    return false;
}

function OstacoloSotto()
{
    var ost = document.getElementsByClassName("ostacolo");
    var player = document.getElementById("player");
    var a = parseInt(player.style.left) + playerstart + 10;
    var a1 = parseInt(player.style.left) + playerwidth - 10;
    var y = parseInt(player.style.bottom);

    for(let i=0;i<ost.length;i++)
    {
        var b = parseInt(ost[i].style.left);
        var b1 = parseInt(ost[i].style.left) + 100;
        var y1 = parseInt(ost[i].style.bottom);

        if(((a > b && a < b1) || 
            (a1 > b && a1 < b1) ||
            (a < b && a1 > b1) ||
            (b < a && b1 > a1)) && 
            (y <= y1 + 100))
            return true;
    }

    return false;
}

function OstacoloSottoPos()
{
    var ost = document.getElementsByClassName("ostacolo");
    var player = document.getElementById("player");
    var a = parseInt(player.style.left) + playerstart + 10;
    var a1 = parseInt(player.style.left) + playerwidth - 10;
    var y = parseInt(player.style.bottom);

    for(let i=0;i<ost.length;i++)
    {
        var b = parseInt(ost[i].style.left);
        var b1 = parseInt(ost[i].style.left) + 100;
        var y1 = parseInt(ost[i].style.bottom);

        if(((a > b && a < b1) ||
            (a1 > b && a1 < b1) ||
            (a < b && a1 > b1) ||
            (b < a && b1 > a1)) &&
            (y <= y1 + 100))
            return parseInt(ost[i].style.bottom)+100;
    }
    return base;
}

function CreaPavimentoInizio()
{
    var div=document.createElement("div");
    var grass=document.createElement("div");

    div.className="pavimento";
    div.appendChild(grass);
    div.style.left="0px";
    div.style.bottom="0px";
    div.style.height = base + "px";

    document.body.appendChild(div);
}

function CreaPavimento(x)
{
    var div=document.createElement("div");
    var grass=document.createElement("div");

    div.className="pavimento";
    div.appendChild(grass);
    div.style.left=x+"px";
    div.style.bottom="0px";
    div.style.height = base + "px";


    document.body.appendChild(div);
}

function LeggiLivelloProf(n)
{
    var urlo;
    if(n == 0 || n == 1)
        urlo="https://www.apibaobab.com/api/v1/Scuola/super-mario/level/"+n;
    else
        urlo="https://www.apibaobab.com/api/v1/Scuola/super-mario/level/0";

	const levelJson0 = [{"X":25,"Y":25,"Type":"wall"},{"X":100,"Y":25,"Type":"wall"},{"X":125,"Y":25,"Type":"wall"},{"X":125,"Y":50,"Type":"wall"},{"X":150,"Y":25,"Type":"wall"},{"X":300,"Y":25,"Type":"wall"},{"X":325,"Y":25,"Type":"wall"},{"X":325,"Y":50,"Type":"wall"},{"X":350,"Y":25,"Type":"wall"},{"X":450,"Y":25,"Type":"wall"},{"X":525,"Y":25,"Type":"wall"},{"X":600,"Y":25,"Type":"wall"},{"X":625,"Y":25,"Type":"wall"},{"X":625,"Y":50,"Type":"wall"},{"X":750,"Y":25,"Type":"wall"},{"X":900,"Y":25,"Type":"coin"},{"X":1000,"Y":25,"Type":"wall"},{"X":1025,"Y":25,"Type":"wall"},{"X":1025,"Y":50,"Type":"wall"},{"X":1050,"Y":25,"Type":"wall"},{"X":1100,"Y":25,"Type":"wall"},{"X":1100,"Y":50,"Type":"end_flag"}];
	const levelJson1 = [{"X":25,"Y":25,"Type":"wall"},{"X":100,"Y":25,"Type":"wall"},{"X":125,"Y":25,"Type":"wall"},{"X":125,"Y":50,"Type":"wall"},{"X":125,"Y":75,"Type":"coin"},{"X":150,"Y":25,"Type":"wall"},{"X":175,"Y":25,"Type":"plant"},{"X":200,"Y":25,"Type":"coin"},{"X":300,"Y":25,"Type":"wall"},{"X":325,"Y":25,"Type":"wall"},{"X":325,"Y":50,"Type":"wall"},{"X":350,"Y":25,"Type":"wall"},{"X":350,"Y":50,"Type":"plant"},{"X":400,"Y":25,"Type":"coin"},{"X":450,"Y":25,"Type":"wall"},{"X":525,"Y":25,"Type":"wall"},{"X":600,"Y":25,"Type":"wall"},{"X":600,"Y":100,"Type":"coin"},{"X":625,"Y":25,"Type":"wall"},{"X":625,"Y":50,"Type":"wall"},{"X":725,"Y":75,"Type":"coin"},{"X":750,"Y":25,"Type":"wall"},{"X":875,"Y":25,"Type":"plant"},{"X":900,"Y":25,"Type":"coin"},{"X":1000,"Y":25,"Type":"wall"},{"X":1025,"Y":25,"Type":"wall"},{"X":1025,"Y":50,"Type":"wall"},{"X":1050,"Y":25,"Type":"wall"},{"X":1050,"Y":50,"Type":"plant"},{"X":1100,"Y":25,"Type":"coin"},{"X":1100,"Y":25,"Type":"wall"},{"X":1100,"Y":50,"Type":"end_flag"}]
	const levelJson2 = [{"X":25,"Y":25,"Type":"wall"},{"X":50,"Y":50,"Type":"wall"},{"X":75,"Y":75,"Type":"wall"},{"X":100,"Y":75,"Type":"coin"},{"X":125,"Y":25,"Type":"plant"},{"X":175,"Y":50,"Type":"wall"},{"X":200,"Y":25,"Type":"wall"},{"X":225,"Y":25,"Type":"coin"},{"X":250,"Y":25,"Type":"wall"},{"X":250,"Y":50,"Type":"plant"},{"X":300,"Y":25,"Type":"wall"},{"X":325,"Y":25,"Type":"wall"},{"X":350,"Y":25,"Type":"wall"},{"X":375,"Y":25,"Type":"coin"},{"X":400,"Y":25,"Type":"wall"},{"X":450,"Y":25,"Type":"wall"},{"X":475,"Y":50,"Type":"wall"},{"X":500,"Y":75,"Type":"wall"},{"X":525,"Y":75,"Type":"coin"},{"X":550,"Y":25,"Type":"wall"},{"X":550,"Y":50,"Type":"plant"},{"X":600,"Y":50,"Type":"coin"},{"X":625,"Y":50,"Type":"coin"},{"X":650,"Y":50,"Type":"coin"},{"X":700,"Y":25,"Type":"plant"},{"X":750,"Y":25,"Type":"wall"},{"X":775,"Y":50,"Type":"wall"},{"X":800,"Y":75,"Type":"wall"},{"X":825,"Y":100,"Type":"wall"},{"X":850,"Y":75,"Type":"coin"},{"X":850,"Y":100,"Type":"coin"},{"X":875,"Y":25,"Type":"wall"},{"X":875,"Y":50,"Type":"wall"},{"X":875,"Y":75,"Type":"plant"},{"X":925,"Y":75,"Type":"wall"},{"X":950,"Y":50,"Type":"wall"},{"X":975,"Y":25,"Type":"wall"},{"X":1025,"Y":25,"Type":"coin"},{"X":1050,"Y":25,"Type":"coin"},{"X":1075,"Y":25,"Type":"coin"},{"X":1100,"Y":25,"Type":"wall"},{"X":1100,"Y":50,"Type":"plant"},{"X":1150,"Y":25,"Type":"end_flag"}];
	
	var y;
	switch(Math.floor(Math.random()*3))
	{
		case 0:y= levelJson0;
		break;
		case 1:y= levelJson1;
		break;
		case 2:y= levelJson2;
	break;
	}

	for(let i=0;i<y.length;i++){		
		GeneraOstacolo((((y[i].X * 100) / 25) + 1200), ((((y[i].Y - 25) * 100) / 25) + base), y[i].Type);
	}

    /*fetch(urlo)
        .then(x=>x.json())
        .then(y=>{            
            for(let i=0;i<y.length;i++)
                GeneraOstacolo((((y[i].X * 100) / 25) + 1200), ((((y[i].Y - 25) * 100) / 25) + base), y[i].Type);
        })
        .catch(error=>{
            console.error("errore megagalattico incredibile esplodi sistema: ", error);
        })*/
}

function GeneraOstacolo(x,y,type)
{
    var div=document.createElement("div");

    if(type != "wall")
        div.classList.add(type, "movibile");
    else
        div.classList.add(type, "ostacolo", "movibile");

    div.style.bottom = y + "px";
    div.style.left = x + "px";

    document.body.appendChild(div);
}

function MonetaMoneta()
{
    var monete = document.getElementsByClassName("coin");
    var pl = document.getElementById("player");
    var p = document.getElementById("display_punti");
    var a = parseInt(pl.style.left) + playerstart;
    var a1 = parseInt(pl.style.left) + playerwidth;
    var y = parseInt(pl.style.bottom);
    var b,b1,y1;

    for(let i = 0; i < monete.length; i++)
    {
        b = parseInt(monete[i].style.left)+20;
        b1 = b + 60;
        y1 = parseInt(monete[i].style.bottom)+20;

        if ((a < b1 && a1 > b) && (y < (y1 + 60) && (y + 100) > y1))
        {
            document.body.removeChild(monete[i]);
            Sounds.coinpickup.play();
            punt++;
            p.getElementsByTagName("h1")[0].getElementsByTagName("span")[0].innerHTML = punt;
        }
    }
}

function IlPiantaMina()
{
    var pericoli = document.getElementsByClassName("plant");
    var pl = document.getElementById("player");
    var a = parseInt(pl.style.left) + playerstart;
    var a1 = parseInt(pl.style.left) + playerwidth;
    var y = parseInt(pl.style.bottom);

    for(let i = 0; i < pericoli.length; i++)
    {
        b = parseInt(pericoli[i].style.left) + 18;
        b1 = b + 64;
        y1 = parseInt(pericoli[i].style.bottom);

        if((a < b1 && a1 > b) && y == y1)
            return true;
    }
    return false;
}

function Vittoria()
{
    var flag = document.getElementsByClassName("end_flag")[0];
    var pl = document.getElementById("player");
    var a, a1, b, b1, y, y1;

    if(flag != null && pl != null)
    {
        a = parseInt(pl.style.left) + playerstart;
        b = parseInt(flag.style.left);
        y = parseInt(pl.style.bottom);
        y1 = parseInt(flag.style.bottom);
        
        a1 = parseInt(pl.style.left) + playerwidth;
        b1 = b + 100;

        if((a < b1 && a1 > b) && y == y1)
            return true;
        else
            return false;
    }
}

function Controlla()
{
    if(IlPiantaMina() && !fine) {
        fine = true;
        clearInterval(id_interval);
        Sounds.explosion.play();
        setTimeout(Fine,100,false);
    }
    else if(Vittoria() && !fine) {
        fine = true;
        clearInterval(id_interval);
        setTimeout(Fine,100,true);
    }
}

function Fine(op)
{
    var copertura = document.createElement("div");
    var popup = document.createElement("div");
    var riavvia_button = document.createElement("button");
    var menu_button = document.createElement("button");
    copertura.id = "copertura";
    popup.id = "popup";

    riavvia_button.innerHTML = "Riavvia";
    menu_button.innerHTML = "Menu";

    riavvia_button.addEventListener("click",e => {
        CancellaGioco();
        document.body.removeChild(document.getElementById("popup"));
        document.body.removeChild(document.getElementById("copertura"));
        IniziaGioco();
        if(op) {
            Sounds.win.pause();
            Sounds.win.currentTime = 0;
        } else {
            Sounds.lost.pause();
            Sounds.lost.currentTime = 0;
        }
    });

    menu_button.addEventListener("click",e => {
        CancellaGioco();
        document.body.removeChild(document.getElementById("popup"));
        document.body.removeChild(document.getElementById("copertura"));
        CreaMenu();
        if(op) {
            Sounds.win.pause();
            Sounds.win.currentTime = 0;
        } else {
            Sounds.lost.pause();
            Sounds.lost.currentTime = 0;
        }
    });

    if(op) {
        //cose vittoria
        Sounds.win.play();
        popup.innerHTML = "<h1>Hai Vinto!</h1><br><p>punteggio tot: "+punt+"</p>";
        if(punt > playercorr.punteggio) {
            popup.innerHTML += "<p><br>Nuovo record!</p>";
            playercorr.punteggio = punt;
            ModificaRecords();
        }
        console.log("win");
    }
    else {
        //cose perdita
        Sounds.lost.play();
        popup.innerHTML = "<h1>Hai perso.</h1><br><p>riprova la prossima volta</p>";
        console.log("lost");
    }

    popup.appendChild(riavvia_button);
    popup.appendChild(menu_button);

    document.body.appendChild(copertura);
    document.body.appendChild(popup);
}

function IniziaGioco()
{
    CreaPavimentoInizio();
    Inizio();
    setTimeout(e => {
        id_interval = setInterval(Controlla,10);
    },1000)
}

function RimuoviMenu()
{
    var menu = document.getElementById("menu");
    var opacita = document.getElementById("opacita");

    document.body.removeChild(menu);
    document.body.removeChild(document.getElementById("bottone_record"));
    document.body.removeChild(opacita);
}

function CreaMenu()
{
    var opacita = document.createElement("div");
    var menu = document.createElement("div");
    menu.id = "menu";
    menu.innerHTML = "<span>Somario</span>";
    opacita.id = "opacita";

    var bottone_record = document.createElement("button");
    var bottone_inizio = document.createElement("button");
    var bottone_pingu = document.createElement("button");
    var bottone_esci = document.createElement("button");

    bottone_record.innerHTML = "Record<br>Board";
    bottone_inizio.innerHTML = "Inizia";
    bottone_pingu.innerHTML = "Pinguinos";
    bottone_esci.innerHTML = "Esci";

    bottone_record.addEventListener("click", e => location.href = "Scoreboard.html");
    bottone_record.id = "bottone_record";

    bottone_inizio.addEventListener("click", e => {
        Sounds.click.volume = 1;
        Sounds.click.play();
        RimuoviMenu();
        IniziaGioco();
    });

    bottone_pingu.addEventListener("click", e => {
        Sounds.click.volume = 1;
        Sounds.click.play();
        RimuoviMenu();
        SceltaPingu();
    });

    bottone_esci.addEventListener("click", e => {
        Sounds.click.volume = 1;
        Sounds.click.play();
        location.href="LoginPage.html";
    });

    menu.appendChild(bottone_inizio);
    menu.appendChild(bottone_pingu);
    menu.appendChild(bottone_esci);

    document.body.appendChild(menu);
    document.body.appendChild(bottone_record);
    document.body.appendChild(opacita);
}

function CancellaGioco()
{
    var movibile = document.getElementsByClassName("movibile");
    var pavimenti = document.getElementsByClassName("pavimento");
    var player = document.getElementById("player");
    var p = document.getElementById("display_punti");
    var movibile_length = movibile.length;
    var pavimenti_length = pavimenti.length;

    clearInterval(id_interval);

    for(let i = 0; i < movibile_length; i++) {
        if(movibile[0] != null) {
            document.body.removeChild(movibile[0]);
            console.log("rimosso ostacolo "+(i+1));
        }
    }

    for(let i = 0; i < pavimenti_length; i++) {
        if(pavimenti[0]!=null) {
            document.body.removeChild(pavimenti[0]);
            console.log("pavimento rimosso");
        }
    }

    document.body.removeChild(player);
    console.log("player runisso");
    document.body.removeChild(p);
    document.removeEventListener("keydown", Muoviti);
    document.removeEventListener("keydown", JumpJump);
    document.removeEventListener("keydown", Impostazioni);
    fine = false;
}

function SceltaPingu()
{
    var container = document.createElement("div");
    var div = document.createElement("div");
    var bottoni = [document.createElement("button"),document.createElement("button"),document.createElement("button")];
    var exit = document.createElement("button");
    container.id = "container";
    exit.id="esci_selettore_skin";
    exit.addEventListener("click", e => {
        var a=document.getElementById("container");
        document.body.removeChild(a);
        CreaMenu();
    });
    exit.innerHTML="Esci";

    for(let i = 0; i < bottoni.length; i++) {
        var dil = document.createElement("div");
        bottoni[i].className = "bottone_pinguino";
        bottoni[i].appendChild(dil);
    }


    bottoni[0].getElementsByTagName("div")[0].style.backgroundImage = 'url(images/player_skins/player_cool.png)';
    bottoni[1].getElementsByTagName("div")[0].style.backgroundImage = 'url(images/player_skins/player.png)';
    bottoni[2].getElementsByTagName("div")[0].style.backgroundImage = 'url(images/player_skins/player2.png)';
    

    bottoni[0].addEventListener("click", e => {
        playercorr.skin = 'url(images/player_skins/player_cool.png)';
        ModificaRecords();
        var a=document.getElementById("container");
        document.body.removeChild(a);
        CreaMenu();
    });
    bottoni[1].addEventListener("click", e => {
        playercorr.skin = 'url(images/player_skins/player.png)';
        ModificaRecords();
        var a=document.getElementById("container");
        document.body.removeChild(a);
        CreaMenu();
    });
    bottoni[2].addEventListener("click", e => {
        playercorr.skin = 'url(images/player_skins/player2.png)';
        ModificaRecords();
        var a=document.getElementById("container");
        document.body.removeChild(a);
        CreaMenu();
    });

    div.id="contieniskin";
    
    for(let i = 0; i < bottoni.length; i++)
        div.appendChild(bottoni[i]);

    container.appendChild(div);
    container.appendChild(exit);

    document.body.appendChild(container);
}

function Impostazioni(event)
{
    if(event.key == 'Escape')
    {
        if(!pausa) {
            pausa = true;
            document.removeEventListener("keydown", JumpJump);
            document.removeEventListener("keydown", Muoviti);

            var copertura = document.createElement("div");
            var popup = document.createElement("div");
            var riavvia_button = document.createElement("button");
            var menu_button = document.createElement("button");
            copertura.id = "copertura";
            popup.id = "popup";

            riavvia_button.innerHTML = "Torna";
            menu_button.innerHTML = "Menu";

            riavvia_button.addEventListener("click", Riprendi);

            menu_button.addEventListener("click", e => {
                CancellaGioco();
                document.body.removeChild(document.getElementById("popup"));
                document.body.removeChild(document.getElementById("copertura"));
                CreaMenu();
                pausa = false;
            });

            popup.innerHTML = "<h1>Pausa</h1>";

            popup.appendChild(riavvia_button);
            popup.appendChild(menu_button);

            document.body.appendChild(copertura);
            document.body.appendChild(popup);
        } else {
            Riprendi();
        }
    }
}

function ModificaRecords()
{
    var arr = JSON.parse(localStorage.getItem('dati_giocatori'));
    arr[arr.indexOf(arr.find((e) => e.name == playercorr.name && e.surname == playercorr.surname && e.nick == playercorr.nick))] = playercorr;
    localStorage.setItem('dati_giocatori',JSON.stringify(arr));
    sessionStorage.setItem('current', JSON.stringify(playercorr));
}

function Riprendi()
{
    document.body.removeChild(document.getElementById("popup"));
    document.body.removeChild(document.getElementById("copertura"));
    document.addEventListener("keydown", Muoviti);
    document.addEventListener("keydown", JumpJump);
    pausa = false;
}