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

system.beforeEvents.startup.subscribe((initEvent) => {
    initEvent.blockComponentRegistry.registerCustomComponent("rustic:oxidation_chain", {
        onTick: e => {
            const { block, dimension } = e;
            let nextStage = ""

            if (block.hasTag("copperOxidized")) {
                return;
            }
            else if (block.hasTag("copper")) {
                nextStage = "rustic:chain_copper_exposed"
            }
            else if (block.hasTag("copperExposed")) {
                nextStage = "rustic:chain_copper_weathered"
            }
            else if (block.hasTag("copperWeathered")) {
                nextStage = "rustic:chain_copper_oxidized"
            }

            const blockState = block.permutation.getState("minecraft:block_face");

            if (blockState) {
                const oxidation = BlockPermutation.resolve(nextStage, {"minecraft:block_face": blockState});
                
                block.setPermutation(oxidation);
            }
        },
    });
});


world.beforeEvents.playerInteractWithBlock.subscribe((eventData) => {
    const { player, block } = eventData;
    const item = eventData.itemStack;
    if (!eventData.isFirstEvent) return;
    if (!block.typeId.includes("rustic:chain") && !block.typeId.includes("rustic:chandelier")) return;
    
    if (!item?.hasTag('minecraft:is_axe')) return;
    else if (item?.typeId == "minecraft:honeycomb"){
        system.run(() => {
            block.setPermutation(block.permutation.withState("rustic:oxidation",true));
        })
        
        waxOn(blockCenter,dimension)

        const equipment = player.getComponent('equippable');
        system.run(() => {
            if (player.getGameMode() !== "creative") {
                if (item.amount > 1) {
                    item.amount -= 1;
                    equipment.setEquipment('Mainhand', item);
                } else if (item.amount === 1) {
                    equipment.setEquipment('Mainhand', undefined);
                }
            }
        })
        
    }

    const blockCenter = block.center();
    const dimension = block.dimension
    let prevStage = ""

    if (block.hasTag("waxed")) {
        system.run(() => {
            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:chain_copper"
    }
    else if (block.hasTag("copperWeathered")) {
        prevStage = "rustic:chain_copper_exposed"
    }
    else if (block.hasTag("copperOxidized")) {
        prevStage = "rustic:chain_copper_weathered"
    }

    const blockState = block.permutation.getState("minecraft:block_face");

    if (blockState) {
        const oxidation = BlockPermutation.resolve(prevStage, {"minecraft:block_face": blockState});
        system.run(() => {
            block.setPermutation(oxidation);
        })
        
    }

    waxOff(blockCenter,dimension)

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

    // Get the durability component of the selected item
    const durability = item.getComponent('durability');
    system.run(() => {
        if (durability && durability.damage < durability.maxDurability) {
            durability.damage++;
            equipment.setEquipment('Mainhand', item);
        }
        else {
            player.playSound('random.break');
            equipment.setEquipment('Mainhand', new ItemStack('minecraft:air', 1));
        }
    })
    
})

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;
}