import { ItemStack, Player } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";
import { setLoreItem } from "../../library/itemManager";

/**
 * Interfaz de tool switcher
 * @param {Player} player 
 * @param {ItemStack} itemStack 
 */
export const brokenToolSwitcherView = (player, itemStack) => {
    const actionForm = new ActionFormData();
    const state = player.getDynamicProperty('nps_ec:tool_switcher');

    actionForm.title({ translate: "view.title.tool_switcher" })//"Tool Switcher"
        .body({ translate: "view.descripton.tool_switcher" })//"§r§9Set up automatic tool replacement"

    if (state) {
        actionForm.button({ translate: "view.button_1.tool_switcher" })//"Disable tool switcher"
    } else {
        actionForm.button({ translate: "view.button_2.tool_switcher" })//"Enable tool switcher"
    }

    actionForm.button("Exit");

    actionForm.show(player).then((data) => {
        if (data.canceled) return;
        if (data.selection == 0) {
            if (state == undefined) {
                player.setDynamicProperty('nps_ec:tool_switcher', 1);
                player.runCommand("playsound plastic.switch.2 @a [r=5] ~ ~ ~ 0.3");

                setLoreItem(itemStack, player, "§r§9Tool switcher enabled", `Owner: ${player.nameTag}`);
                return
            };

            if (state) {
                player.setDynamicProperty('nps_ec:tool_switcher', 0);
                player.runCommand("playsound plastic.switch.1 @a [r=5] ~ ~ ~ 0.3");
                setLoreItem(itemStack, player, "§r§9Tool switcher disabled", `Owner: ${player.nameTag}`);
            }
            else {
                player.setDynamicProperty('nps_ec:tool_switcher', 1);
                player.runCommand("playsound plastic.switch.2 @a [r=5] ~ ~ ~ 0.3");
                setLoreItem(itemStack, player, "§r§9Tool switcher enabled", `Owner: ${player.nameTag}`);
            }
        }
    })
}