let buttonSelectScore = 0;
let searchName = '';
let scorescreenOn = false;
let dataIsCalled = false;
let clearDataOnce = false;
function keyPressed() {
    // Check if the back button was selected and the Enter key was pressed
    if (keyCode == ENTER && buttonSelectScore == 0) {
        scorescreenOn = false;
    }
}

function homescreen() {
    textAlign(CENTER, CENTER);
    textSize(70);
    fill(100, 255, 100);
    text("Slime\nSymphony", width / 2, height / 4);

    button(255, 255, 255, width / 2, height / 2, "Start Game")
    button(255, 255, 255, width / 2, height / 2 + height / 12, "Scores")

    push()
    textSize(50)
    fill(255, 255, 255)
    text("Highscores", width / 5, height / 4)
    text("Controls", width / 5 * 4, height / 4)
    pop()

    push()
    fill(255, 255, 255)
    textSize(25)
    textAlign(CENTER, CENTER)
    for (let i = 0; i < requesteddata.length; i++) {
        text(i + 1 + ". " + requesteddata[i].Naam + ": " + requesteddata[i].Score, width / 5, height / 3 + (i * 30))
    }
    pop()

    //Scores rectangle
    push()
    stroke(4, 217, 255)
    strokeWeight(5)
    fill(0, 255, 0, 0)
    rect(width / 5, height / 2, width / 4, height / 1.5)
    rect(width / 5 * 4, height / 2, width / 4, height / 1.5)
    pop()

}

function button(r, g, b, buttonX, buttonY, buttonText) {
    push()
    fill(0, 0, 50)
    rectMode(CENTER);
    rect(buttonX, buttonY, buttonWidth, buttonHeight)
    textSize(25)
    fill(r, g, b)
    textAlign(CENTER);
    text(buttonText, buttonX, buttonY)
    pop()
}

function pauseMenu() {
    textAlign(CENTER);
    textSize(50);
    fill(255, 255, 255);
    text("Paused", width / 2, height / 2 - height / 6);
    button(255, 255, 255, width / 2, height / 2, "Resume");
    button(255, 255, 255, width / 2, height / 2 + height / 12, "Main Menu");
}

function scoremenu() {
    textAlign(CENTER, CENTER);
    textSize(50);
    fill(255, 255, 255);
    text("Score", width / 2, height / 5);
    textSize(32);
    text(letters[currentIndex[0]], width / 2 - 30, height / 3);
    text(letters[currentIndex[1]], width / 2, height / 3);
    text(letters[currentIndex[2]], width / 2 + 30, height / 3);
    if (!clearDataOnce) {
        requesteddata = [];
        clearDataOnce = true;
    }

    if (scorescreenOn && keyReleasedFlag) {
        nameSubmit();
    }

    if (entered) {
        push()
        stroke(205, 205, 205)
        strokeWeight(2)
        fill(0, 0, 0, 0)
        rect(width / 2, height / 3, 90, 40)
        pop()
        if (!dataIsCalled) {
            getData(nameHS);
            dataIsCalled = true;
        }
    }
    else {
        nameDraw(3);
    }

    push()
    fill(255, 255, 255)
    textSize(25)
    textAlign(CENTER, CENTER)
    //text for name, score, achievements
    for (let i = 0; i < requesteddata.length; i++) {
        text(i + 1 + ". " + requesteddata[i].Naam + ": " + requesteddata[i].Score + " " + requesteddata[i].Datum, width / 4, height / 3 + (i * 30))
    }
    pop()

    push()
    fill(255, 255, 255)
    textSize(16)
    textAlign(CENTER, CENTER)
    for (let i = 0; i < requesteddata.length; i++) {
        text(i + 1 + ". " + requesteddata[i].Achievements, width / 4 * 3, height / 3 + (i * 30))
    }
    pop()

    push()
    stroke(4, 217, 255)
    strokeWeight(5)
    fill(0, 255, 0, 0)
    //box left
    rect(width / 4 * 3, height / 2, width / 3, height / 1.25)
    //box right
    rect(width / 4, height / 2, width / 3, height / 1.25)
    pop()
    push()
    text("Your scores", width / 4, height / 6)
    text("Your achievements", width / 4 * 3, height / 6)
    pop()
}

function gameOver() {
    if (lives <= 0) {
        //game over screen
        iIndex = constrain(iIndex, 0, 2);
        isDead = true;
        push();
        fill(255, 0, 0);
        textSize(60);
        textAlign(CENTER, CENTER);
        text("Game Over", width / 2, height / 2 - height / 5);

        fill(255, 255, 255)
        textSize(18);
        text("Score: " + int(time), width / 2, height / 2 - height / 8);

        textSize(32);
        textAlign(CENTER, CENTER);
        text(letters[currentIndex[0]], width / 2 - 30, height / 2);
        text(letters[currentIndex[1]], width / 2, height / 2);
        text(letters[currentIndex[2]], width / 2 + 30, height / 2);

        
        text("Restart", width / 2, height / 2 + height / 12);

        text("Main Menu", width / 2, height / 2 + height / 6);
        pop();
    }
}