import { Block, BlockPermutation, ItemStack, Player } from "@minecraft/server";
import { addDamageItem } from "../../library/itemManager";

/**
 * Evento del componente plasmaLighterComponent
 * @param {{blockFace: string, source: Player, usedOnBlockPermutation: BlockPermutation, itemStack: ItemStack, 
* faceLocation: {x: number, y: number, z: number}, block: Block}} event 
*/
export const plasmaLighterEvent = ({blockFace, source, usedOnBlockPermutation, itemStack, faceLocation, block}) => {
    const currentDamage = itemStack.getComponent('minecraft:durability').damage;
    const maxDurability = itemStack.getComponent('minecraft:durability').maxDurability;
    const durability = maxDurability - currentDamage;

    if(durability > 1) {
        addDamageItem(itemStack, source, 1);
        if(Math.random() < 0.5) {
            source.runCommand('playsound plasma.lighter @a [r=5] ~ ~ ~ 0.7');
        } else {
            source.runCommand('playsound plasma.lighter @a [r=5] ~ ~ ~ 0.3');
        }
        switch(blockFace) {
            case 'Down': 
                //fire control event down
                source.runCommand(`fill ${block.x} ${block.y-1} ${block.z} ${block.x} ${block.y-1} ${block.z} fire replace air`);
                break;
            case 'Up': 
                //fire control event up
                source.runCommand(`fill ${block.x} ${block.y+1} ${block.z} ${block.x} ${block.y+1} ${block.z} fire replace air`);
                break;
            case 'North': 
                //fire control event north
                source.runCommand(`fill ${block.x} ${block.y} ${block.z-1} ${block.x} ${block.y} ${block.z-1} fire replace air`);
                break;
            case 'South': 
                //fire control event south
                source.runCommand(`fill ${block.x} ${block.y} ${block.z+1} ${block.x} ${block.y} ${block.z+1} fire replace air`);
                break;
            case 'West': 
                //fire control event west
                source.runCommand(`fill ${block.x-1} ${block.y} ${block.z} ${block.x-1} ${block.y} ${block.z} fire replace air`);
                break;
            case 'East': 
                //fire control event east
                source.runCommand(`fill ${block.x+1} ${block.y} ${block.z} ${block.x+1} ${block.y} ${block.z} fire replace air`);
                break;
        }
    } else {
        source.runCommandAsync(`replaceitem entity @s slot.weapon.mainhand 0 nps_ec:plasma_lighter_unloaded 1 129`)
    }
}