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

const toolTypeIds = [
    'gs_cb:amethyst_axe',
    'gs_cb:amethyst_pickaxe',
    'gs_cb:amethyst_shovel',
    'gs_cb:amethyst_hoe',
    'gs_cb:fungal_axe',
    'gs_cb:fungal_pickaxe',
    'gs_cb:fungal_shovel',
    'gs_cb:fungal_hoe',
    'gs_cb:ice_axe',
    'gs_cb:ice_pickaxe',
    'gs_cb:ice_shovel',
    'gs_cb:ice_hoe',
    'gs_cb:magma_axe',
    'gs_cb:magma_pickaxe',
    'gs_cb:magma_shovel',
    'gs_cb:magma_hoe',
    'gs_cb:netherite_magma_axe',
    'gs_cb:netherite_magma_pickaxe',
    'gs_cb:netherite_magma_shovel',
    'gs_cb:netherite_magma_hoe',
    'gs_cb:diamond_magma_axe',
    'gs_cb:diamond_magma_pickaxe',
    'gs_cb:diamond_magma_shovel',
    'gs_cb:diamond_magma_hoe',
    'gs_cb:golden_magma_axe',
    'gs_cb:golden_magma_pickaxe',
    'gs_cb:golden_magma_shovel',
    'gs_cb:golden_magma_hoe',
    'gs_cb:iron_magma_axe',
    'gs_cb:iron_magma_pickaxe',
    'gs_cb:iron_magma_shovel',
    'gs_cb:iron_magma_hoe',
    'gs_cb:netherite_ice_axe',
    'gs_cb:netherite_ice_pickaxe',
    'gs_cb:netherite_ice_shovel',
    'gs_cb:netherite_ice_hoe',
    'gs_cb:diamond_ice_axe',
    'gs_cb:diamond_ice_pickaxe',
    'gs_cb:diamond_ice_shovel',
    'gs_cb:diamond_ice_hoe',
    'gs_cb:golden_ice_axe',
    'gs_cb:golden_ice_pickaxe',
    'gs_cb:golden_ice_shovel',
    'gs_cb:golden_ice_hoe',
    'gs_cb:iron_ice_axe',
    'gs_cb:iron_ice_pickaxe',
    'gs_cb:iron_ice_shovel',
    'gs_cb:iron_ice_hoe',
    'gs_cb:diamond_chisel',
    'gs_cb:netherite_chisel',
    'gs_cb:magma_staff'
];

const weaponTypeIds = [
    'gs_cb:amethyst_sword',
    'gs_cb:fungal_sword',
    'gs_cb:ice_sword',
    'gs_cb:magma_sword',
    'gs_cb:netherite_magma_sword',
    'gs_cb:diamond_magma_sword',
    'gs_cb:golden_magma_sword',
    'gs_cb:iron_magma_sword',
    'gs_cb:netherite_ice_sword',
    'gs_cb:diamond_ice_sword',
    'gs_cb:golden_ice_sword',
    'gs_cb:iron_ice_sword'
];

world.afterEvents.playerBreakBlock.subscribe(evd => {
    const { player, itemStackBeforeBreak: itemUsed } = evd;

    if (!itemUsed || player.matches({ gameMode: GameMode.creative })) return;

    if (toolTypeIds.includes(itemUsed.typeId) || weaponTypeIds.includes(itemUsed.typeId)) {
        const playerEquippableComp = player.getComponent("equippable");

        if (!playerEquippableComp) return;

        const itemEnchantmentComp = itemUsed.getComponent("minecraft:enchantable");
        const unbreakingLevel = itemEnchantmentComp?.getEnchantment("unbreaking")?.level ?? 0;

        const breakChance = 100 / (unbreakingLevel + 1);
        const randomizeChance = Math.random() * 100;

        if (breakChance < randomizeChance) return;

        const itemUsedDurabilityComp = itemUsed.getComponent("durability");

        if (!itemUsedDurabilityComp) return;

        let durabilityModifier = 0;
        if (toolTypeIds.includes(itemUsed.typeId)) {
            durabilityModifier = 1;
        } else {
            durabilityModifier = 2;
        }

        itemUsedDurabilityComp.damage += durabilityModifier;

        const maxDurability = itemUsedDurabilityComp.maxDurability
        const currentDamage = itemUsedDurabilityComp.damage
        if (currentDamage >= maxDurability) {

            player.playSound('random.break', { pitch: 1, location: player.location, volume: 1 })
            playerEquippableComp.setEquipment("Mainhand", new ItemStack('minecraft:air', 1));
        }
        else if (currentDamage < maxDurability) {

            playerEquippableComp.setEquipment("Mainhand", itemUsed);
        }
    }
})