import { world } from '@minecraft/server';

export function levelHandler() {
    const ALL_PLAYERS = world.getAllPlayers();
    let totalLevels = 0;
    ALL_PLAYERS.forEach(player => {
        const playerLevel = player.level
        totalLevels = totalLevels + playerLevel
        // player.runCommand("say Total Levels: " + totalLevels)
    })
    // call function that writhes the new total level to scorboard if its bigger compared to before
    writeLevel(totalLevels)
    return;
}

/**
     * Updates level scoreboard if the score is higher compared to before.
     *
     * @param {string} newScore - the current total level
     *
     */
function writeLevel(newScore) {
    const scoreboard = world.scoreboard
    const levelSb = scoreboard.getObjective('level') ?? scoreboard.addObjective('level', 'level')
    const currScore = levelSb.getScore('var') ?? levelSb.setScore('var', 0)

    //world.sendMessage(currScore.toString())
    if (currScore < newScore) {
        levelSb.setScore('var', newScore)
        levelUnlockedMessage(newScore, currScore)
        //world.sendMessage('higher level detected')
    } else {
        //world.sendMessage('lower level detected')
        return;
    }
}

function newLevelUnlock() {
    const ALL_PLAYERS = world.getAllPlayers();
    ALL_PLAYERS.forEach(player => {
        if (player == undefined) {
            return;
        }
        player.playSound('horn.call.1', { "pitch": 1.3, "location": player.location, "volume": 2 })
        // player.runCommand("say Total Levels: " + totalLevels)
    })
}

function levelUnlockedMessage(newScore, oldScore) {
    //world.sendMessage('a new level can be unlocked ' + newScore)
    newLevelUnlock()

    // Ensure newScore is at least equal to oldScore
    newScore = Math.max(newScore, oldScore);

    for (let score = oldScore + 1; score <= newScore; score++) {
        switch (score) {
            case 1:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 24 71 7§b!')
                break;
            case 2:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 23 67 -24§b!')
                break;
            case 3:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -7 68 -24§b!')
                break;
            case 4:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -8 68 9§b!')
                break;
            case 5:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -7 70 38§b!')
                break;
            case 6:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 24 70 39§b!')
                break;
            case 7:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 55 71 40§b!')
                break;
            case 8:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 55 71 9§b!')
                break;
            case 9:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 56 67 -24§b!')
                break;
            case 10:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 56 71 -56§b!')
                break;
            case 11:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 24 66 -56§b!')
                break;
            case 12:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -8 67 -56§b!')
                break;
            case 13:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -39 71 -56§b!')
                break;
            case 14:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -40 69 -24§b!')
                break;
            case 15:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -39 67 8§b!')
                break;
            case 16:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -40 72 37§b!')
                break;
            case 17:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -42 68 72§b!')
                break;
            case 18:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -9 67 71§b!')
                break;
            case 19:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 22 68 72§b!')
                break;
            case 20:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 55 69 70§b!')
                break;
            case 21:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 87 67 72§b!')
                break;
            case 22:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 88 71 40§b!')
                break;
            case 23:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 88 69 6§b!')
                break;
            case 24:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 87 71 -24§b!')
                break;
            case 25:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 87 68 -54§b!')
                world.sendMessage('§4§l[Gamemaster]§r§2 Congratulations! You are halfway there!')
                break;
            case 26:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 88 67 -86§b!')
                break;
            case 27:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 55 66 -88§b!')
                break;
            case 28:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 23 66 -88§b!')
                break;
            case 29:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -7 67 -88§b!')
                break;
            case 30:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -38 75 -88§b!')
                break;
            case 31:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -69 76 -87§b!')
                break;
            case 32:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -70 75 -56§b!')
                break;
            case 33:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -72 71 -23§b!')
                break;
            case 34:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -72 68 7§b!')
                break;
            case 35:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -72 70 39§b!')
                break;
            case 36:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -71 68 69§b!')
                break;
            case 37:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -71 69 102§b!')
                break;
            case 38:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -41 67 102§b!')
                break;
            case 39:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 -8 67 104§b!')
                break;
            case 40:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 23 67 104§b!')
                break;
            case 41:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 55 68 103§b!')
                world.sendMessage('§4§l[Gamemaster]§r§2 Congratulations! You reached the Nether.')
                break;
            case 42:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 89 69 104§b!')
                break;
            case 43:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 118 70 104§b!')
                break;
            case 44:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 120 70 72§b!')
                break;
            case 45:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 120 68 42§b!')
                break;
            case 46:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 119 71 9§b!')
                world.sendMessage('§4§l[Gamemaster]§r§2 Congratulations! You reached the End. Prepare to fight the Enderdragon.')
                break;
            case 47:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 120 71 -24§b!')
                break;
            case 48:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 120 71 -54§b!')
                break;
            case 49:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 119 71 -88§b!')
                break;
            case 50:
                world.sendMessage('§4§l[Gamemaster]§r§2 Chunk §b' + score + '§2 is unlocked! Travel to§6 120 68 -116§b!')
                break;
            default:
                break;
        }
    }
}