class DatiUtente {
    constructor(name,surname,nick,password,age) {
        this.name = name;
        this.surname = surname;
        this.nick = nick;
        this.password = password;
        this.age = age;
        this.punteggio=0;
        this.skin="url(images/player_skins/player.png)"; 
    }
}

function SavePlayerData(player_data)
{
    var arr = [];

    if(localStorage.getItem('dati_giocatori') == null) {
        arr.push(player_data);
        localStorage.setItem('dati_giocatori',JSON.stringify(arr));
    } else {
        arr = JSON.parse(localStorage.getItem('dati_giocatori'));
        if(arr.find(e => e.nick == player_data.nick) == undefined) {
            arr.push(player_data);
            localStorage.setItem('dati_giocatori', JSON.stringify(arr));
        }
        else
            alert("nick gia esistente, sceglierne un'altro");
    }
}

function PortamiViaaaa()
{
    location.href = "Scoreboard.html";
}

function Login()
{
    var nick = document.getElementById("nick").value;
    if(nick == "")
        nick = "Pingu";
    var password = document.getElementById("password").value;

    arr = JSON.parse(localStorage.getItem('dati_giocatori'));
    if(arr.find(e => e.nick == nick && e.password == password) != undefined) {
        sessionStorage.setItem("current",JSON.stringify(arr.find(e => e.nick == nick && e.password == password)));
        location.href = "Gioco.html";
    } else {
        alert("Login non riuscito :(");
    }
}

function Registrati()
{
    var name = document.getElementById("name").value;
    if(name=="")
        name = "Pingu";
    var nick = document.getElementById("nick").value;
    if(nick == "")
        nick = "Pingu";
    var age = document.getElementById("age").value;
    if(age == "")
        age = "2000000";
    var surname = document.getElementById("surname").value;
    if(surname == "")
        surname = "Pingu";
    var password = document.getElementById("password").value;
    
    var data = new DatiUtente(name, surname, nick, password, age);

    SavePlayerData(data);

    BottoneLogin();
}

function BottoneRegistrati()
{
    document.body.removeChild(document.getElementById("login"));
    CreaRegistrazione();
}

function BottoneLogin()
{
    var reg = document.getElementById("registrazione");
    document.body.removeChild(reg);
    CreaLogin();
}

function CreaRegistrazione()
{
    var div = document.createElement("div");
    div.setAttribute("id","registrazione");
    
    div.innerHTML = 'Nome:<br><input id="name" type="text"/><br>' +
            'Cognome:<br><input id="surname" type="text"/><br>' +
            'Nickname:<br><input id="nick" type="text"/><br>' +
            'Password:<br><input id="password" type="text"/><br>' +
            'Et&agrave:<br><input id="age" type="text"/><br>' +
            '<div>'+
                '<button id="boltone" onclick="Registrati()">Registrati</button>' +
                '<button id="score" onclick="BottoneLogin()">Login</button>' +
            "</div>";
    document.body.appendChild(div);
}

function CreaLogin()
{
    var div = document.createElement("div");
    div.setAttribute("id","login");
    
    div.innerHTML = 'Nickname:<br><input id="nick" type="text"/><br>' +
            'Password:<br><input id="password" type="text"/><br>' +
            '<div>'+
                '<button id="boltone" onclick="Login()">Login</button>' +
                '<button id="score" onclick="BottoneRegistrati()">Registrati</button>' +
            "</div>";
    document.body.appendChild(div);
}