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

class Totem {
    constructor(pattern, resultEntity, spawnType, spawnOffset = { x: 0.5, y: -1, z: 0.5 }) {
        this.pattern = pattern;
        this.resultEntity = resultEntity;
        this.spawnType = spawnType;
        this.spawnOffset = spawnOffset;
    }

    checkPattern(location, dimension, direction) {
        let patternToCheck = this.pattern;
        if (direction === "z") {
            patternToCheck = this.pattern.map(pos => {
                return {
                    x: pos.z,
                    y: pos.y,
                    z: pos.x,
                    block: pos.block
                };
            });
        } else if (direction === "x_mirror") {
            patternToCheck = this.pattern.map(pos => {
                return {
                    x: -pos.x,
                    y: pos.y,
                    z: pos.z,
                    block: pos.block
                };
            });
        } else if (direction === "z_mirror") {
            patternToCheck = this.pattern.map(pos => {
                return {
                    x: pos.z,
                    y: pos.y,
                    z: -pos.x,
                    block: pos.block
                };
            });
        }

        for (const pos of patternToCheck) {
            const blockLoc = {
                x: Math.floor(location.x + pos.x),
                y: Math.floor(location.y + pos.y),
                z: Math.floor(location.z + pos.z)
            };
            const block = dimension.getBlock(blockLoc);
            if (!block || block.typeId !== pos.block) {
                return false;
            }
        }
        return true;
    }

    spawn(location, dimension) {
        const spawnLoc = {
            x: location.x + this.spawnOffset.x,
            y: location.y + this.spawnOffset.y,
            z: location.z + this.spawnOffset.z
        };

        // Spawn efektleri
        dimension.runCommand(`particle minecraft:huge_explosion_emitter ${spawnLoc.x} ${spawnLoc.y} ${spawnLoc.z}`);
        dimension.runCommand(`execute positioned ${spawnLoc.x} ${spawnLoc.y} ${spawnLoc.z} run fill ~3~10~3 ~-3~~-3 air destroy`)
        dimension.runCommand(`execute positioned ${spawnLoc.x} ${spawnLoc.y} ${spawnLoc.z} run playsound random.explode @a[r=10] ~~~`);

        if (this.spawnType) {
            dimension.runCommand(`summon ${this.resultEntity} ${spawnLoc.x} ${spawnLoc.y - 1} ${spawnLoc.z} 0 0 bunnystudios_rpg:${this.spawnType}`);
        } else {
            dimension.runCommand(`summon ${this.resultEntity} ${spawnLoc.x} ${spawnLoc.y - 1} ${spawnLoc.z} 0 0`);
        }
    }

    removeStructure(location, dimension, pattern, direction) {
        let patternToRemove = pattern;
        if (direction === "z") {
            patternToRemove = pattern.map(pos => {
                return {
                    x: pos.z,
                    y: pos.y,
                    z: pos.x,
                    block: pos.block
                };
            });
        } else if (direction === "x_mirror") {
            patternToRemove = pattern.map(pos => {
                return {
                    x: -pos.x,
                    y: pos.y,
                    z: pos.z,
                    block: pos.block
                };
            });
        } else if (direction === "z_mirror") {
            patternToRemove = pattern.map(pos => {
                return {
                    x: pos.z,
                    y: pos.y,
                    z: -pos.x,
                    block: pos.block
                };
            });
        }

        for (const pos of patternToRemove) {
            const blockLoc = {
                x: Math.floor(location.x + pos.x),
                y: Math.floor(location.y + pos.y),
                z: Math.floor(location.z + pos.z)
            };
            dimension.getBlock(blockLoc).setType("minecraft:air");
        }
    }
}

// Patterns
const bouldrax = new Totem(
    [
        { x: 1, y: 1, z: 0, block: "minecraft:coarse_dirt" },
        { x: -1, y: 1, z: 0, block: "minecraft:coarse_dirt" },
        { x: 0, y: 0, z: 0, block: "minecraft:coarse_dirt" },
        { x: -1, y: 0, z: 0, block: "minecraft:coarse_dirt" },
        { x: 1, y: 0, z: 0, block: "minecraft:coarse_dirt" },
        { x: 0, y: -1, z: 0, block: "minecraft:coarse_dirt" },
    ],
    "bunnystudios_rpg:bouldrax",
    null,
    { x: 0.5, y: 0, z: 0.5 }
);


const electroCube = new Totem(
    [
        { x: 0, y: 0, z: 0, block: "minecraft:amethyst_block" },
        { x: 0, y: -1, z: 0, block: "minecraft:amethyst_block" },
        { x: -1, y: 0, z: 0, block: "minecraft:amethyst_block" },
        { x: -1, y: -1, z: 0, block: "minecraft:amethyst_block" },
        { x: -1, y: 0, z: -1, block: "minecraft:amethyst_block" },
        { x: -1, y: -1, z: -1, block: "minecraft:amethyst_block" },
        { x: 0, y: 0, z: -1, block: "minecraft:amethyst_block" },
        { x: 0, y: -1, z: -1, block: "minecraft:amethyst_block" },
    ],
    "bunnystudios_rpg:cube",
    "spawn_electro",
    { x: 1, y: 0, z: -1 }
);

const fireCube = new Totem(
    [
        { x: 0, y: 0, z: 0, block: "minecraft:magma" },
        { x: 0, y: -1, z: 0, block: "minecraft:magma" },
        { x: -1, y: 0, z: 0, block: "minecraft:magma" },
        { x: -1, y: -1, z: 0, block: "minecraft:magma" },
        { x: -1, y: 0, z: -1, block: "minecraft:magma" },
        { x: -1, y: -1, z: -1, block: "minecraft:magma" },
        { x: 0, y: 0, z: -1, block: "minecraft:magma" },
        { x: 0, y: -1, z: -1, block: "minecraft:magma" },
    ],
    "bunnystudios_rpg:cube",
    "spawn_fire",
    { x: 1, y: 0, z: -1 }
);

const frostCube = new Totem(
    [
        { x: 0, y: 0, z: 0, block: "minecraft:snow" },
        { x: 0, y: -1, z: 0, block: "minecraft:snow" },
        { x: -1, y: 0, z: 0, block: "minecraft:snow" },
        { x: -1, y: -1, z: 0, block: "minecraft:snow" },
        { x: -1, y: 0, z: -1, block: "minecraft:snow" },
        { x: -1, y: -1, z: -1, block: "minecraft:snow" },
        { x: 0, y: 0, z: -1, block: "minecraft:snow" },
        { x: 0, y: -1, z: -1, block: "minecraft:snow" },
    ],
    "bunnystudios_rpg:cube",
    "spawn_frost",
    { x: 1, y: 0, z: -1 }
);


const electroBloom = new Totem(
    [
        { x: 0, y: 0, z: 0, block: "minecraft:amethyst_block" },
        { x: -1, y: 0, z: 0, block: "minecraft:amethyst_block" },
        { x: 1, y: 0, z: 0, block: "minecraft:amethyst_block" },
        { x: 0, y: -1, z: 0, block: "minecraft:amethyst_block" },
    ],
    "bunnystudios_rpg:bloom",
    "spawn_electro",
    { x: 0.5, y: 0, z: 0.5 }
);

const fireBloom = new Totem(
    [
        { x: 0, y: 0, z: 0, block: "minecraft:magma" },
        { x: -1, y: 0, z: 0, block: "minecraft:magma" },
        { x: 1, y: 0, z: 0, block: "minecraft:magma" },
        { x: 0, y: -1, z: 0, block: "minecraft:magma" },
    ],
    "bunnystudios_rpg:bloom",
    "spawn_fire",
    { x: 0.5, y: 0, z: 0.5 }
);

const frostBloom = new Totem(
    [
        { x: 0, y: 0, z: 0, block: "minecraft:snow" },
        { x: -1, y: 0, z: 0, block: "minecraft:snow" },
        { x: 1, y: 0, z: 0, block: "minecraft:snow" },
        { x: 0, y: -1, z: 0, block: "minecraft:snow" },
    ],
    "bunnystudios_rpg:bloom",
    "spawn_frost",
    { x: 0.5, y: 0, z: 0.5 }
);


const twinWolf = new Totem(
    [
        { x: -1, y: -1, z: 0, block: "minecraft:amethyst_block" },
        { x: 0, y: -1, z: 0, block: "minecraft:amethyst_block" },
        { x: 0, y: 0, z: 0, block: "minecraft:amethyst_block" },
        { x: 1, y: 0, z: 0, block: "minecraft:amethyst_block" },
    ],
    "bunnystudios_rpg:twin_wolf",
    "spawn_purple",
    { x: 0, y: 0, z: 0 }
);

const frostTwinWolf = new Totem(
    [
        { x: -1, y: -1, z: 0, block: "minecraft:snow" },
        { x: 0, y: -1, z: 0, block: "minecraft:snow" },
        { x: 0, y: 0, z: 0, block: "minecraft:snow" },
        { x: 1, y: 0, z: 0, block: "minecraft:snow" },
    ],
    "bunnystudios_rpg:twin_wolf",
    "spawn_ice",
    { x: 0, y: 0, z: 0 }
);


world.afterEvents.itemUse.subscribe((event) => {
    const player = event.source;
    const item = event.itemStack;

    if (item.typeId !== "bunnystudios_rpg:kridident") return;
    const block = player.getBlockFromViewDirection();
    if (!block) {
        return;
    }

    const dimension = player.dimension;
    const location = block.block.location;

    const possibleCenters = [
        { x: -1, y: -1, z: -1 },
        { x: -1, y: -1, z: 0 },
        { x: -1, y: -1, z: 1 },
        { x: -1, y: 0, z: -1 },
        { x: -1, y: 0, z: 0 },
        { x: -1, y: 0, z: 1 },
        { x: -1, y: 1, z: -1 },
        { x: -1, y: 1, z: 0 },
        { x: -1, y: 1, z: 1 },
        { x: 0, y: -1, z: -1 },
        { x: 0, y: -1, z: 0 },
        { x: 0, y: -1, z: 1 },
        { x: 0, y: 0, z: -1 },
        { x: 0, y: 0, z: 0 },
        { x: 0, y: 0, z: 1 },
        { x: 0, y: 1, z: -1 },
        { x: 0, y: 1, z: 0 },
        { x: 0, y: 1, z: 1 },
        { x: 1, y: -1, z: -1 },
        { x: 1, y: -1, z: 0 },
        { x: 1, y: -1, z: 1 },
        { x: 1, y: 0, z: -1 },
        { x: 1, y: 0, z: 0 },
        { x: 1, y: 0, z: 1 },
        { x: 1, y: 1, z: -1 },
        { x: 1, y: 1, z: 0 },
        { x: 1, y: 1, z: 1 }
    ];

    for (const offset of possibleCenters) {
        const centerLoc = {
            x: location.x + offset.x,
            y: location.y + offset.y,
            z: location.z + offset.z
        };

        // Tüm totem varyantlarını kontrol et
        const totems = [bouldrax, electroCube, fireCube, frostCube, electroBloom, frostBloom, fireBloom, twinWolf, frostTwinWolf];

        for (const totem of totems) {
            // X ekseni kontrolü
            if (totem.checkPattern(centerLoc, dimension)) {
                totem.removeStructure(centerLoc, dimension, totem.pattern);
                system.runTimeout(() => {
                    totem.spawn(centerLoc, dimension);
                    player.runCommandAsync("clear @s[m=!c] bunnystudios_rpg:kridident 0 1");
                }, 1);
                return;
            }

            // Z ekseni kontrolü
            if (totem.checkPattern(centerLoc, dimension, "z")) {
                totem.removeStructure(centerLoc, dimension, totem.pattern, "z");
                system.runTimeout(() => {
                    totem.spawn(centerLoc, dimension);
                    player.runCommandAsync("clear @s[m=!c] bunnystudios_rpg:kridident 0 1");
                }, 1);
                return;
            }

            // X ekseni ayna kontrolü
            if (totem.checkPattern(centerLoc, dimension, "x_mirror")) {
                totem.removeStructure(centerLoc, dimension, totem.pattern, "x_mirror");
                system.runTimeout(() => {
                    totem.spawn(centerLoc, dimension);
                    player.runCommandAsync("clear @s[m=!c] bunnystudios_rpg:kridident 0 1");
                }, 1);
                return;
            }

            // Z ekseni ayna kontrolü 
            if (totem.checkPattern(centerLoc, dimension, "z_mirror")) {
                totem.removeStructure(centerLoc, dimension, totem.pattern, "z_mirror");
                system.runTimeout(() => {
                    totem.spawn(centerLoc, dimension);
                    player.runCommandAsync("clear @s[m=!c] bunnystudios_rpg:kridident 0 1");
                }, 1);
                return;
            }
        }
    }
});