import { system, world } from "@minecraft/server";
import { DynamicEffect } from "../modules/dynamicEffectModule";
import ScriptingModule from "../modules/scriptingModule";
import { namespace } from "./_config";
export default class DynamicEffectModule extends ScriptingModule {
    constructor() {
        super("DynamicEffectModule");
        this.onInitialize = () => {
            this.registerDynamicEffects();
            world.afterEvents.itemUseOn.subscribe((event) => {
                const item = event.itemStack;
                if (item.typeId === "minecraft:ender_eye" && event.block && event.block.location) {
                    const block = event.block;
                    const y = block.location.y + 1;
                    // Spawn particles with error checking
                    try {
                        if (y) {
                            block.dimension.spawnParticle("cc_re:eye_used_x", {
                                x: block.location.x + 0.5,
                                y: y,
                                z: block.location.z + 0.24,
                            });
                            block.dimension.spawnParticle("cc_re:eye_used_x", {
                                x: block.location.x + 0.5,
                                y: y,
                                z: block.location.z + 0.76, // Adjusted to avoid redundancy
                            });
                            block.dimension.spawnParticle("cc_re:eye_used_z", {
                                x: block.location.x + 0.24,
                                y: y,
                                z: block.location.z + 0.5,
                            });
                            block.dimension.spawnParticle("cc_re:eye_used_z", {
                                x: block.location.x + 0.76,
                                y: y,
                                z: block.location.z + 0.5,
                            });
                        }
                    }
                    catch (error) { }
                    function findPortalCenter(block, direction) {
                        const directions = {
                            north: { check: block.north(), right: "west", left: "east", forward: "north" },
                            south: { check: block.south(), right: "east", left: "west", forward: "south" },
                            east: { check: block.east(), right: "south", left: "north", forward: "east" },
                            west: { check: block.west(), right: "north", left: "south", forward: "west" },
                        };
                        const { check, right, left, forward } = directions[direction];
                        if (check && check.typeId === "minecraft:end_portal") {
                            const adjacentRight = check[right] && check[right]();
                            const adjacentLeft = check[left] && check[left]();
                            let centerBlock;
                            if (adjacentLeft?.typeId === "minecraft:end_portal" &&
                                adjacentRight?.typeId === "minecraft:end_portal") {
                                world.sendMessage(`Portal found to the ${direction} center`);
                                centerBlock = check[forward] && check[forward]();
                            }
                            else if (adjacentLeft?.typeId !== "minecraft:end_portal" &&
                                adjacentRight?.typeId === "minecraft:end_portal") {
                                world.sendMessage(`Portal found to the ${direction} left`);
                                centerBlock = check[forward]() && check[forward]()[right]();
                            }
                            else if (adjacentLeft?.typeId === "minecraft:end_portal" &&
                                adjacentRight?.typeId !== "minecraft:end_portal") {
                                world.sendMessage(`Portal found to the ${direction} right`);
                                centerBlock = check[forward]() && check[forward]()[left]();
                            }
                            if (centerBlock) {
                                const x = centerBlock.location.x;
                                const y = centerBlock.location.y;
                                const z = centerBlock.location.z;
                                world.setDynamicProperty(`${namespace}:end_portal_${x},${y},${z}`, true);
                                try {
                                    world.getDimension("overworld").spawnParticle("cc_re:end_fire_fly", {
                                        x: x + 0.5,
                                        y: y + 1,
                                        z: z + 0.5,
                                    });
                                    world.getDimension("overworld").spawnParticle("cc_re:portal_x", {
                                        x: x + 0.5,
                                        y: y + 1,
                                        z: z + 2,
                                    });
                                    world.getDimension("overworld").spawnParticle("cc_re:portal_x", {
                                        x: x + 0.5,
                                        y: y + 1,
                                        z: z - 1,
                                    });
                                    world.getDimension("overworld").spawnParticle("cc_re:portal_z", {
                                        x: x + 2,
                                        y: y + 1,
                                        z: z + 0.5,
                                    });
                                    world.getDimension("overworld").spawnParticle("cc_re:portal_z", {
                                        x: x - 1,
                                        y: y + 1,
                                        z: z + 0.5,
                                    });
                                }
                                catch (error) { }
                            }
                        }
                    }
                    system.runTimeout(() => {
                        ["north", "south", "east", "west"].forEach((direction) => findPortalCenter(block, direction));
                    }, 1);
                }
            });
            system.runInterval(() => {
                const activePortals = world.getDynamicPropertyIds().forEach((id) => {
                    if (id.startsWith(`${namespace}:end_portal_`)) {
                        if (world.getDynamicProperty(id) == false)
                            return;
                        const portal = id.split("_")[3];
                        const [x, y, z] = portal.split(",").map((coord) => parseInt(coord, 10) || 0); // Default to 0 if undefined
                        if (x && y && z) {
                            const block = world.getDimension("overworld").getBlock({ x, y, z });
                            if (block?.typeId === "minecraft:end_portal") {
                                try {
                                    world.getDimension("overworld").spawnParticle("cc_re:end_fire_fly", {
                                        x: x + 0.5,
                                        y: y + 1,
                                        z: z + 0.5,
                                    });
                                    world.getDimension("overworld").spawnParticle("cc_re:portal_x", {
                                        x: x + 0.5,
                                        y: y + 1,
                                        z: z + 2,
                                    });
                                    world.getDimension("overworld").spawnParticle("cc_re:portal_x", {
                                        x: x + 0.5,
                                        y: y + 1,
                                        z: z - 1,
                                    });
                                    world.getDimension("overworld").spawnParticle("cc_re:portal_z", {
                                        x: x + 2,
                                        y: y + 1,
                                        z: z + 0.5,
                                    });
                                    world.getDimension("overworld").spawnParticle("cc_re:portal_z", {
                                        x: x - 1,
                                        y: y + 1,
                                        z: z + 0.5,
                                    });
                                }
                                catch (error) { }
                            }
                        }
                    }
                });
            }, 40);
        };
    }
    registerDynamicEffects() {
        DynamicEffect.register(new DynamicEffect({
            dynamicProperty: "cc_re:allow_particle_water_splash",
            filter: {
                include: "all",
                exclude: ["minecraft:item"],
            },
            effects: {
                onStart: {
                    particles: [
                        "cc_re:splash_1",
                        "cc_re:splash_2",
                        "cc_re:splash_3",
                        "cc_re:splash_4",
                        "cc_re:splash_drops",
                        "cc_re:splash_wave",
                    ],
                    sounds: ["cc_re:splash"],
                },
            },
            conditions: {
                startCondition: (target, tags) => {
                    return target.isInWater && target.getVelocity().y < -0.5;
                },
                intervalCondition: (target, tags) => {
                    return target.isInWater;
                },
                endCondition: (target, tags) => {
                    return !target.isInWater;
                },
            },
        }));
        DynamicEffect.register(new DynamicEffect({
            dynamicProperty: "cc_re:allow_particle_water_splash",
            filter: {
                include: "all",
                exclude: ["minecraft:item"],
            },
            effects: {
                onInterval: {
                    particles: [
                        {
                            type: "cc_re:splash_wave",
                            interval: 20,
                        },
                    ],
                },
            },
            conditions: {
                startCondition: (target, tags) => {
                    return (target.isInWater &&
                        target.dimension.getBlock(target.location)?.above()?.typeId === "minecraft:air");
                },
                intervalCondition: (target, tags) => {
                    return (target.isInWater &&
                        target.dimension.getBlock(target.location)?.above()?.typeId === "minecraft:air");
                },
                endCondition: (target, tags) => {
                    return !target.isInWater;
                },
            },
        }));
        DynamicEffect.register(new DynamicEffect({
            dynamicProperty: "cc_re:allow_particle_water_splash",
            filter: {
                isFamily: ["boat"],
                include: "all",
                exclude: ["minecraft:item"],
            },
            effects: {
                onInterval: {
                    particles: [
                        {
                            type: "cc_re:splash_wave_boat",
                            interval: 20,
                        },
                    ],
                },
            },
            conditions: {
                startCondition: (target, tags) => {
                    return (target.isInWater &&
                        target.dimension.getBlock(target.location)?.above()?.typeId === "minecraft:air");
                },
                intervalCondition: (target, tags) => {
                    return (target.isInWater &&
                        target.dimension.getBlock(target.location)?.above()?.typeId === "minecraft:air");
                },
                endCondition: (target, tags) => {
                    return !target.isInWater;
                },
            },
        }));
        DynamicEffect.register(new DynamicEffect({
            dynamicProperty: "cc_re:allow_particle_water_splash",
            filter: {
                include: ["minecraft:item"],
            },
            effects: {
                onStart: {
                    particles: [
                        "cc_re:splash_1_small",
                        "cc_re:splash_2_small",
                        "cc_re:splash_3_small",
                        "cc_re:splash_4_small",
                    ],
                    sounds: ["cc_re:splash_small"],
                },
            },
            conditions: {
                startCondition: (target, tags) => {
                    return target.isInWater && target.getVelocity().y < -0.2;
                },
                intervalCondition: (target, tags) => {
                    return target.isInWater;
                },
                endCondition: (target, tags) => {
                    return !target.isInWater;
                },
            },
        }));
        // Particle
        DynamicEffect.register(new DynamicEffect({
            dynamicProperty: "cc_re:allow_particle_water_splash",
            filter: {
                include: "all",
                isFamily: ["boat"],
            },
            effects: {
                onStart: {
                    particles: ["cc_re:splash_drops_boat", "cc_re:splash_wave_boat"],
                },
                onInterval: {
                    particles: [
                        { type: "cc_re:splash_wave_boat", interval: 2 },
                        { type: "cc_re:splash_drops_boat", interval: 6 },
                    ],
                },
            },
            conditions: {
                startCondition: (target, tags) => {
                    return (target.isInWater &&
                        (target.getVelocity().x > 0.2 ||
                            target.getVelocity().x < -0.2 ||
                            target.getVelocity().z > 0.2 ||
                            target.getVelocity().z < -0.2));
                },
                intervalCondition: (target, tags) => {
                    return (target.isInWater &&
                        (target.getVelocity().x > 0.2 ||
                            target.getVelocity().x < -0.2 ||
                            target.getVelocity().z > 0.2 ||
                            target.getVelocity().z < -0.2));
                },
                endCondition: (target, tags) => {
                    return (!target.isInWater ||
                        (Math.abs(target.getVelocity().x) <= 0.2 && Math.abs(target.getVelocity().z) <= 0.2));
                },
            },
        }));
    }
}
