import { EntityDamageCause, MolangVariableMap, world } from "@minecraft/server";
import { SuppStates } from "../reg/suppStates";
import { getItemInHand, isLivingEntity } from "../util/miscUtils";
import { McItems } from "../reg/mcItems";
import { McParticles } from "../reg/mcParticles";
import { intToRGB } from "../util/mthUtils";
export const BambooSpikeComponent = {
    onTick(event) {
        let block = event.block;
        if (!block.isValid())
            return;
        const entities = event.dimension.getEntitiesAtBlockLocation(block.location).filter(entity => {
            return isLivingEntity(entity);
        });
        if (entities.length == 0)
            return;
        //const dir = getADirection(block);
        const effect = getEffect(block);
        if (effect) {
            const type = block.permutation.getState(SuppStates.PotionUpgrade);
            const durationAmpl = type == "long" ? 2 : 1;
            const duration = durationAmpl * 60;
            const potency = type == "strong" ? 1 : 0;
            //TODO: tweak instant damage... otherwise its op (actually wont even be applied due to how dmg woks)
            const opt = {
                showParticles: true,
                amplifier: potency,
            };
            for (const entity of entities) {
                entity.addEffect(effect, duration, opt);
            }
        }
        const opt = {
            cause: EntityDamageCause.contact
        };
        //damage
        for (const entity of entities) {
            entity.applyDamage(2, opt);
        }
        if (Math.random() < 0.3) {
            addParticles(block);
        }
    },
    onRandomTick(event) {
        addParticles(event.block);
    },
    onPlayerInteract(event) {
        const player = event.player;
        const item = getItemInHand(player);
        if (item && item.typeId == McItems.LingeringPotion) {
            //TODO: once they add potion components
            //TODO: sound
        }
    }
};
const colorMap = new Map([
    ["razz_sup:bamboo_spikes_decay" /* SuppBlocks.SpikesDecay */, intToRGB(7561558)],
    ["razz_sup:bamboo_spikes_harming" /* SuppBlocks.SpikesHarming */, intToRGB(11101546)],
    ["razz_sup:bamboo_spikes_infestation" /* SuppBlocks.SpikesInfestation */, intToRGB(9214860)],
    ["razz_sup:bamboo_spikes_oozing" /* SuppBlocks.SpikesOozing */, intToRGB(10092451)],
    ["razz_sup:bamboo_spikes_poison" /* SuppBlocks.SpikesPoison */, intToRGB(8889187)],
    ["razz_sup:bamboo_spikes_slowness" /* SuppBlocks.SpikesSlowness */, intToRGB(9154528)],
    ["razz_sup:bamboo_spikes_weakness" /* SuppBlocks.SpikesWeakness */, intToRGB(4738376)],
    ["razz_sup:bamboo_spikes_wind_charging" /* SuppBlocks.SpikesWindCharging */, intToRGB(12438015)],
    ["razz_sup:bamboo_spikes_weaving" /* SuppBlocks.SpikesWeaving */, intToRGB(7891290)],
]);
function addParticles(block) {
    const map = new MolangVariableMap();
    const color = colorMap.get(block.typeId);
    if (color) {
        map.setColorRGBA("variable.color", color);
        const loc = { x: block.x + Math.random(), y: block.y + Math.random(), z: block.z + Math.random() };
        block.dimension.spawnParticle(McParticles.MobSpell, loc, map);
    }
}
//this could have been a block permutation...
function getEffect(block) {
    const id = block.typeId;
    switch (id) {
        case "razz_sup:bamboo_spikes":
            return undefined;
        case "razz_sup:bamboo_spikes_slowness":
            return "slowness";
        case "razz_sup:bamboo_spikes_harming":
            return "instant_damage";
        case "razz_sup:bamboo_spikes_poison":
            return "poison";
        case "razz_sup:bamboo_spikes_weakness":
            return "weakness";
        case "razz_sup:bamboo_spikes_decay":
            return "wither";
        case "razz_sup:bamboo_spikes_wind_charging":
            return "wind_charged";
        case "razz_sup:bamboo_spikes_weaving":
            return "weaving";
        case "razz_sup:bamboo_spikes_oozing":
            return "oozing";
        case "razz_sup:bamboo_spikes_infestation":
            return "infested";
    }
}
world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    if (!event.isFirstEvent)
        return;
    const item = event.itemStack;
    if (item && item.typeId == McItems.SplashPotion) {
        event.cancel = true;
    }
});
