import { World, world } from "@minecraft/server"

const tools = [
    {
        id: "better_on_bedrock:iron_pickaxe"
    },
    {
        id: "better_on_bedrock:iron_axe"
    },
    {
        id: "better_on_bedrock:diamond_axe"
    },
    {
        id: "better_on_bedrock:diamond_pickaxe"
    },
    {
        id: "better_on_bedrock:netherite_axe"
    },
    {
        id: "better_on_bedrock:netherite_pickaxe"
    },
    {
        id: "better_on_bedrock:stone_axe"
    },
    {
        id: "better_on_bedrock:stone_pickaxe"
    },

    {
        id: "better_on_bedrock:stardust_pickaxe"
    },
    {
        id: "better_on_bedrock:stardust_axe"
    },
    {
        id: "better_on_bedrock:stardust_shovel"
    },

    {
        id: "better_on_bedrock:copper_axe"
    },
    {
        id: "better_on_bedrock:copper_pickaxe"
    },
    {
        id: "better_on_bedrock:stardust_sword"
    },
]

/*world.afterEvents.playerBreakBlock.subscribe(ev => {
    const foundTool = tools.find(tool => ev.itemStackAfterBreak.typeId === tool.id);

    console.warn("Event itemStackAfterBreak:", ev.itemStackAfterBreak.typeId);
    console.warn("Found Tool:", JSON.stringify(foundTool));

    if(foundTool) {
        const player = ev.player
        const inventory = player.getComponent("minecraft:inventory").container;
        const item = inventory.getItem(player.selectedSlot);
        const damage = item.getComponent("durability").damage;
        const durability = item.getComponent("durability").maxDurability;

        if(damage <= durability) {
        item.getComponent('durability').damage = item.getComponent('durability').damage + 1;
        console.warn("Item Component:", JSON.stringify(item));
        console.warn("Durability Damage Before:", item.getComponent('durability').damage);
        console.warn("Durability Damage After:", item.getComponent('durability').damage);

        inventory.setItem(player.selectedSlot, item);
    }
    if(damage == durability) {

    }
}
})*/
world.afterEvents.playerBreakBlock.subscribe((data) => {
    const {player, itemStackAfterBreak: item} = data
    const inv = player?.getComponent('inventory').container
    const durability = item?.getComponent('durability')
    const enchant = item?.getComponent("minecraft:enchantable").getEnchantments().map((e) => e.type);
    const unbreaking = enchant?.includes( "unbreaking" )
    const level = unbreaking?.level
    const randomMath = Math.round(Math.random() * 1)
    if(item && durability) {
    if((durability.maxDurability - durability.damage) == 0) {
        
        inv.setItem(player.selectedSlot);
            
        }
        else if(item.getLore().includes("§r§7Tree Capitator I") || item.getLore().includes("§r§7Vein Miner I"))  {
            console.warn("tws")
        } else if(item && !unbreaking) {
            console.warn("tws")
            durability.damage += 1;
            inv.setItem(player.selectedSlot, item);
        }
        if(item && level == 1){
            console.warn(Math.round(Math.random(1/(1+1)) * 1))
            durability.damage += Math.round(Math.random(1/(1+1)) * 1);
            inv.setItem(player.selectedSlot, item);
        }
        if(item && level == 2) {
            console.warn(Math.round(Math.random(1/(2+1)) * 1))
            durability.damage += Math.round(Math.random(1/(2+1)) * 1);
            inv.setItem(player.selectedSlot, item);
        }
        if(item && level == 3) {
            console.warn(Math.round(Math.random(1/(3+1)) * 1))
            durability.damage += Math.round(Math.random(1/(4+1)) * 1);
            inv.setItem(player.selectedSlot, item);
        }
    }
})

world.afterEvents.entityHitEntity.subscribe(ev => {
    if(ev.damagingEntity.typeId == 'minecraft:player'){
        const player = ev.damagingEntity
        const inventory = player.getComponent("minecraft:inventory").container;
        const item = inventory.getItem(player.selectedSlot);
        
        console.warn("Item Component:", JSON.stringify(item));
        console.warn("Durability Damage Before:", item.getComponent('durability').damage);

        item.getComponent('durability').damage = item.getComponent('durability').damage + 1;

        console.warn("Durability Damage After:", item.getComponent('durability').damage);

        inventory.setItem(player.selectedSlot, item);
    }
})
