import {
    BlockPermutation,
    EntityComponentTypes,
    EntityEquippableComponent,
    EquipmentSlot,
    GameMode,
    world,
} from "@minecraft/server";
import { Entities, EntitiesMap } from "../data/entities";
import { handleLectern } from "./cursed_lectern";

world.beforeEvents.worldInitialize.subscribe((event) => {
    const cursed_lectern = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:cursed_lectern",
        {
            onPlayerInteract: (bEvent) => {
                const { block, player } = bEvent;
                /**
                 * @type {EntityEquippableComponent}
                 */
                const equippable = player.getComponent(EntityComponentTypes.Equippable);
                const heldItem = equippable.getEquipment(EquipmentSlot.Mainhand);

                handleLectern(player, block, heldItem);
            },
        }
    );

    const wise_wood_log = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:wise_wood_log",
        {
            onTick: (bEvent) => {},
        }
    );
    const boss_spawner = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:boss_spawner",
        {
            onTick: (bEvent) => {
                const { block, player } = bEvent;

                const nearbyPlayer = block.dimension.getPlayers({
                    location: block.location,
                    maxDistance: 16,
                    closest: 1,
                })[0];

                if (
                    (nearbyPlayer && nearbyPlayer?.getGameMode() == GameMode.survival) ||
                    nearbyPlayer?.getGameMode() == GameMode.adventure ||
                    nearbyPlayer?.getGameMode() == GameMode.creative
                ) {
                    const bossState = block.permutation.getState("self:boss");

                    switch (bossState) {
                        case "black_widow": {
                            const boss = block.dimension.spawnEntity("ns_rh:black_widow", {
                                x: block.location.x,
                                y: block.location.y + 1,
                                z: block.location.z,
                            });

                            if (boss) {
                                block.setType("minecraft:air");
                            }
                            break;
                        }
                        case "dracula": {
                            const boss = block.dimension.spawnEntity("ns_rh:dracula", {
                                x: block.location.x,
                                y: block.location.y + 1,
                                z: block.location.z,
                            });

                            if (boss) {
                                block.setType("minecraft:air");
                            }
                            break;
                        }
                        case "smiler": {
                            const nearbyBoss = block.dimension.getEntities({
                                location: block.location,
                                type: Entities.the_smiler.typeId,
                                maxDistance: 32,
                            });

                            //if theres already has a boss spawned in the backroom another boss will not spawn

                            //  console.warn(nearbyBoss.length);

                            if (nearbyBoss.length > 0) {
                                block.setType("minecraft:air");
                                return;
                            }

                            const boss = block.dimension.spawnEntity("ns_rh:the_smiler", {
                                x: block.location.x,
                                y: block.location.y + 1,
                                z: block.location.z,
                            });

                            // boss.triggerEvent("self:spawn_as_boss");

                            if (boss) {
                                block.setType("minecraft:air");
                            }

                            break;
                        }
                        case "madre": {
                            const boss = block.dimension.spawnEntity("ns_rh:madre", {
                                x: block.location.x,
                                y: block.location.y + 1,
                                z: block.location.z,
                            });

                            if (boss) {
                                block.setType("minecraft:air");
                            }
                            break;
                        }
                        case "lich": {
                            const boss = block.dimension.spawnEntity("ns_rh:nightmare", {
                                x: block.location.x,
                                y: block.location.y + 1,
                                z: block.location.z,
                            });

                            if (boss) {
                                block.setType("minecraft:air");
                            }
                            break;
                        }
                        case "kid_the_clown": {
                            const boss = block.dimension.spawnEntity("ns_rh:kid_the_clown", {
                                x: block.location.x,
                                y: block.location.y + 1,
                                z: block.location.z,
                            });

                            if (boss) {
                                block.setType("minecraft:air");
                            }
                            break;
                        }
                        case "scarecrow": {
                            const boss = block.dimension.spawnEntity("ns_rh:scarecrow", {
                                x: block.location.x,
                                y: block.location.y + 1,
                                z: block.location.z,
                            });

                            if (boss) {
                                block.setType("minecraft:air");
                            }
                            break;
                        }
                        case "plague_doctor": {
                            const boss = block.dimension.spawnEntity("ns_rh:scp_049", {
                                x: block.location.x,
                                y: block.location.y + 1,
                                z: block.location.z,
                            });

                            if (boss) {
                                block.setType("minecraft:air");
                            }
                            break;
                        }
                        case "mrbear": {
                            const boss = block.dimension.spawnEntity("ns_rh:mrbear", {
                                x: block.location.x,
                                y: block.location.y + 1,
                                z: block.location.z,
                            });

                            if (boss) {
                                block.setType("minecraft:air");
                            }
                            break;
                        }
                    }
                }
            },

            onPlayerInteract: (bEvent) => {
                const { block, player } = bEvent;

                const bossState = block.permutation.getState("self:boss");

                const bosses = [
                    "none",
                    "mrbear",
                    "black_widow",
                    "dracula",
                    "kid_the_clown",
                    "lich",
                    "madre",
                    "plague_doctor",
                    "scarecrow",
                    "smiler",
                ];

                const currentIndex = bosses.indexOf(bossState);
                const nextBoss =
                    bosses[currentIndex + 1 > bosses.length - 1 ? 1 : currentIndex + 1];

                block.setPermutation(
                    BlockPermutation.resolve(block.typeId, {
                        "self:boss": nextBoss,
                    })
                );

                //  console.warn(block.permutation.getState("self:boss"));
            },
        }
    );

    const barn_mobs_spawner = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:barn_mobs_spawner",
        {
            onTick: (bEvent) => {
                const { block, player } = bEvent;

                const nearbyPlayer = block.dimension.getPlayers({
                    location: block.location,
                    maxDistance: 16,
                    closest: 1,
                })[0];

                if (
                    (nearbyPlayer && nearbyPlayer?.getGameMode() == GameMode.survival) ||
                    nearbyPlayer?.getGameMode() == GameMode.adventure ||
                    nearbyPlayer?.getGameMode() == GameMode.creative
                ) {
                    const mobState = block.permutation.getState("self:mob");

                    switch (mobState) {
                        case mobState: {
                            const mobId = EntitiesMap.get(mobState)?.typeId;

                            if (mobId) {
                                const boss = block.dimension.spawnEntity(mobId, {
                                    x: block.location.x,
                                    y: block.location.y + 1,
                                    z: block.location.z,
                                });

                                if (boss) {
                                    block.setType("minecraft:air");
                                }
                            }

                            break;
                        }
                    }
                }
            },

            onPlayerInteract: (bEvent) => {
                const { block, player } = bEvent;

                const mobState = block.permutation.getState("self:mob");

                const bosses = ["none", "weeping_hound", "wendigo"];

                const currentIndex = bosses.indexOf(mobState);
                const nextMob = bosses[currentIndex + 1 > bosses.length - 1 ? 1 : currentIndex + 1];

                block.setPermutation(
                    BlockPermutation.resolve(block.typeId, {
                        "self:mob": nextMob,
                    })
                );

                //  console.warn(block.permutation.getState("self:mob"));
            },
        }
    );
    const animatronic_mobs_spawner = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:animatronic_mobs_spawner",
        {
            onTick: (bEvent) => {
                const { block, player } = bEvent;

                const nearbyPlayer = block.dimension.getPlayers({
                    location: block.location,
                    maxDistance: 16,
                    closest: 1,
                })[0];

                if (
                    (nearbyPlayer && nearbyPlayer?.getGameMode() == GameMode.survival) ||
                    nearbyPlayer?.getGameMode() == GameMode.adventure ||
                    nearbyPlayer?.getGameMode() == GameMode.creative
                ) {
                    const mobState = block.permutation.getState("self:mob");

                    switch (mobState) {
                        case mobState: {
                            const mobId = EntitiesMap.get(mobState)?.typeId;

                            if (mobId) {
                                const boss = block.dimension.spawnEntity(mobId, {
                                    x: block.location.x,
                                    y: block.location.y + 1,
                                    z: block.location.z,
                                });

                                if (boss) {
                                    block.setType("minecraft:air");
                                }
                            }

                            break;
                        }
                    }
                }
            },

            onPlayerInteract: (bEvent) => {
                const { block, player } = bEvent;

                const mobState = block.permutation.getState("self:mob");

                const bosses = ["none", "mrcuddles", "mrrabbit"];

                const currentIndex = bosses.indexOf(mobState);
                const nextMob = bosses[currentIndex + 1 > bosses.length - 1 ? 1 : currentIndex + 1];

                block.setPermutation(
                    BlockPermutation.resolve(block.typeId, {
                        "self:mob": nextMob,
                    })
                );

                //    console.warn(block.permutation.getState("self:mob"));
            },
        }
    );
    const backrooms_mobs_spawner = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:backrooms_mobs_spawner",
        {
            onTick: (bEvent) => {
                const { block, player } = bEvent;

                const nearbyPlayer = block.dimension.getPlayers({
                    location: block.location,
                    maxDistance: 16,
                    closest: 1,
                })[0];

                if (
                    (nearbyPlayer && nearbyPlayer?.getGameMode() == GameMode.survival) ||
                    nearbyPlayer?.getGameMode() == GameMode.adventure ||
                    nearbyPlayer?.getGameMode() == GameMode.creative
                ) {
                    const mobState = block.permutation.getState("self:mob");

                    switch (mobState) {
                        case mobState: {
                            const mobId = EntitiesMap.get(mobState)?.typeId;

                            if (mobId) {
                                const boss = block.dimension.spawnEntity(mobId, {
                                    x: block.location.x,
                                    y: block.location.y + 1,
                                    z: block.location.z,
                                });

                                if (boss) {
                                    block.setType("minecraft:air");
                                }
                            }

                            break;
                        }
                    }
                }
            },

            onPlayerInteract: (bEvent) => {
                const { block, player } = bEvent;

                const mobState = block.permutation.getState("self:mob");

                const bosses = ["none", "the_shadow", "the_pecker"];

                const currentIndex = bosses.indexOf(mobState);
                const nextMob = bosses[currentIndex + 1 > bosses.length - 1 ? 1 : currentIndex + 1];

                block.setPermutation(
                    BlockPermutation.resolve(block.typeId, {
                        "self:mob": nextMob,
                    })
                );

                //   console.warn(block.permutation.getState("self:mob"));
            },
        }
    );
    const circus_mobs_spawner = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:circus_mobs_spawner",
        {
            onTick: (bEvent) => {
                const { block, player } = bEvent;

                const nearbyPlayer = block.dimension.getPlayers({
                    location: block.location,
                    maxDistance: 16,
                    closest: 1,
                })[0];

                if (
                    (nearbyPlayer && nearbyPlayer?.getGameMode() == GameMode.survival) ||
                    nearbyPlayer?.getGameMode() == GameMode.adventure ||
                    nearbyPlayer?.getGameMode() == GameMode.creative
                ) {
                    const mobState = block.permutation.getState("self:mob");

                    switch (mobState) {
                        case mobState: {
                            const mobId = EntitiesMap.get(mobState)?.typeId;

                            if (mobId) {
                                const boss = block.dimension.spawnEntity(mobId, {
                                    x: block.location.x,
                                    y: block.location.y + 1,
                                    z: block.location.z,
                                });

                                if (boss) {
                                    block.setType("minecraft:air");
                                }
                            }

                            break;
                        }
                    }
                }
            },

            onPlayerInteract: (bEvent) => {
                const { block, player } = bEvent;

                const mobState = block.permutation.getState("self:mob");

                const bosses = ["none", "riber_the_jester", "dreezy_the_mask"];

                const currentIndex = bosses.indexOf(mobState);
                const nextMob = bosses[currentIndex + 1 > bosses.length - 1 ? 1 : currentIndex + 1];

                block.setPermutation(
                    BlockPermutation.resolve(block.typeId, {
                        "self:mob": nextMob,
                    })
                );

                //  console.warn(block.permutation.getState("self:mob"));
            },
        }
    );
    const classic_mobs_spawner = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:classic_mobs_spawner",
        {
            onTick: (bEvent) => {
                const { block, player } = bEvent;

                const nearbyPlayer = block.dimension.getPlayers({
                    location: block.location,
                    maxDistance: 16,
                    closest: 1,
                })[0];

                if (
                    (nearbyPlayer && nearbyPlayer?.getGameMode() == GameMode.survival) ||
                    nearbyPlayer?.getGameMode() == GameMode.adventure ||
                    nearbyPlayer?.getGameMode() == GameMode.creative
                ) {
                    const mobState = block.permutation.getState("self:mob");

                    switch (mobState) {
                        case mobState: {
                            const mobId = EntitiesMap.get(mobState)?.typeId;

                            if (mobId) {
                                const boss = block.dimension.spawnEntity(mobId, {
                                    x: block.location.x,
                                    y: block.location.y + 1,
                                    z: block.location.z,
                                });

                                if (boss) {
                                    block.setType("minecraft:air");
                                }
                            }

                            break;
                        }
                    }
                }
            },

            onPlayerInteract: (bEvent) => {
                const { block, player } = bEvent;

                const mobState = block.permutation.getState("self:mob");

                const bosses = ["none", "frankenstein", "werewolf"];

                const currentIndex = bosses.indexOf(mobState);
                const nextMob = bosses[currentIndex + 1 > bosses.length - 1 ? 1 : currentIndex + 1];

                block.setPermutation(
                    BlockPermutation.resolve(block.typeId, {
                        "self:mob": nextMob,
                    })
                );

                //  console.warn(block.permutation.getState("self:mob"));
            },
        }
    );
    const doll_mobs_spawner = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:doll_mobs_spawner",
        {
            onTick: (bEvent) => {
                const { block, player } = bEvent;

                const nearbyPlayer = block.dimension.getPlayers({
                    location: block.location,
                    maxDistance: 16,
                    closest: 1,
                })[0];

                if (
                    (nearbyPlayer && nearbyPlayer?.getGameMode() == GameMode.survival) ||
                    nearbyPlayer?.getGameMode() == GameMode.adventure ||
                    nearbyPlayer?.getGameMode() == GameMode.creative
                ) {
                    const mobState = block.permutation.getState("self:mob");

                    switch (mobState) {
                        case mobState: {
                            const mobId = EntitiesMap.get(mobState)?.typeId;

                            if (mobId) {
                                const boss = block.dimension.spawnEntity(mobId, {
                                    x: block.location.x,
                                    y: block.location.y + 1,
                                    z: block.location.z,
                                });

                                if (boss) {
                                    block.setType("minecraft:air");
                                }
                            }

                            break;
                        }
                    }
                }
            },

            onPlayerInteract: (bEvent) => {
                const { block, player } = bEvent;

                const mobState = block.permutation.getState("self:mob");

                const bosses = ["none", "alex", "giulia"];

                const currentIndex = bosses.indexOf(mobState);
                const nextMob = bosses[currentIndex + 1 > bosses.length - 1 ? 1 : currentIndex + 1];

                block.setPermutation(
                    BlockPermutation.resolve(block.typeId, {
                        "self:mob": nextMob,
                    })
                );

                //     console.warn(block.permutation.getState("self:mob"));
            },
        }
    );
    const phobia_mobs_spawner = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:phobia_mobs_spawner",
        {
            onTick: (bEvent) => {
                const { block, player } = bEvent;

                const nearbyPlayer = block.dimension.getPlayers({
                    location: block.location,
                    maxDistance: 16,
                    closest: 1,
                })[0];

                if (
                    (nearbyPlayer && nearbyPlayer?.getGameMode() == GameMode.survival) ||
                    nearbyPlayer?.getGameMode() == GameMode.adventure ||
                    nearbyPlayer?.getGameMode() == GameMode.creative
                ) {
                    const mobState = block.permutation.getState("self:mob");

                    switch (mobState) {
                        case mobState: {
                            const mobId = EntitiesMap.get(mobState)?.typeId;

                            if (mobId) {
                                const boss = block.dimension.spawnEntity(mobId, {
                                    x: block.location.x,
                                    y: block.location.y + 1,
                                    z: block.location.z,
                                });

                                if (boss) {
                                    block.setType("minecraft:air");
                                }
                            }

                            break;
                        }
                    }
                }
            },

            onPlayerInteract: (bEvent) => {
                const { block, player } = bEvent;

                const mobState = block.permutation.getState("self:mob");

                const bosses = ["none", "ghost", "the_swarm"];

                const currentIndex = bosses.indexOf(mobState);
                const nextMob = bosses[currentIndex + 1 > bosses.length - 1 ? 1 : currentIndex + 1];

                block.setPermutation(
                    BlockPermutation.resolve(block.typeId, {
                        "self:mob": nextMob,
                    })
                );

                //     console.warn(block.permutation.getState("self:mob"));
            },
        }
    );
    const scp_mobs_spawner = event.blockComponentRegistry.registerCustomComponent(
        "ns_rh:scp_mobs_spawner",
        {
            onTick: (bEvent) => {
                const { block, player } = bEvent;

                const nearbyPlayer = block.dimension.getPlayers({
                    location: block.location,
                    maxDistance: 16,
                    closest: 1,
                })[0];

                if (
                    (nearbyPlayer && nearbyPlayer?.getGameMode() == GameMode.survival) ||
                    nearbyPlayer?.getGameMode() == GameMode.adventure ||
                    nearbyPlayer?.getGameMode() == GameMode.creative
                ) {
                    const mobState = block.permutation.getState("self:mob");

                    switch (mobState) {
                        case mobState: {
                            const mobId = EntitiesMap.get(mobState)?.typeId;

                            if (mobId) {
                                const boss = block.dimension.spawnEntity(mobId, {
                                    x: block.location.x,
                                    y: block.location.y + 1,
                                    z: block.location.z,
                                });

                                if (boss) {
                                    block.setType("minecraft:air");
                                }
                            }

                            break;
                        }
                    }
                }
            },

            onPlayerInteract: (bEvent) => {
                const { block, player } = bEvent;

                const mobState = block.permutation.getState("self:mob");

                const bosses = ["none", "scp_096", "scp_173"];

                const currentIndex = bosses.indexOf(mobState);
                const nextMob = bosses[currentIndex + 1 > bosses.length - 1 ? 1 : currentIndex + 1];

                block.setPermutation(
                    BlockPermutation.resolve(block.typeId, {
                        "self:mob": nextMob,
                    })
                );

                //     console.warn(block.permutation.getState("self:mob"));
            },
        }
    );

    //....
});
