import * as SERVER from '@minecraft/server';
import * as UI from '@minecraft/server-ui';

SERVER.system.afterEvents.scriptEventReceive.subscribe((data) => {
    if (!data.sourceBlock) return;
    if (data.id === "vc:chorus_check_players") {
        let block = data.sourceBlock;

        const playersNearby = (block.dimension.getEntities({ maxDistance: 5, location: block.location, }).length > 0);
        const alreadyActive = (block.above(1).permutation.getState("vc:upper_bit") == true)
        if (playersNearby)
            grow(block);
        else
            shrink(block);

    };
});
function grow(block) {
    let possibleHeight = getRandomInt(2, 5);
    const loc = block.location;
    for (let i = 1; i < possibleHeight; i++) {
        if (!block.above(i).isAir) break;
        else {
            block.dimension.runCommand(`setblock ${loc.x} ${loc.y + i} ${loc.z} vc:chorus_carnavorus_plant`)
            block.dimension.runCommand(`particle vc:chorus_plant_burst ${loc.x} ${loc.y + i} ${loc.z}`)
            block.dimension.runCommand(`playsound chorus.extend @a ${loc.x} ${loc.y} ${loc.z}`)
        }
    }
}
function brokengrow(block) {
    let possibleHeight = 1;
    for (let i = 1; i < 10; i++) {
        let sucess = true;
        for (let j = 0; j < i; j++) {
            if (!block.above(j).isAir) { sucess = false; break; }
        }
        if (sucess) {
            possibleHeight = i;
        }
    }
    const loc = block.location;
    block.dimension.runCommand(`fill ${loc.x} ${loc.y} ${loc.z} ${loc.x} ${loc.y + getRandomInt(1, possibleHeight)} ${loc.z} vc:chorus_carnavorus_plant`)
}
function shrink(block) {
    for (let i = 1; i < 10; i++) {
        let blockToBreak = block.above(i);
        const loc = blockToBreak.location;

        if (blockToBreak.permutation.getState("vc:upper_bit") == true && blockToBreak.typeId == 'vc:chorus_carnavorus_plant') {
            block.dimension.runCommand(`setblock ${loc.x} ${loc.y} ${loc.z} air`)
            block.dimension.runCommand(`particle vc:chorus_plant_burst ${loc.x} ${loc.y} ${loc.z}`)
            block.dimension.runCommand(`playsound chorus.shrink @a ${loc.x} ${loc.y} ${loc.z}`)
        }
        else
            break;
    }
}
SERVER.world.beforeEvents.playerBreakBlock.subscribe((data) => {
    let player = data.player;
    let block = player.getBlockFromViewDirection().block;
    SERVER.system.run(() => {
        //if (block.typeId == 'vc:chorus_carnavorus_plant') {
        shrink(block);
        //}
    })
});
function getRandomInt(min, max) {
    const minCeiled = Math.ceil(min);
    const maxFloored = Math.floor(max);
    return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // The maximum is exclusive and the minimum is inclusive
}