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

const hoeIds = [
    'gs_cb:resin_hoe',
    'gs_cb:wooden_hoe',
    'gs_cb:amethyst_hoe',
    'gs_cb:fungal_hoe',
    'gs_cb:ice_hoe',
    'gs_cb:magma_hoe',
    'gs_cb:netherite_magma_hoe',
    'gs_cb:diamond_magma_hoe',
    'gs_cb:golden_magma_hoe',
    'gs_cb:iron_magma_hoe',
    'gs_cb:netherite_ice_hoe',
    'gs_cb:diamond_ice_hoe',
    'gs_cb:golden_ice_hoe',
    'gs_cb:iron_ice_hoe'
];

const shovelIds = [
    'gs_cb:resin_shovel',
    'gs_cb:wooden_shovel',
    'gs_cb:amethyst_shovel',
    'gs_cb:fungal_shovel',
    'gs_cb:ice_shovel',
    'gs_cb:magma_shovel',
    'gs_cb:netherite_magma_shovel',
    'gs_cb:diamond_magma_shovel',
    'gs_cb:golden_magma_shovel',
    'gs_cb:iron_magma_shovel',
    'gs_cb:netherite_ice_shovel',
    'gs_cb:diamond_ice_shovel',
    'gs_cb:golden_ice_shovel',
    'gs_cb:iron_ice_shovel'
];

const axeIds = [
    'gs_cb:resin_axe',
    'gs_cb:wooden_axe',
    'gs_cb:amethyst_axe',
    'gs_cb:fungal_axe',
    'gs_cb:ice_axe',
    'gs_cb:magma_axe',
    'gs_cb:netherite_magma_axe',
    'gs_cb:diamond_magma_axe',
    'gs_cb:golden_magma_axe',
    'gs_cb:iron_magma_axe',
    'gs_cb:netherite_ice_axe',
    'gs_cb:diamond_ice_axe',
    'gs_cb:golden_ice_axe',
    'gs_cb:iron_ice_axe'
];

world.beforeEvents.itemUseOn.subscribe(evd => {
    const { source: player, block, itemStack: itemUsed } = evd;

    if (!itemUsed) return;

    if (hoeIds.includes(itemUsed.typeId) || shovelIds.includes(itemUsed.typeId) || axeIds.includes(itemUsed.typeId)) {

        const playerEquippableComp = player.getComponent("equippable");

        if (!playerEquippableComp) return;

        if (hoeIds.includes(itemUsed.typeId)) {
            const blockAbove = block.dimension.getBlock(block.location).above(1);
            if (!blockAbove.isAir) return;
            if (block.permutation.matches('minecraft:dirt') || block.permutation.matches('minecraft:grass_block') || block.permutation.matches('minecraft:dirt_with_roots') || block.permutation.matches('minecraft:grass_path')) {
                system.run(function () {
                    player.playSound('use.gravel', { pitch: 1, location: block.location, volume: 1 });
                })
            }
            else return;
        }

        if (shovelIds.includes(itemUsed.typeId)) {
            const blockAbove = block.dimension.getBlock(block.location).above(1);
            if (!blockAbove.isAir) return;
            if (block.permutation.matches('minecraft:dirt') || block.permutation.matches('minecraft:grass_block') || block.permutation.matches('minecraft:dirt_with_roots') || block.permutation.matches('minecraft:mycelium') || block.permutation.matches('minecraft:podzol')) {
                if (!block.permutation.matches('minecraft:grass_block')) {
                    system.run(function () {
                        block.setType("minecraft:grass_path");
                    })
                }
                system.run(function () {
                    player.playSound('use.grass', { pitch: 1, location: block.location, volume: 0.5 });
                })
            }
            else return;
        }

        if (axeIds.includes(itemUsed.typeId)) {
            if (block.permutation.matches('minecraft:oak_log') || block.permutation.matches('minecraft:birch_log') || block.permutation.matches('minecraft:spruce_log') || block.permutation.matches('minecraft:dark_oak_log') || block.permutation.matches('minecraft:jungle_log') || block.permutation.matches('minecraft:acacia_log') || block.permutation.matches('minecraft:mangrove_log') || block.permutation.matches('minecraft:oak_wood') || block.permutation.matches('minecraft:birch_wood') || block.permutation.matches('minecraft:spruce_wood') || block.permutation.matches('minecraft:dark_oak_wood') || block.permutation.matches('minecraft:jungle_wood') || block.permutation.matches('minecraft:acacia_wood') || block.permutation.matches('minecraft:mangrove_wood'))
                system.run(function () {
                    player.playSound('use.wood', { pitch: 1, location: block.location, volume: 1 })
                })
            else if (block.permutation.matches('minecraft:cherry_log') || block.permutation.matches('minecraft:cherry_wood'))
                system.run(function () {
                    player.playSound('step.cherry_wood', { pitch: 1, location: block.location, volume: 1 })
                })
            else if (block.permutation.matches('minecraft:bamboo_block'))
                system.run(function () {
                    player.playSound('step.bamboo_wood', { pitch: 1, location: block.location, volume: 1 })
                })
            else if (block.permutation.matches('minecraft:crimson_stem') || block.permutation.matches('minecraft:warped_stem') || block.permutation.matches('minecraft:crimson_hyphae') || block.permutation.matches('minecraft:warped_hyphae'))
                system.run(function () {
                    player.playSound('use.stem', { pitch: 1, location: block.location, volume: 1 })
                })
            else return;
        }

        if (player.matches({ gameMode: GameMode.creative })) 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;

        system.run(function () {
            itemUsedDurabilityComp.damage += 1;

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

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