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

world.afterEvents.itemStartUseOn.subscribe(eventData => {
    const player = eventData.source;
    const block = eventData.block;
    const equipment = player.getComponent('equippable');
    const selectedItem = equipment.getEquipment('Mainhand');

    if ( selectedItem && seeds[selectedItem.typeId] ) {
        if ( block.typeId == "rustic:sugar_cane" && selectedItem.typeId == "minecraft:sugar_cane") {
            block.above().setType("rustic:sugar_cane");
            block.dimension.playSound("dig.grass",{x: block.x, y: block.y+1, z: block.z}, {volume: 0.8})
        }
        if ( block.typeId != "rustic:fertile_soil" ) return;
        const crop = seeds[selectedItem.typeId]
        block.dimension.playSound("dig.grass",{x: block.x, y: block.y+1, z: block.z}, {volume: 0.8})
        system.run( () => {
            block.above().setType(crop.block)
            if (player.matches({ gameMode: GameMode.creative }))return;
            removeStack(player,selectedItem)
        })
        
        return
    }

    if (!selectedItem?.hasTag('minecraft:is_hoe')) return;

    if ( block.typeId != "rustic:fertile_soil_ready" ) return;

    system.run( () => {
        
        block.setType("rustic:fertile_soil")

        block.dimension.playSound("use.gravel",{x: block.x, y: block.y, z: block.z}, {volume: 0.8})
    
    
        if (player.matches({ gameMode: GameMode.creative }))return;
    
        const durability = selectedItem.getComponent('durability');
    
        if (durability && durability.damage < durability.maxDurability) {
            durability.damage++;
                    
            equipment.setEquipment('Mainhand', selectedItem);
        }
    
        if (durability && durability.damage >= durability.maxDurability) {
            player.playSound('random.break');
                    
            equipment.setEquipment('Mainhand', new ItemStack('minecraft:air', 1));
        }
    })

});


export const seeds = {
    "minecraft:wheat_seeds": {
        block: "rustic:wheat"
    },
    "minecraft:beetroot_seeds": {
        block: "rustic:beetroot"
    },
    "minecraft:melon_seeds": {
        block: "rustic:melon_stem"
    },
    "minecraft:pumpkin_seeds": {
        block: "rustic:pumpkin"
    },
    "minecraft:torchflower_seeds": {
        block: "rustic:torchflower"
    },
    "minecraft:sugar_cane": {
        block: "rustic:sugar_cane"
    },
    "minecraft:potato": {
        block: "rustic:potatoes"
    },
    "minecraft:carrot": {
        block: "rustic:carrot"
    },
    "minecraft:pitcher_pod": {
        block: "rustic:pitcher"
    }
}

export function removeStack(player,item){
    const equipment = player.getComponent('equippable');
    if (item.amount > 1) {
        item.amount -= 1;
        equipment.setEquipment('Mainhand', item);
    } 
    else if (item.amount === 1) {
        equipment.setEquipment('Mainhand', undefined);
    }
}

system.beforeEvents.startup.subscribe((initEvent) => {
    initEvent.blockComponentRegistry.registerCustomComponent("rustic:swing_animation", {
        onPlace: (event) => {
            const { block, dimension, previousBlock } = event;
            // Your code here
        },
    });
});