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

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

import { getElement, getElementColour, getCost, getLore, spellList } from "./main.js"
import { sendNotification } from "main.js"

system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent("mm:mystery_scroll", {
        onUse(e) {
            const source = e.source;
            source.playSound("block.cartography_table.use", { pitch: 1.2 })
        },
        onCompleteUse(e, p) {
            var spellOptions = p.params.options;
            if (spellOptions == undefined || spellOptions.length == 0) spellOptions = []
            const source = e.source;
            const equippable = source.getComponent("minecraft:equippable")
            let newScroll = new ItemStack("mm:scroll", 1)
            if (spellOptions.length >= 1) {
                let rand = Math.floor(Math.random() * spellOptions.length);
                let spell = spellOptions[rand];
                newScroll.setLore([("§8§o" + getCost(spell, undefined, undefined) + " Mana§r"), getElementColour(getElement(spell)) + getLore(spell)])
                newScroll.setDynamicProperty("mm:spell", spell)

                if (!source.hasTag(spell + "_recipe_unlocked")) {
                    source.addTag(spell + "_recipe_unlocked")
                    sendNotification(source, `§dNew Recipe\n§r${getLore((spell))}`, "textures/ui/form/spell/spell_" + spell.replace("mm:", ""), getElement(spell))
                }
            }

            equippable.setEquipment("Mainhand", newScroll)
            source.dimension.spawnParticle("minecraft:witchspell_emitter", source.location)
            source.playSound("block.enchanting_table.use", { pitch: 0.5 })
            source.startItemCooldown("spell", 20);
        }
    });

    event.itemComponentRegistry.registerCustomComponent("mm:creative_scroll", {
        onUse(e) {
            const source = e.source;
            source.playSound("block.cartography_table.use", { pitch: 1.2 })
        },
        onCompleteUse(e, p) {
            const source = e.source;
            const equippable = source.getComponent("minecraft:equippable")
            let newScroll = new ItemStack("mm:scroll", 1)


            let scrollMenu = new ActionFormData();
            scrollMenu.title("mm:spell_crafter_grid_menu")
            scrollMenu.body(`Select a Spell`)
            var buttonArray = []
            for (const scroll of spellList) {
                //if (scroll == "all") continue;
                if (buttonArray.includes(scroll)) continue;
                const spellName = getLore(scroll)
                buttonArray.push(scroll)
                scrollMenu.button(spellName, "textures/ui/form/spell/spell_" + spellName.toLowerCase())
            }

            scrollMenu.show(source).then(r => {
                if (r.canceled) {
                    return;
                } else {
                    const selectedSpell = buttonArray[r.selection]
                    newScroll.setLore([("§8§o" + getCost(selectedSpell, undefined, undefined) + " Mana§r"), getElementColour(getElement(selectedSpell)) + getLore(selectedSpell)])
                    newScroll.setDynamicProperty("mm:spell", selectedSpell)
                    equippable.setEquipment("Mainhand", newScroll)
                    source.dimension.spawnParticle("minecraft:witchspell_emitter", source.location)
                    source.playSound("block.enchanting_table.use", { pitch: 0.5 })
                    source.startItemCooldown("spell", 20);
                    if (!source.hasTag(selectedSpell + "_recipe_unlocked")) {
                        source.addTag(selectedSpell + "_recipe_unlocked")
                        sendNotification(source, `§dNew Recipe\n§r${getLore((selectedSpell))}`, "textures/ui/form/spell/spell_" + selectedSpell.replace("mm:", ""), getElement(selectedSpell))
                    }
                }
            }).catch(e => {
                console.error(e, e.stack);
            });
        }
    })
})