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

// Erius Behaviors
import "./sub_cores/erius"
import "./sub_cores/weapons"
import "./sub_cores/lore"

// Guide Book
world.beforeEvents.itemUse.subscribe(use => {
    if (use.itemStack.typeId === 'darkw_fallenking:guide_book') {
        const player = use.source

        player.runCommandAsync('/playsound item.book.page_turn @s ~ ~ ~')
        GuideBook_Main(player)
    }
    if (use.itemStack.typeId === 'darkw_fallenking:erius_treasure_bag') {
        const player = use.source
        player.runCommandAsync('/loot give @s loot "darkw/fallenking/erius_p2_bag"')
        player.runCommandAsync('/clear @s darkw_fallenking:erius_treasure_bag 0 1')
        player.runCommandAsync('playsound armor.equip_leather @s')
    }
})

function GuideBook_Main(player) {
    // Settings
    const GuideBookMain = new ActionFormData
    GuideBookMain.title('Guide Book')
    GuideBookMain.body('Welcome to The Fallen King addon! \n\n• A short but sweet pack that adds a singular boss to your world. A multi-phase brute thats empowered by the raw strength of wither skeletons.\n\n• This guide book will give you general pointers on how this addon works. Although there isnt too much to ready as this is a fairly small pack.')
    GuideBookMain.button('Crafting')
    GuideBookMain.button('Boss')

    system.run(() => {
        // Show Guide Book
        GuideBookMain.show(player).then(r => {
            if (r.selection === 0) {
                player.playSound('item.book.page_turn')
                GuideBook_Crafting(player)
            }
            if (r.selection === 1) {
                player.playSound('item.book.page_turn')
                GuideBook_Boss(player)
            }
        })

    })
}

function GuideBook_Crafting(player) {
    // Settings
    const GuideBookMain = new ActionFormData
    GuideBookMain.title('Crafting')
    GuideBookMain.body('Page 1: Crafting \n\n• In this addon, there are only two required items needed in order to get started. This guide book (halfway there!), and the summoning item. The recipes are automatically unlocked when you load in but its also provided below just in case you cant find it.\n\n|  §cA §7X §cA§r  |\n|  §cA §6B §cA§r  |\n|  §eC §aD §eC§r  |\n\n§cA - Bone\n§6B - Guide Book\n§eC - Stone\n§aD - Stone Sword\n§7X - Air§f\n\nAll items are color coded to make it easier to read.')
    GuideBookMain.button('Back')

    system.run(() => {
        // Show Guide Book
        GuideBookMain.show(player).then(r => {
            if (r.selection === 0) {
                player.playSound('item.book.page_turn')
                GuideBook_Main(player)
            }
        })

    })
}

function GuideBook_Boss(player) {
    // Settings
    const GuideBookMain = new ActionFormData
    GuideBookMain.title('Boss')
    GuideBookMain.body(`• Erius is an experiment gone wrong to make a wither skeleton into the ultimate warrior. When failed, he was repurposed and mutated even more, giving him complete mastery of his wither skeleton abilities\n\n• Erius has a pretty tough fight and an array of attacks. Some more deadly than others, and also inflicts some nasty effects. When defeated, you are provided a pretty good Loot Pool!\n\n§eLoot:§f\n\n(100%%) §dTreasure Bag *§f\n(100%%) 3-7 Bones\n(100%%) 5-12 Netherite Scraps\n(100%%) 1-3 Wither Skulls\n(100%%) Scythe of The Fallen King\n(10%%) Erius Skull\n(10%%) Fallen Erian Skull (Phase One)\n\n§f* §dTreasure Bags§f will ONLY drop in HARD difficulty. When opened, they provide more loot and boss heads will drop more often.`)
    GuideBookMain.button('Back')

    system.run(() => {
        // Show Guide Book
        GuideBookMain.show(player).then(r => {
            if (r.selection === 0) {
                player.playSound('item.book.page_turn')
                GuideBook_Main(player)
            }
        })

    })
}