import { world, system, Entity, EntityDamageCause } from '@minecraft/server';

system.runInterval(() => {
    let player_list = world.getAllPlayers();

    for (let player of player_list) {
        if (player.hasTag("armadilha")) {
            let { x, y, z } = player.location;

            player.removeTag("armadilha");
            player.addTag("fim_da_emboscada");

            system.runTimeout(() => {            
        player.runCommandAsync("title @s title §4§o§lBANDIT ATTACK!");
     player.runCommandAsync("scoreboard objectives add BanditCount dummy"); 
                player.runCommandAsync("scoreboard players set @s BanditCount 5"); 
          player.runCommandAsync("music stop 1.0");
     player.runCommandAsync("music play music.game.berserk.ghosts_theme 1.0 0.5 loop");
            }, 10);
            
            let d_base = 15;
            system.runTimeout(() => { player.runCommandAsync(`summon berserk:bandit ${x} ${y} ${z + 8}`); }, d_base); 
      system.runTimeout(() => { player.runCommandAsync(`summon berserk:bandit ${x} ${y} ${z - 8}`); }, d_base + 1); 
            system.runTimeout(() => { player.runCommandAsync(`summon berserk:bandit ${x + 8} ${y} ${z}`); }, d_base + 2); 
        system.runTimeout(() => { player.runCommandAsync(`summon berserk:bandit ${x - 8} ${y} ${z}`); }, d_base + 3); 
            system.runTimeout(() => { player.runCommandAsync(`summon berserk:bandit_leader ${x} ${y + 8} ${z}`); }, d_base + 4); 
            
            system.runTimeout(() => {
                if (!player.isValid() || !player.hasTag("fim_da_emboscada")) return;

                player.removeTag("fim_da_emboscada");
           player.removeTag("hearing_cry");
                
        player.runCommandAsync("scoreboard players reset @s BanditCount");
     player.runCommandAsync("kill @e[type=berserk:bandit,r=50]");
           player.runCommandAsync("kill @e[type=berserk:bandit_leader,r=50]");
     player.runCommandAsync("tag @e[type=nb:crying_girl,r=50] remove armadilha_start");
               player.runCommandAsync("tag @e[type=nb:crying_girl,r=50] remove armadilha_fim");
player.runCommandAsync("kill @e[type=nb:crying_girl,r=50]");    
                
                player.sendMessage("§aYou escaped from the bandits!");
     player.runCommandAsync("music stop 2.0");
                 player.runCommandAsync("music play music.game.berserk.murder_theme 1.0 0.5 loop");
            }, 2400); 
        } 

        if (player.hasTag("fim_da_emboscada")) {
            try {
                let placar = world.scoreboard.getObjective("BanditCount");
                let pts = placar.getScore(player);
                if (pts !== undefined && pts <= 0) {
                    system.run(() => {
                player.runCommandAsync("function fim");
           player.runCommandAsync("scoreboard players reset @s BanditCount");
                    });
                }
            } catch (e) {}
        }
    }
}, 10);

function cry_girl_detect() {
    let players = world.getPlayers();
    for (let player of players) {
        if (player.hasTag("armadilha") || player.hasTag("fim_da_emboscada")) continue; 
        
        let girls = player.dimension.getEntities({
            type: "nb:crying_girl",
            location: player.location, 
            maxDistance: 16.0,         
            closest: 1                
        });

        if (girls.length > 0) {
            let first_girl = girls[0]; 
            if (!first_girl.hasTag("armadilha_start")) {
                player.addTag("armadilha");
           first_girl.addTag('armadilha_start');  
            }     
        }
    }
}
system.runInterval(cry_girl_detect, 20);

function cry_girl_sound() {
    let all_players = world.getPlayers();
    for (let player of all_players) {
        let fighting = player.hasTag("fim_da_emboscada") || player.hasTag("armadilha");
        let near_girls = player.dimension.getEntities({
            type: "nb:crying_girl",
            location: player.location, 
            maxDistance: 20.0, 
            closest: 1                
        });

        let is_hearing = player.hasTag("hearing_cry");
        
        if (near_girls.length > 0) {
            let girl = near_girls[0]; 
            if (!girl.hasTag("armadilha_fim") && !girl.hasTag("armadilha_start")) {    
                if (!is_hearing && !fighting) {
                    player.addTag("hearing_cry");
              player.runCommandAsync("music play music.ambient.crying_girl 1.0 0.5 loop"); 
                }
            }
        } else {
            if (is_hearing && !fighting) {
                player.removeTag("hearing_cry");
           player.runCommandAsync("music stop 1.0"); 
              player.runCommandAsync("music play music.game.berserk.murder_theme 1.0 0.5 loop");
            }
        }
    }
}
system.runInterval(cry_girl_sound, 20);