import * as mc from "@minecraft/server"

const SOUND_NOTIFY = "pb.notification";
const SOUND_OPTIONS = { pitch: 1.0, volume: 3000.0, };

export function main() {
    entityDeathListener();
}

function entityDeathListener() {
    mc.world.afterEvents.entityDie.subscribe((eventData) => {
        if(eventData.deadEntity.typeId == "pb:skeleton_boss")
        {
            if(mc.world.scoreboard.getObjective("dragon_unlock").getScore("any") == 0)
            {
                tutorialBossDead(eventData.deadEntity.location);
                mc.world.scoreboard.getObjective("dragon_unlock").setScore("any", 1);
            }
            if(eventData.deadEntity.hasTag("boss_earth")) mc.world.scoreboard.getObjective("dragon_unlock").setScore("earth", 1);
            if(eventData.deadEntity.hasTag("boss_water")) mc.world.scoreboard.getObjective("dragon_unlock").setScore("water", 1);
            if(eventData.deadEntity.hasTag("boss_fire")) mc.world.scoreboard.getObjective("dragon_unlock").setScore("fire", 1);
            if(eventData.deadEntity.hasTag("boss_jungle")) mc.world.scoreboard.getObjective("dragon_unlock").setScore("jungle", 1);
            if(eventData.deadEntity.hasTag("boss_dark")) mc.world.scoreboard.getObjective("dragon_unlock").setScore("dark", 1);
        }
    })
}

//this runs the first time a dungeon boss has been defeated
function tutorialBossDead(location)
{
    mc.system.runTimeout(() => {
        mc.world.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_boss_01"}]});
        mc.world.playSound(SOUND_NOTIFY, location, SOUND_OPTIONS);
    }, mc.TicksPerSecond*3);
    mc.system.runTimeout(() => {
        mc.world.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_boss_02"}]});
        mc.world.playSound(SOUND_NOTIFY, location, SOUND_OPTIONS);
    }, mc.TicksPerSecond*8);
    mc.system.runTimeout(() => {
        mc.world.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_boss_03"}]});
        mc.world.playSound(SOUND_NOTIFY, location, SOUND_OPTIONS);
    }, mc.TicksPerSecond*13);
}

export function tutorialDragonHornCollected(location, dimension)
{
    mc.world.getDimension(dimension.id).spawnEntity("pb:wizard_abroad",{x: location.x, y: location.y - 10, z: location.z});
    mc.system.runTimeout(() => {
        mc.world.getDimension(dimension.id).runCommandAsync("dialogue open @e[type=pb:wizard_abroad,c=1] @a wizard_horn_01");
    }, 5);
}

//this runs the first time a dungeon boss has been defeated
export function tutorialDragonFreed(location)
{
    mc.world.scoreboard.getObjective("dragon_unlock").setScore("any", 2);
    mc.system.runTimeout(() => {
        mc.world.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_dragontame_01"}]});
        mc.world.playSound(SOUND_NOTIFY, location, SOUND_OPTIONS);
    }, mc.TicksPerSecond*5);
    mc.system.runTimeout(() => {
        mc.world.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_dragontame_02"}]});
        mc.world.playSound(SOUND_NOTIFY, location, SOUND_OPTIONS);
    }, mc.TicksPerSecond*8);
    mc.system.runTimeout(() => {
        mc.world.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_dragontame_03"}]});
        mc.world.playSound(SOUND_NOTIFY, location, SOUND_OPTIONS);
    }, mc.TicksPerSecond*14);
    mc.system.runTimeout(() => {
        mc.world.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_dragontame_04"}]});
        mc.world.playSound(SOUND_NOTIFY, location, SOUND_OPTIONS);
    }, mc.TicksPerSecond*20);
    mc.system.runTimeout(() => {
        mc.world.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_dragontame_05"}]});
        mc.world.playSound(SOUND_NOTIFY, location, SOUND_OPTIONS);
    }, mc.TicksPerSecond*25);
}

//this runs the first time a dungeon boss has been defeated
export function tutorialDragonRide(player)
{
    player.addTag("dragon_rider");
    mc.system.runTimeout(() => {
        player.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_flight_01"}]});
        player.playSound(SOUND_NOTIFY, SOUND_OPTIONS);
    }, mc.TicksPerSecond*2);

    mc.system.runTimeout(() => {
        player.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_flight_02"}]});
        player.playSound(SOUND_NOTIFY, SOUND_OPTIONS);
    }, mc.TicksPerSecond*8);

    mc.system.runTimeout(() => {
        player.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_flight_03"}]});
        player.playSound(SOUND_NOTIFY, SOUND_OPTIONS);
    }, mc.TicksPerSecond*14);

    mc.system.runTimeout(() => {
        player.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_flight_04"}]});
        player.playSound(SOUND_NOTIFY, SOUND_OPTIONS);
    }, mc.TicksPerSecond*20);

    mc.system.runTimeout(() => {
        player.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_flight_05"}]});
        player.playSound(SOUND_NOTIFY, SOUND_OPTIONS);
    }, mc.TicksPerSecond*26);

    mc.system.runTimeout(() => {
        player.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_flight_06"}]});
        player.playSound(SOUND_NOTIFY, SOUND_OPTIONS);
    }, mc.TicksPerSecond*34);

    mc.system.runTimeout(() => {
        player.sendMessage({"rawtext":[{"text":"§b["},{"translate":"npc.wizard"},{"text":"]§f "},{"translate":"chat.wizard_flight_07"}]});
        player.playSound(SOUND_NOTIFY, SOUND_OPTIONS);
    }, mc.TicksPerSecond*42);
}