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


//music this is def an attack trust
system.afterEvents.scriptEventReceive.subscribe((event) => {
    const id = event.id;
    const entity = event.sourceEntity;
    if (id === 'dragons:end_music') {

        entity.runCommand("stopsound @a[r=90,tag=dragons:music_playing] record.megalophobia");
        system.runTimeout(() => {
            entity.runCommand(`playsound record.megalophobia_end @a[tag=!dragons:ignore_music,tag=dragons:music_playing,r=64] ~~~ 1.5`)
            entity.runCommand(`tag @a[r=90] remove dragons:music_playing`);
        }, 5);

    }
});

system.afterEvents.scriptEventReceive.subscribe((event) => {
    const id = event.id;
    const entity = event.sourceEntity;
    if (id === 'dragons:play_music') {

        entity.runCommand(`playsound record.megalophobia @a[tag=!dragons:ignore_music,tag=!dragons:music_playing,r=64]`)
        entity.runCommand(`execute as @a[tag=!dragons:ignore_music,tag=!dragons:music_playing,r=64] run scriptevent dragons:music_timer`)


    }
});


system.afterEvents.scriptEventReceive.subscribe((event) => {
    const id = event.id;
    const entity = event.sourceEntity;
    if (id === 'dragons:music_timer') {
        if (entity.hasTag("dragons:music_playing")) return;
        entity.addTag("dragons:music_playing")
        var musicT = world.scoreboard.getObjective("musicT")
        if (!musicT) {
            world.scoreboard.addObjective("musicT");
            musicT = world.scoreboard.getObjective("musicT")
        }
        musicT.setScore(entity, 2910)
    }
});

world.afterEvents.playerSpawn.subscribe((event) => {
    const player = event.player;
    if (event.initialSpawn === false) {
        let entities = player.dimension.getEntities({
            location: player.location,
            maxDistance: 70,
            families: ['lethal']
        });
        if (entities.length <= 0) {
            player.removeTag("dragons:music_playing");
            player.runCommand("stopsound @s record.megalophobia");
        }
    }

    system.runTimeout(() => {
        if (event.initialSpawn === true) player.removeTag("dragons:music_playing");
    }, 100)
})

system.runInterval(() => {
    let players = world.getPlayers({ tags: ["dragons:music_playing"] })
    for (const player of players) {
        let musicT = world.scoreboard.getObjective("musicT");
        musicT.addScore(player, -1)

        if (world.scoreboard.getObjective("musicT").getScore(player) <= 1) {
            player.removeTag("dragons:music_playing");
            player.runCommand("stopsound @s record.megalophobia");
            return;
        }
    }
})

system.runInterval(() => {
    let players = world.getPlayers({ tags: ["dragons:music_playing"] })
    for (const player of players) {



        let entities = player.dimension.getEntities({
            location: player.location,
            maxDistance: 70,
            families: ['lethal']
        });
        if (entities.length <= 0) {
            player.runCommand("stopsound @s record.megalophobia");
            player.removeTag("dragons:music_playing");

        }
    }
}, 100)
