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

//const ui = new ActionFormData()
//   .title("Form")
//    .body("")
//    .button("button1")
//    .button("button2")
//    .button("button3");

world.beforeEvents.itemUse.subscribe(data => {
    let player = data.source
    if (data.itemStack.typeId == "fiery:skillbook")
        system.run(() => main(player))

    function main() {
        const inventory = player.getComponent('inventory').container;
        const selectedItem = inventory.getItem(player.selectedSlot);
        const durability = selectedItem?.getComponent('durability');
        const DoubleJumpUi = new ActionFormData()
            .title("Double Jump")
            .body("")
        if (!player.hasTag("DoubleJump3")) DoubleJumpUi.button("I - 10 EXP", "textures/ui/double_jump0")
        if (!player.hasTag("DoubleJump3")) DoubleJumpUi.button("II - 20 EXP", "textures/ui/double_jump1")
        if (!player.hasTag("DoubleJump3")) DoubleJumpUi.button("III - 30 EXP", "textures/ui/double_jump2")
        if (!player.hasTag("DoubleJump3")) DoubleJumpUi.button("IV - 40 EXP", "textures/ui/double_jump3")
        if (player.hasTag("DoubleJump3")) DoubleJumpUi.button("V - 50 EXP", "textures/ui/double_jump4");
        DoubleJumpUi.show(player).then(r => {
            if (r.selection == 0) {
                if (!player.hasTag('DoubleJump0') && player.level >= 10) {
                    player.addTag('DoubleJump0');
                    player.sendMessage("§aCongratulations! You have unlocked the ability §l§bDouble Jump I");
                    player.addLevels(-10);
                    player.playSound("random.levelup");
                    durability.damage += 1;
                    inventory.setItem(player.selectedSlot, selectedItem);
                } else if (!player.hasTag('DoubleJump0') && !player.hasTag('DoubleJump3') && player.level <= 10) {
                    player.sendMessage("§cRequired §a10 experiences§c to unlock this ability!");
                    player.playSound("random.break");
                } else if (player.hasTag('DoubleJump0') && !player.hasTag('DoubleJump3')) {
                    player.sendMessage("§aYou already unlocked this ability!");
                    player.playSound("random.orb");
                }
            }
            if (r.selection == 1) {
                if (!player.hasTag('DoubleJump1') && player.hasTag('DoubleJump0') && player.level >= 20) {
                    player.addTag('DoubleJump1');
                    player.sendMessage("§aCongratulations! You have unlocked the ability §l§bDouble Jump II");
                    player.addLevels(-20);
                    player.playSound("random.levelup");
                    durability.damage += 1;
                    inventory.setItem(player.selectedSlot, selectedItem);
                } else if (!player.hasTag('DoubleJump0')) {
                    player.sendMessage("§cYou need to unlock the previous ability first!");
                    player.playSound("random.break");
                }
                else if (!player.hasTag('DoubleJump1') && player.level <= 20) {
                    player.sendMessage("§cRequired §a20 experiences§c to unlock this ability!");
                    player.playSound("random.break");
                } else if (player.hasTag('DoubleJump1')) {
                    player.sendMessage("§aYou already unlocked this ability!");
                    player.playSound("random.orb");
                }
            }
            if (r.selection == 2) {
                if (!player.hasTag('DoubleJump2') && player.hasTag('DoubleJump1') && player.level >= 30) {
                    player.addTag('DoubleJump2');
                    player.sendMessage("§aCongratulations! You have unlocked the ability §l§bDouble Jump III");
                    player.addLevels(-30);
                    player.playSound("random.levelup");
                    durability.damage += 1;
                    inventory.setItem(player.selectedSlot, selectedItem);
                } else if (!player.hasTag('DoubleJump1')) {
                    player.sendMessage("§cYou need to unlock the previous ability first!");
                    player.playSound("random.break");
                }
                else if (!player.hasTag('DoubleJump2') && player.level <= 30) {
                    player.sendMessage("§cRequired §a30 experiences§c to unlock this ability!");
                    player.playSound("random.break");
                } else if (player.hasTag('DoubleJump2')) {
                    player.sendMessage("§aYou already unlocked this ability!");
                    player.playSound("random.orb");
                }
            }
            if (r.selection == 3) {
                if (!player.hasTag('DoubleJump3') && player.hasTag('DoubleJump2') && player.level >= 40) {
                    player.addTag('DoubleJump3');
                    player.sendMessage("§aCongratulations! You have unlocked the ability §l§bDouble Jump IV");
                    player.addLevels(-40);
                    player.playSound("random.levelup");
                    durability.damage += 1;
                    inventory.setItem(player.selectedSlot, selectedItem);
                } else if (!player.hasTag('DoubleJump2')) {
                    player.sendMessage("§cYou need to unlock the previous ability first!");
                    player.playSound("random.break");
                }
                else if (!player.hasTag('DoubleJump3') && player.level <= 40) {
                    player.sendMessage("§cRequired §a40 experiences§c to unlock this ability!");
                    player.playSound("random.break");
                } else if (player.hasTag('DoubleJump3')) {
                    player.sendMessage("§aYou already unlocked this ability!");
                    player.playSound("random.orb");
                }
            }
            if (r.selection == 0) {
                if (!player.hasTag('DoubleJump4') && player.hasTag('DoubleJump1') && player.hasTag('DoubleJump2') && player.hasTag('DoubleJump3') && player.level >= 50) {
                    player.addTag('DoubleJump4');
                    player.sendMessage("§aCongratulations! You have unlocked the ability §l§mDouble Jump V");
                    player.addLevels(-50);
                    player.playSound("random.levelup");
                    player.runCommand("clear @s fiery:skillbook_double_jump 0 1");
                } else if (!player.hasTag('DoubleJump4') && player.hasTag('DoubleJump1') && player.hasTag('DoubleJump2') && player.hasTag('DoubleJump3') && player.level <= 50) {
                    player.sendMessage("§cRequired §a50 experiences§c to unlock this ability!");
                    player.playSound("random.break");
                } else if (player.hasTag('DoubleJump4') && player.hasTag('DoubleJump1') && player.hasTag('DoubleJump2') && player.hasTag('DoubleJump3')) {
                    player.sendMessage("§aYou already unlocked this ability!");
                    player.playSound("random.orb");
                }
            }
        })
    }
})

//world.afterEvents.itemUse.subscribe((event) => {
//   const { source, itemStack } = event
//    switch (itemStack.typeId) {
//        //        case "minecraft:book": ui.show(source); break;
//        case "minecraft:stick": DoubleJumpUi.show(source); break;
//    }
//})
