import {
    EntityComponentTypes,
    EntityEquippableComponent,
    EquipmentSlot,
    Player,
    system,
    world,
} from "@minecraft/server";
import { ActionFormData, FormCancelationReason, MessageFormData } from "@minecraft/server-ui";

/**
 *
 * @param {Player} player
 */
export function warningMenu(player) {
    player.menuOpen = true;

    const UI = new ActionFormData();

    const warnText = `§e⚠ IMPORTANT WARNING ⚠§r:

This game contains §ejumpscares, loud sounds, and elements that may be frightening to some players.§r

If you are sensitive to sudden scares or have issues with horror content, play with caution.

\ue104 Headphones are recommended for a better experience.

\ue103 You have been warned! Good luck.


`;

    UI.title("Warning");

    UI.body(warnText);

    UI.button("Confirm");

    UI.show(player).then((response) => {
        if (
            response.cancelationReason == FormCancelationReason.UserClosed ||
            response.cancelationReason == FormCancelationReason.UserBusy
        ) {
            player.setDynamicProperty("Warned", false);
        } else if (response.canceled == true) {
            player.setDynamicProperty("Warned", true);
        }

        if (response.selection == 0) {
            player.setDynamicProperty("Warned", true);
        }
    });
}

/**
 *
 * @param {Player} player
 */
export function summonRelicUseWarnMenu(player) {
    const UI = new MessageFormData();

    const playerLocation = player.location;

    UI.title("");
    UI.body(
        "This fight will teleport you to far away up the skies to battle with The Lich. Are you ready?"
    );

    UI.button1("Yes");
    UI.button2("No");

    UI.show(player).then((response) => {
        if (
            response.canceled == true ||
            response.cancelationReason == FormCancelationReason.UserClosed
        ) {
            return;
        }

        if (response.selection == 0) {
            //Clear used Item

            /**
             * @type {EntityEquippableComponent}
             */
            const equippableComponent = player.getComponent(EntityComponentTypes.Equippable);

            equippableComponent.setEquipment(EquipmentSlot.Mainhand);

            //...

            //Define structure spawn heigth
            const getSpawnHeigth =
                playerLocation.y + 100 < player.dimension.heightRange.max - 32
                    ? playerLocation.y + 100
                    : player.dimension.heightRange.max - 32;
            //...

            //generate the structure
            const structure = world.structureManager.place("ns_rh:coliseum", player.dimension, {
                x: playerLocation.x - 16,
                y: getSpawnHeigth,
                z: playerLocation.z - 16,
            });
            //...

            //Define where player will spawn on the structure after teleported.
            const playerSpawnLocation = {
                x: playerLocation.x,
                y: getSpawnHeigth + 7,
                z: playerLocation.z,
            };
            //...

            //Spawn return portal
            const portal = player.dimension.spawnEntity("ns_rh:portal", {
                x: playerLocation.x,
                y: playerLocation.y + 1,
                z: playerLocation.z,
            });

            portal.setProperty("self:teleport_to", "coliseum");

            portal.setDynamicProperty("arena_location", {
                x: playerLocation.x - 16,
                y: getSpawnHeigth,
                z: playerLocation.z - 16,
            });
            //...

            //Teleport player to the structure after n seconds
            system.runTimeout(() => {
                player.teleport(playerSpawnLocation);
            }, 5 * 20);
            //...
        }
        if (response.selection == 1) {
            return;
        }
    });
}
