import { system, world } from "@minecraft/server";

// Function to check tags and reset
function checkTagsAndReset() {
    const players = world.getAllPlayers();
    
    // Check if ANY player has the 'alive' tag
    const hasAlivePlayers = players.some(p => p.hasTag('alive'));
    
    // Only proceed if NO players have the 'alive' tag
    if (!hasAlivePlayers) {
        for (const player of players) {
            if (player.hasTag('area_1')) {
                player.runCommand('tag @a add end_lost_normal');
                player.runCommand('tag @a remove area_1');
                player.runCommand('clear @a jam:extinguisher');
                player.runCommand('kill @e[family=water]');
            }
            if (player.hasTag('area_2')) {
                player.runCommand('tag @a add end_lost_medium');
                player.runCommand('tag @a remove area_2');
                player.runCommand('clear @a jam:extinguisher');
                player.runCommand('kill @e[family=water]');
            }
            if (player.hasTag('area_3')) {
                player.runCommand('tag @a add end_lost_large');
                player.runCommand('tag @a remove area_3');
                player.runCommand('clear @a jam:extinguisher');
                player.runCommand('kill @e[family=water]');
            }
        }
    }
}

// Run the check every tick using system.runInterval
system.runInterval(() => {
    checkTagsAndReset();
}, 1);

// Detector for unlocking medium tower (run once)
let mediumTowerUnlocked = false;
system.runInterval(() => {
    if (mediumTowerUnlocked) return;
    // Check if any player has area1 score of 5 or more
    const unlockCheck = world.getDimension("overworld").runCommand("execute as @a[scores={area1=5..}] run testfor @s");
    if (unlockCheck.successCount > 0) {
        // Check if 53 -62 21 is air
        const blockCheck = world.getDimension("overworld").runCommand("testforblock 53 -62 21 air");
        if (blockCheck.successCount > 0) {
            // Set block and congratulate
            world.getDimension("overworld").runCommand("setblock 53 -62 21 redstone_block");
            world.getDimension("overworld").runCommand('tellraw @a {"rawtext":[{"text":"§d➜ Congratulations! You have unlocked the Medium Tower!"}]}');
            system.runTimeout(() => {
                world.getDimension("overworld").runCommand('execute as @a at @s run playsound random.levelup @a ~~~ 1 1');
                system.runTimeout(() => {
                    world.getDimension("overworld").runCommand('execute as @a at @s run playsound random.levelup @a ~~~ 1 1');
                    system.runTimeout(() => {
                        world.getDimension("overworld").runCommand('execute as @a at @s run playsound random.levelup @a ~~~ 1 1');
                        mediumTowerUnlocked = true;
                    }, 20);
                }, 20);
            }, 0);
        }
    }
}, 20);

// Detector for unlocking hard tower (run once)
let hardTowerUnlocked = false;
system.runInterval(() => {
    if (hardTowerUnlocked) return;
    // Check if any player has area2 score of 5 or more
    const unlockCheck = world.getDimension("overworld").runCommand("execute as @a[scores={area2=5..}] run testfor @s");
    if (unlockCheck.successCount > 0) {
        // Check if 59 -62 21 is air
        const blockCheck = world.getDimension("overworld").runCommand("testforblock 59 -62 21 air");
        if (blockCheck.successCount > 0) {
            // Set block and congratulate
            world.getDimension("overworld").runCommand("setblock 59 -62 21 redstone_block");
            world.getDimension("overworld").runCommand('tellraw @a {"rawtext":[{"text":"§d➜ Congratulations! You have unlocked the Hard Tower!"}]}');
            system.runTimeout(() => {
                world.getDimension("overworld").runCommand('execute as @a at @s run playsound random.levelup @a ~~~ 1 1');
                system.runTimeout(() => {
                    world.getDimension("overworld").runCommand('execute as @a at @s run playsound random.levelup @a ~~~ 1 1');
                    system.runTimeout(() => {
                        world.getDimension("overworld").runCommand('execute as @a at @s run playsound random.levelup @a ~~~ 1 1');
                        hardTowerUnlocked = true;
                    }, 20);
                }, 20);
            }, 0);
        }
    }
}, 20);

system.runInterval(() => {
    const players = world.getAllPlayers();
    for (const player of players) {
        if (
            (player.hasTag('alive') && player.hasTag('tutorial')) ||
            (!player.hasTag('alive') && !player.hasTag('tutorial'))
        ) {
            player.runCommand('effect @s saturation 2 1 true');
            player.runCommand('effect @s instant_health 1 1 true');
        }
    }
}, 1);

