var opciok = ["A harag rossz tanácsadó", "Jó pap holtig tanul", "Ajándék lónak ne nézd a fogát!", "Bort iszik és vizet prédikál", "Nyugalom a hosszú élet titka"];
const ABC = "aábcdeéfghiíjklmnoóöőpqrstuúüűvwxyz";
var current = "";
var guessed = [];
var specials = [" ", "!", ".", ",", "-", "?"];
var mistakes = 0;

document.addEventListener("DOMContentLoaded", () => {
    var options = document.getElementById("betuk");
    for (i in ABC) {
        var letter = document.createElement("option");
        letter.value = (ABC[i]);
        letter.innerText = (ABC[i]);
        options.appendChild(letter);
    }
    generate();
    play();
    mistakes = 0;
    document.getElementById("hibakep").src = `./60px-Hangman-${mistakes}.png`;
});

function generate() {
    current = opciok[Math.floor(Math.random() * opciok.length)];
}
function play() {
    var out = "";
    wrong = true;
    for (i in current) {
        if (guessed.indexOf(current[i].toLowerCase()) >= 0 && document.getElementById("betuk").value == current[i].toLowerCase()) {
            wrong = false;
        }
        if (guessed.indexOf(current[i].toLowerCase()) >= 0) {
            out += current[i];
        } else if (specials.indexOf(current[i].toLowerCase()) >= 0) {
            out += current[i];
        } else {
            out += '_';
        }
    }
    if (current == out) {
        alert("Gratulálok, megoldottad a feladatot!");
    }
    if (wrong) {
        if (wrong + 1 == 6) {
            alert("Sajnos vesztettél! Próbáld újra");
        }
        mistakes++;
        document.getElementById("hibakep").src = `./60px-Hangman-${mistakes}.png`;

    }
    document.getElementById("feladvany").innerText = out;
}
function send(event) {
    event.preventDefault();
    guessed.push(document.getElementById("betuk").value);
    play()
    var options = Array.from(document.getElementById("betuk").children);
    for (i in options) {
        if (options[i].value == document.getElementById("betuk").value) {
            options[i].remove();
            break;
        }
    }
    document.getElementById("betuk").value = options[0];
}