import { world } from "@minecraft/server";

import { portalActivation, portalEnter, portalReturn } from "../ui/portal";

world.afterEvents.playerInteractWithEntity.subscribe((event) => {
    const { player, target } = event;

    if (target.typeId === "pixell_bbo:portal") {
        /**
         * @type {string|undefined}
         */
        const portalArena = target.getDynamicProperty("target");

        if (!portalArena) return;

        portalActivation(player, target, portalArena);
    }
});

world.afterEvents.dataDrivenEntityTrigger.subscribe((event) => {
    const { entity, eventId } = event;

    if (entity.typeId === "pixell_bbo:portal") {
        const portalShowPropName = "ninja:portal_ui_show";

        switch (eventId) {
            case "self:show_ui": {
                try {
                    const playersInside = entity?.dimension?.getPlayers({
                        location: entity.location,
                        excludeTags: ["pixell_bbo:teleporting_to_arena"],
                        maxDistance: 2,
                    });

                    const playersOutside = entity?.dimension?.getPlayers({
                        location: entity.location,
                        minDistance: 2,
                        maxDistance: 4,
                    });

                    for (let player of playersInside) {
                        /**
                         * @type {boolean | undefined}
                         */
                        const playerShowCooldown = player.getDynamicProperty(portalShowPropName);

                        if (!playerShowCooldown) {
                            /**
                             * @type {string | undefined}
                             */
                            const portalArena = entity.getDynamicProperty("target");

                            if (!portalArena) return;

                            player.setDynamicProperty(portalShowPropName, true);

                            const isReturnPortal =
                                entity.getDynamicProperty("pixell_bbo:return_portal") ?? false;

                            if (isReturnPortal) {
                                portalReturn(player, portalArena);

                                continue;
                            }

                            if (!entity.getProperty("self:activated")) {
                                portalActivation(player, entity, portalArena);
                            }

                            if (entity.getProperty("self:activated")) {
                                portalEnter(player, entity, portalArena);
                            }

                            continue;
                        }
                    }

                    for (let player of playersOutside) {
                        /**
                         * @type {boolean | undefined}
                         */
                        const playerShowCooldown = player.getDynamicProperty(portalShowPropName);

                        if (playerShowCooldown == undefined) {
                            player.setDynamicProperty(portalShowPropName, false);
                        }

                        if (playerShowCooldown) {
                            player.setDynamicProperty(portalShowPropName, false);
                        }

                        continue;
                    }
                } catch (error) {}

                break;
            }

            default: {
                break;
            }
        }
    }
});
