import { ItemStack, Player, system, world } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";
import { filterCoordinateTags, getCoordinatesFromUUID } from "../../library/uuidCoords";
import { getGamemode } from "../../library/worldFuntions";

const dimensionHud = {
    /**
     * @param {Player} player 
     * @param {ItemStack} item 
     * @returns 
     */
    0: (player, item) => teleportIn(player, item, "minecraft:overworld"),
    /**
     * @param {Player} player 
     * @param {ItemStack} item 
     * @returns 
     */
    1: (player, item) => teleportIn(player, item, "minecraft:nether"),
    /**
     * @param {Player} player 
     * @param {ItemStack} item 
     * @returns 
     */
    2: (player, item) => teleportIn(player, item, "minecraft:the_end")
}

/**
 * Interfaz de teletransporte
 * @param {Player} player Datos del jugador
 * @param {ItemStack} item Datos del objeto
 */
export const teleportTo = (player, item) => {
    const actionForm = new ActionFormData();
    actionForm.title("view.title.teleport_to");
    actionForm.button("dimension.dimensionName0", "textures/blocks/grass_side_carried");
    actionForm.button("dimension.dimensionName1", "textures/blocks/crimson_nylium_side");
    actionForm.button("dimension.dimensionName2", "textures/blocks/end_stone");
    try {
        actionForm.show(player).then((data) => {
            if (data.canceled) return;
            dimensionHud[data.selection](player, item);
        })
    } catch { }
}


const teleportIn = (player, item, dimension) => {
    const tpAction = new ActionFormData();
    const playerTags = filterCoordinateTags(player.getTags(), dimension);
    const tpDimesion = world.getDimension(dimension.replace('minecraft:', ''))
    tpAction.title("view.title.teleport_in");
    if (playerTags.length > 0) {
        for (let i = 0; i < playerTags.length; i++) {
            const coord = getCoordinatesFromUUID(playerTags[i])
            const coordString = `(${coord.x}, ${coord.y}, ${coord.z})`
            tpAction.button({ translate: "view.button_1.teleport_in", with: [coordString]});
        }
        try {
            tpAction.show(player).then((data) => {
                if (data.canceled) return;
                const targetUUID = playerTags[data.selection];
                const targetCoordinates = getCoordinatesFromUUID(targetUUID);
                if(getGamemode(player) === 'survival' || getGamemode(player) === 'adventure') {
                    if ((item.amount - 1) != 0) {
                        player.runCommandAsync(`replaceitem entity @s slot.weapon.mainhand 0 nps_ec:teleport_point ${item.amount - 1}`);
                    } else {
                        player.runCommandAsync(`replaceitem entity @s slot.weapon.mainhand 0 air`)
                    }
                }
                tpDimesion.runCommandAsync(`tp ${player.nameTag} ${targetCoordinates.x} ${targetCoordinates.y + 1} ${targetCoordinates.z}`);

                system.runTimeout(() => {
                    tpDimesion.playSound("mob.shulker.teleport", targetCoordinates, { volume: 1 });
                    tpDimesion.runCommand(`particle nps_ec:teleport_center ${targetCoordinates.x} ${targetCoordinates.y + 1.009} ${targetCoordinates.z}`)
                    tpDimesion.runCommand(`particle nps_ec:teleport_smoke ${targetCoordinates.x} ${targetCoordinates.y + 1.5} ${targetCoordinates.z}`)
                    system.runTimeout(() => {
                        tpDimesion.runCommand(`particle nps_ec:teleport_smoke ${targetCoordinates.x} ${targetCoordinates.y + 1.5} ${targetCoordinates.z}`)
                    }, 30)
                }, 1)
            })
        } catch { }
    } else {
        tpAction.button('view.button_2.teleport_in')
        tpAction.show(player).then((data) => {
            if (data.canceled) return;
        })
    }
}