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

system.beforeEvents.startup.subscribe((initEvent) => {
    initEvent.blockComponentRegistry.registerCustomComponent("rustic:lit", {
        onPlayerInteract: e => {
            const { player, block, dimension } = e;
            const blockCenter = block.center();
            const item = player.getComponent('inventory').container.getItem(player.selectedSlotIndex);

            if (!item) {
                return;
            }

            if (item.typeId !== "minecraft:flint_and_steel") {
                return;
            }

            block.setPermutation(block.permutation.withState("r:lit",true));

            dimension.runCommand(`playsound fire.ignite @a ${blockCenter.x} ${blockCenter.y} ${blockCenter.z}`)

            candleFlame(blockCenter,dimension, block)

            const equipment = player.getComponent('equippable');
            // Get the durability component of the selected item
            const durability = item.getComponent('durability');

            // Check if the item has a durability component and if its damage is less than the maximum durability
            if (durability && durability.damage < durability.maxDurability) {
                // Increment the damage of the item, reducing its durability
                durability.damage++;
                
                // Update the equipment in the player's main hand with the modified item
                equipment.setEquipment('Mainhand', item);
            }

            // Check if the item has a durability component and if its damage is greater than or equal to the maximum durability
            if (durability && durability.damage >= durability.maxDurability) {
                // Play the sound effect for breaking an item
                player.playSound('random.break');
                
                // Replace the item in the player's main hand with an air block (i.e., remove the item)
                equipment.setEquipment('Mainhand', new ItemStack('minecraft:air', 1));
            }
            
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent("rustic:unlit", {
        onPlayerInteract: e => {
            const { player, block, dimension, face, faceLocation } = e;
            const blockCenter = block.center();
            const item = player.getComponent('inventory').container.getItem(player.selectedSlotIndex);

            if (item) {
                return;
            }

            block.setPermutation(block.permutation.withState("r:lit",false));
            dimension.runCommand(`playsound extinguish.candle @a ${blockCenter.x} ${blockCenter.y} ${blockCenter.z}`)
        },
        onTick: e => {
            const { block, dimension } = e;
            const blockCenter = block.center();
            candleFlame(blockCenter,dimension, block)
            
        },
    });
});

export function candleFlame(location, dimension, block){
    const blockCenter = location;
    if (block.hasTag('candle_down')) {
        dimension.runCommand(`particle minecraft:candle_flame_particle ${blockCenter.x} ${blockCenter.y + 0.2} ${blockCenter.z}`)
        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x} ${blockCenter.y + 0.2} ${blockCenter.z}`)
    }
    else if (block.hasTag('candle_north')) {
        dimension.runCommand(`particle minecraft:candle_flame_particle ${blockCenter.x} ${blockCenter.y + 0.25} ${blockCenter.z + 0.26}`)
        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x} ${blockCenter.y + 0.25} ${blockCenter.z + 0.26}`)
    }
    else if (block.hasTag('candle_south')) {
        dimension.runCommand(`particle minecraft:candle_flame_particle ${blockCenter.x} ${blockCenter.y + 0.25} ${blockCenter.z - 0.26}`)
        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x} ${blockCenter.y + 0.25} ${blockCenter.z - 0.26}`)
    }
    else if (block.hasTag('candle_west')) {
        dimension.runCommand(`particle minecraft:candle_flame_particle ${blockCenter.x + 0.25} ${blockCenter.y + 0.25} ${blockCenter.z}`)
        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x + 0.25} ${blockCenter.y + 0.25} ${blockCenter.z}`)
    }
    else if (block.hasTag('candle_east')) {
        dimension.runCommand(`particle minecraft:candle_flame_particle ${blockCenter.x - 0.25} ${blockCenter.y + 0.25} ${blockCenter.z}`)
        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x - 0.25} ${blockCenter.y + 0.25} ${blockCenter.z}`)
    }
    return;
}