import { world, system } from "@minecraft/server"
import { ActionFormData } from "@minecraft/server-ui"

world.beforeEvents.itemUse.subscribe(data => {
    let player = data.source
    if (data.itemStack.typeId == "bey:book_of_knowledge_hak") system.run(() => Main(player))

    function Main() {
        const tutorialUI = new ActionFormData()
            .title("§l§6Book §fOf §dKnowledge")
            .body('Everything You Need To Know Is In This Book')
            .button("§l§8Knifes", "textures/ui/knife-ui")
            .button("§l§4Heart §f& §cFragment", "textures/ui/heart-ui")
            .button("§l§bConsequence", "textures/ui/lightning-ui")
            .button("§l§6GOD", "textures/ui/god_anger_detector-ui")
            .button(`§cCancel`)
        tutorialUI.show(player).then(r => {
            if (r.selection == 0) Knifes(player)
            if (r.selection == 1) Heart(player)
            if (r.selection == 2) Consequence(player)
            if (r.selection == 3) God(player)
        })
    }

    function Knifes() {
        const knifeUI = new ActionFormData()
            .title("§l§8Knifes")
            .body("There are 3 types of knifes craftable in the crafting table. Each Upgrades Makes The Knife Do More Damage And Have More Durability. Killing A Mob With The Weapon Will Make The Mob Drop A Heart Fragment With It\n\n    \n\n")
            .button("§cCancel")
        knifeUI.show(player).then(r => {
            if (r.selection == 0) Main(player)
        })

    }

    function Heart() {
        const HeartUI = new ActionFormData()
            .title("§l§4Heart §f& §cFragment")
            .body("Once you get 8 Heart Fragment you can combine those with a netherite scrap to make a pure heart. Sneak and interacting with the Pure Heart will gain you 1+ max health. But make sure you remember for each 3 extra heart you will trigger a consequence that will do something bad to you in someway or other.\n\n   \n\n")
            .button("§cCancel")
        HeartUI.show(player).then(r => {
            if (r.selection == 0) Main(player)
        })

    }

    function Consequence() {
        const ConsequenceUI = new ActionFormData()
            .title("§l§bConsequence")
            .body("For each 3+ max heart 1 new consequence will be triggred. There is total 5 consequence trigger. I am not gonna spoil what all consequence does but you can find them yourself, keep collecting\n\n")
            .button("§cCancel")
        ConsequenceUI.show(player).then(r => {
            if (r.selection == 0) Main(player)
        })
    }

    function God() {
        const GodUI = new ActionFormData()
            .title("§l§6GOD")
            .body("God is the supernatural entity thats lives above your worlds in this mod. He is a strict pacifist. Every kill from your knife will be counted as a murder and will make GOD more and more angry. Once he has reached level 3 of his anger he will start throwing lightning at you in order to make you stop. You can get around this problem by killing a mob and wait for the GOD to calm down and kill mobs again. You can check the GODs anger by using the God Meter given to you.\n\n  \n\n")
            .button("§cCancel")
        GodUI.show(player).then(r => {
            if (r.selection == 0) Main(player)
        })
    }

})


const bossMob = [
    "minecraft:ender_dragon",
    "minecraft:warden",
    "minecraft:elder_guardian",
    "minecraft:evocation_illager"
];

world.afterEvents.entityDie.subscribe((event) => {
    if (event.damageSource.damagingEntity?.hasTag("knifer")) {
        if (bossMob.includes(event.deadEntity.typeId)) {
            event.deadEntity.runCommandAsync("structure load heart ~~~")
            event.damageSource.damagingEntity.runCommand("/scoreboard players add @s godAnger 600")
        } else {
            event.deadEntity.runCommandAsync("structure load broken_heart ~~~")
            event.damageSource.damagingEntity.runCommand("/scoreboard players add @s godAnger 600")
        }
    }
});

world.afterEvents.entityDie.subscribe((event) => {
    if (event.deadEntity?.typeId == "minecraft:player") {
        event.deadEntity.runCommand("/event entity @s bey:heart_back")
        event.deadEntity.runCommand("/function health_back")

        if (event.deadEntity?.hasTag("YesgodAnger")) {
            event.deadEntity.runCommand("/scoreboard players set @s godAnger 499")
            event.deadEntity.runCommand("/function tellraw_when_god_kill")
        }
        else { }
    }
})


world.afterEvents.entityHitEntity.subscribe((event) => {
    if (event.damagingEntity.hasTag("godAnger")) {
        event.damagingEntity.runCommand("/summon lightning_bolt ~~~")
    }
})