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



world.beforeEvents.itemUse.subscribe(data => {
    if (data.itemStack.typeId !== "maca_evolvingmobs:info_device") return;
    let player = data.source // This makes sure the player is data.source from now on in the code.
    let title = "§l§0Evolving Mobs"
    if (player.getDynamicProperty('maca_evolvingmobs:mobChosen') === undefined) system.run(() => main(player))

    function main() {
        const form = new ActionFormData()
        .title(title)
        .body(`Welcome §l§n${player.nameTag}§r!\nPlease read the Info Device thoroughly.\n\nContact E-Mail: §l§nmacadesigns@outlook.com\n§rDiscord server in Development!\n\nChoose a starter mob:`) // Why is this not ("Welcome " + player.nameTag)
        .button('§l§fGhasty\n§rA baby ghasty.')
        .button('§l§1Gnawie\n§rA baby zombie.')
        .button('§l§aCreepy\n§rA baby creeper')
        .show(player).then(r => { // Is r => equal to function(r)
            if (r.selection == 0) {
                player.sendMessage("You picked Ghasty!")
                player.runCommandAsync(`execute as @a[tag=!maca_evolvingmobs:mob_chosen] at @s run structure load "maca_evolvingmobs/ghasty" ~~0.5~`)
                player.runCommandAsync(`execute as @a[tag=!maca_evolvingmobs:mob_chosen] at @s run tag @s add maca_evolvingmobs:mob_chosen`)
                player.setDynamicProperty('maca_evolvingmobs:mobChosen', true)
            }
            if (r.selection == 1) {
                player.sendMessage("You picked Gnawie!")
                player.runCommandAsync(`execute as @a[tag=!maca_evolvingmobs:mob_chosen] at @s run structure load "maca_evolvingmobs/gnawie" ~~0.5~`)
                player.runCommandAsync(`execute as @a[tag=!maca_evolvingmobs:mob_chosen] at @s run tag @s add maca_evolvingmobs:mob_chosen`)
                player.setDynamicProperty('maca_evolvingmobs:mobChosen', true)
            }
            if (r.selection == 2) {
                player.sendMessage("You picked Creepy!")
                player.runCommandAsync(`execute as @a[tag=!maca_evolvingmobs:mob_chosen] at @s run structure load "maca_evolvingmobs/creepy" ~~0.5~`)
                player.runCommandAsync(`execute as @a[tag=!maca_evolvingmobs:mob_chosen] at @s run tag @s add maca_evolvingmobs:mob_chosen`)
                player.setDynamicProperty('maca_evolvingmobs:mobChosen', true)
            }

    })
    }

})
