import { Block, Dimension } from "@minecraft/server";
import { checkAnyNeighborHasTag, setBlockToAir } from "../../library/blockManager";

/**
 * Evento del componente copperMobSpawnerComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const copperMobSpawnerEvent = ({ block, dimension }) => {
    let permutation = block.permutation;
    const state = permutation.getState('nps_ec:state');
    const destroy = permutation.getState('nps_ec:destroy');
    const variant = permutation.getState('nps_ec:variants');

    const powerOnConditions = checkAnyNeighborHasTag(block, 'power:on');

    if (state <= 11 && powerOnConditions && variant !== 'none') {
        spawnMobs(variant, block, dimension);
        if (state <= 14) {
            if (Math.random() < 0.50) {
                permutation = permutation.withState('nps_ec:state', state + 1);
            }
        } else if (state >= 15 && destroy <= 9) {
            if (Math.random() < 0.50) {
                permutation = permutation.withState('nps_ec:destroy', destroy + 1);
            }
        } else if (destroy >= 10) {
            setBlockToAir(block, dimension);
        }
    } else if (state <= 14 && variant !== 'none') {
        if (Math.random() < 0.50) {
            permutation = permutation.withState('nps_ec:state', state + 1);
        }
    } else if (state >= 15 && destroy <= 9) {
        if (Math.random() < 0.50) {
            permutation = permutation.withState('nps_ec:destroy', destroy + 1);
        }
    } else if (destroy >= 10) {
        setBlockToAir(block, dimension);
    }
    if(block.typeId === 'minecraft:air') return

    block.setPermutation(permutation)
}

/**
 * @param {string} variant 
 * @param {Block} block
 * @param {Dimension} dimension 
 */
const spawnMobs = (variant, block, dimension) => {
    if(variant === 'none') return
    const spawnVariants = [
        { weight: 66, commands: [`summon minecraft:${variant} ${block.x} ${block.y+1} ${block.z+1}`, `summon nps_ec:push_mobs ${block.x} ${block.y+1} ${block.z-1}`, `particle minecraft:ice_evaporation_emitter ${block.x} ${block.y+1} ${block.z}`] },
        { weight: 11, commands: [`summon minecraft:${variant} ${block.x} ${block.y+1} ${block.z+0.2}`, `summon nps_ec:push_mobs ${block.x} ${block.y+1} ${block.z+1}`, `particle minecraft:ice_evaporation_emitter ${block.x} ${block.y+1} ${block.z+0.2}`, `summon minecraft:${variant} ${block.x+1} ${block.y} ${block.z}`, `particle minecraft:ice_evaporation_emitter ${block.x+1} ${block.y} ${block.z}`, `summon minecraft:${variant} ${block.x} ${block.y} ${block.z+1}`, `particle minecraft:ice_evaporation_emitter ${block.x} ${block.y} ${block.z+1}`] },
        { weight: 11, commands: [`summon minecraft:${variant} ${block.x+0.2} ${block.y+1} ${block.z+1}`, `summon nps_ec:push_mobs ${block.x+0.2} ${block.y+1} ${block.z-1}`, `particle minecraft:ice_evaporation_emitter ${block.x+0.2} ${block.y+1} ${block.z}`, `summon minecraft:${variant} ${block.x-1} ${block.y} ${block.z}`, `particle minecraft:ice_evaporation_emitter ${block.x-1} ${block.y} ${block.z}`, `summon minecraft:${variant} ${block.x} ${block.y} ${block.z-1}`, `particle minecraft:ice_evaporation_emitter ${block.x} ${block.y} ${block.z-1}`] },
        { weight: 11, commands: [`summon minecraft:${variant} ${block.x+0.2} ${block.y+1} ${block.z+0.2}`, `summon nps_ec:push_mobs ${block.x+0.2} ${block.y+1} ${block.z+1}`, `particle minecraft:ice_evaporation_emitter ${block.x+0.2} ${block.y+1} ${block.z+0.2}`, `summon minecraft:${variant} ${block.x-1} ${block.y} ${block.z+1}`, `particle minecraft:ice_evaporation_emitter ${block.x-1} ${block.y} ${block.z+1}`, `summon minecraft:${variant} ${block.x+1} ${block.y} ${block.z-1}`, `particle minecraft:ice_evaporation_emitter ${block.x+1} ${block.y} ${block.z-1}`] }
    ];

    const totalWeight = spawnVariants.reduce((acc, item) => acc + item.weight, 0);
    let random = Math.random() * totalWeight;
    for (const item of spawnVariants) {
        if (random < item.weight) {
            
            item.commands.forEach(command => 
                dimension.runCommand(command)
            );
            break;
        }
        random -= item.weight;
    }
};