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

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

            if (!item?.hasTag('minecraft:is_axe')) return;

            let prevStage = ""

            if (block.hasTag("waxed")) {
                block.setPermutation(block.permutation.withState("rustic:oxidation",false));
                waxOff(blockCenter,dimension)
                return;
            }
            else if (block.hasTag("copper")) {
                return;
            }
            else if (block.hasTag("copperExposed")) {
                prevStage = "rustic:chandelier_copper"
            }
            else if (block.hasTag("copperWeathered")) {
                prevStage = "rustic:chandelier_copper_exposed"
            }
            else if (block.hasTag("copperOxidized")) {
                prevStage = "rustic:chandelier_copper_weathered"
            }


            const oxidation = BlockPermutation.resolve(prevStage);
                
            block.setPermutation(oxidation);

            waxOff(blockCenter,dimension)

            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:oxidation_chandelier", {
        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;
            }

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

            block.setPermutation(block.permutation.withState("rustic:oxidation",true));
            waxOn(blockCenter,dimension)

            const equipment = player.getComponent('equippable');

            if (player.getGameMode() !== "creative") {
                if (item.amount > 1) {
                    item.amount -= 1;
                    equipment.setEquipment('Mainhand', item);
                } else if (item.amount === 1) {
                    equipment.setEquipment('Mainhand', undefined); // Clear the slot if only 1 item left
                }
            }
        },
        onTick: e => {
            const { block, dimension } = e;

            let nextStage = ""

            if (block.hasTag("copperOxidized")) {
                return;
            }
            else if (block.hasTag("copper")) {
                nextStage = "rustic:chandelier_copper_exposed"
            }
            else if (block.hasTag("copperExposed")) {
                nextStage = "rustic:chandelier_copper_weathered"
            }
            else if (block.hasTag("copperWeathered")) {
                nextStage = "rustic:chandelier_copper_oxidized"
            }


            const oxidation = BlockPermutation.resolve(nextStage);
                
            block.setPermutation(oxidation);
        },
    });
});

export function waxOn(location, dimension){
    const blockCenter = location;
    dimension.runCommand(`particle rustic:wax_particle_on ${blockCenter.x-0.2} ${blockCenter.y} ${blockCenter.z}`)
    dimension.runCommand(`particle rustic:wax_particle_on ${blockCenter.x} ${blockCenter.y+0.3} ${blockCenter.z-0.3}`)
    dimension.runCommand(`particle rustic:wax_particle_on ${blockCenter.x-0.1} ${blockCenter.y-0.5} ${blockCenter.z-0.2}`)
    dimension.runCommand(`particle rustic:wax_particle_on ${blockCenter.x-0.4} ${blockCenter.y+0.2} ${blockCenter.z-0.1}`)
    dimension.runCommand(`particle rustic:wax_particle_on ${blockCenter.x+0.2} ${blockCenter.y} ${blockCenter.z}`)
    dimension.runCommand(`particle rustic:wax_particle_on ${blockCenter.x} ${blockCenter.y+0.3} ${blockCenter.z+0.3}`)
    dimension.runCommand(`playsound copper.wax.on @a ${blockCenter.x} ${blockCenter.y+0.3} ${blockCenter.z+0.3} 10`)
    return;
}

export function waxOff(location, dimension){
    const blockCenter = location;
    dimension.runCommand(`particle rustic:wax_particle_off ${blockCenter.x-0.2} ${blockCenter.y} ${blockCenter.z}`)
    dimension.runCommand(`particle rustic:wax_particle_off ${blockCenter.x} ${blockCenter.y+0.3} ${blockCenter.z-0.3}`)
    dimension.runCommand(`particle rustic:wax_particle_off ${blockCenter.x-0.1} ${blockCenter.y-0.5} ${blockCenter.z-0.2}`)
    dimension.runCommand(`particle rustic:wax_particle_off ${blockCenter.x-0.4} ${blockCenter.y+0.2} ${blockCenter.z-0.1}`)
    dimension.runCommand(`particle rustic:wax_particle_off ${blockCenter.x+0.2} ${blockCenter.y} ${blockCenter.z}`)
    dimension.runCommand(`particle rustic:wax_particle_off ${blockCenter.x} ${blockCenter.y+0.3} ${blockCenter.z+0.3}`)
    dimension.runCommand(`playsound copper.wax.off @a ${blockCenter.x} ${blockCenter.y+0.3} ${blockCenter.z+0.3} 10`)
    return;
}