import { Block, BlockEvent, BlockPermutation, Player, world } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";
import "./entities_controller";

world.beforeEvents.worldInitialize.subscribe((event) => {
    if (world.getDynamicProperty("CanMobsSpawn") == undefined) {
        world.setDynamicProperty("CanMobsSpawn", true);
    }

    const preventMonsterSpawning = event.blockComponentRegistry.registerCustomComponent(
        "ns_ma:prevent_monster_spawn",
        {
            onTick: (BlockEvent) => {
                const { block } = BlockEvent;

                const areaSpawn =
                    block.permutation.getState("ns_ma:activated") == 1 ? true : false ?? false;

                if (areaSpawn == false) {
                    const location = block.location;

                    const entities = block.dimension.getEntities({
                        location: location,
                        families: ["ns_ma"],
                        excludeFamilies: ["passive"],
                        maxDistance: 32,
                    });

                    entities.forEach((entity) => {
                        if (entity.spawning == true) {
                            entity.remove();
                        }
                    });
                }
            },
        }
    );

    const spawnSwitcher = event.blockComponentRegistry.registerCustomComponent(
        "ns_ma:spawn_switcher",
        {
            onPlayerInteract: (BlockEvent) => {
                const { player, block } = BlockEvent;

                tranquilityLanternMenu(player, block);

                //console.warn(block.permutation.getState("ns_ma:activated"));
            },
        }
    );
});

/**
 *
 * @param {Player} player
 * @param {Block} block
 */

function tranquilityLanternMenu(player, block) {
    const UI = new ModalFormData();

    const canMobsSpawn = world.getDynamicProperty("CanMobsSpawn");
    const areaSpawn = block.permutation.getState("ns_ma:activated") == 1 ? true : false;

    UI.title("Spawn Switcher");

    UI.toggle("Global Spawn", canMobsSpawn);
    UI.toggle("Area Spawn", areaSpawn);

    UI.submitButton("Confirm");

    UI.show(player).then((response) => {
        if (response.canceled) {
            return;
        } else {
            try {
                const [globalSpawnToggle, areaSpawnToggle] = response.formValues;

                if (globalSpawnToggle == true) {
                    world.setDynamicProperty("CanMobsSpawn", globalSpawnToggle);
                } else {
                    world.setDynamicProperty("CanMobsSpawn", globalSpawnToggle);
                }
                if (areaSpawnToggle == true) {
                    block.setPermutation(
                        BlockPermutation.resolve(block.typeId, {
                            "ns_ma:activated": 1,
                        })
                    );
                } else {
                    block.setPermutation(
                        BlockPermutation.resolve(block.typeId, {
                            "ns_ma:activated": 0,
                        })
                    );
                }
            } catch (e) {
                console.warn(e);
            }
        }
    });
}
