import * as mc from "@minecraft/server";
import { itemLores } from "../items/itemLores";
mc.system.runInterval(() => {
    const players = mc.world.getAllPlayers();
    players.forEach(player => {
        const inv = player?.getComponent("inventory");
        const invContainer = inv?.container;
        const hand = player?.getComponent("equippable").getEquipmentSlot("Mainhand").getItem();
        itemLores(inv, invContainer);

        if (hand && hand?.typeId === `cy_cr:redstone_capsule`) {
            const lores = hand.getLore();
            let mobInside = false;
            lores.forEach(lore => {
                if (lore.includes("Captured mob")) {
                    mobInside = true;
                }
            });

            if (!mobInside) {
                player.onScreenDisplay.setActionBar([{translate: "translate.cy_cr.redstone_capsule_empty"}])
            } else {
                player.onScreenDisplay.setActionBar([{translate: "translate.cy_cr.redstone_capsule_full"}])
            }
        }
    });

    const getFurniture = mc.world.getDimension("overworld").getEntities({ families: ["cy_cr:has_openable_container"] });
    const getFurnitureN = mc.world.getDimension("nether").getEntities({ families: ["cy_cr:has_openable_container"] });
    const getFurnitureE = mc.world.getDimension("the_end").getEntities({ families: ["cy_cr:has_openable_container"] });
    getFurniture.push(...getFurnitureN, ...getFurnitureE);
    try {
        for (const furniture of getFurniture) {
            const players = furniture.dimension.getEntities({
                type: "minecraft:player",
                location: furniture.location,
                maxDistance: 7,
            });

            let isLookedAt = false;

            if (players) {
                for (const player of players) {
                    const familyComponent = furniture.getComponent("minecraft:type_family");
                    if (familyComponent) {
                        const lookAtBlock = player.getBlockFromViewDirection({
                            includeTypes: ["minecraft:barrier"],
                            maxDistance: 7
                        });

                        if (lookAtBlock) {
                            const machines = ["cy_cr:arrow_farm", "cy_cr:cobble_factory", "cy_cr:potion_farm", "cy_cr:string_farm", "cy_cr:wood_factory", "cy_cr:berry_farm", "cy_cr:beetroot_farm", "cy_cr:carrot_farm", "cy_cr:netherwart_farm", "cy_cr:potato_farm", "cy_cr:wheat_farm", "cy_cr:sugar_farm", "cy_cr:eggs_farm", "cy_cr:furnace"]
                            const loc = lookAtBlock.block.location;
                            loc.x += 0.5;
                            loc.z += 0.5;
                            let maxDist = 0;
                            if (machines.includes(furniture.typeId)) {
                                maxDist = 4
                            } else if (furniture.typeId === `cy_cr:tear_farm`) {
                                maxDist = 15
                            } else if (furniture.typeId === `cy_cr:cake_farm`) {
                                maxDist = 16
                            } else {
                                maxDist = 0.5
                            };
                            const entity = furniture.dimension.getEntities({
                                families: furniture.getComponent("type_family").getTypeFamilies(),
                                location: loc,
                                maxDistance: maxDist,
                                closest: 1,
                            })[0];

                            if (entity && entity.id === furniture.id) {
                                player.addTag("cy_cr:look_at_furniture");
                                isLookedAt = true;

                            }
                        }
                    }
                };

                if (isLookedAt) {
                    furniture.addTag("cy_cr:is_looked_at");
                } else {
                    furniture.removeTag("cy_cr:is_looked_at");
                }
            }
        }
    } catch (error) {
        console.log(error);
    }
});
