import { EntityEquippableComponent, EquipmentSlot, ItemDurabilityComponent, ItemStack, Player, ItemComponentConsumeEvent, ItemComponentUseOnEvent } from "@minecraft/server";


class ItemComponent {
    constructor() { }

    /**
     * Decrements the durability of the item in hand
     * @param {Player} player 
     * @param {ItemStack} item 
     * @param {number} amount
     */
    decrementDurability(player, item, amount) {
        if (player.getGameMode() == 'creative') return;
        /** @type {ItemDurabilityComponent}} */
        let durabilityComponent = item.getComponent('durability');
        if (durabilityComponent.damage + amount >= durabilityComponent.maxDurability) {
            this.breakItem(player);
        } else {
            durabilityComponent.damage = durabilityComponent.damage + amount;
            this.setItem(player, item);
        }

    }

    breakItem(player) {
        /** @type {EntityEquippableComponent} */
        const equippableComponent = player.getComponent("equippable");
        equippableComponent.setEquipment(EquipmentSlot.Mainhand);
    }

    setItem(player, item) {
        /** @type {EntityEquippableComponent} */
        const equippableComponent = player.getComponent("equippable");
        equippableComponent.setEquipment(EquipmentSlot.Mainhand, item);
    }
}

export class onUseWrapper extends ItemComponent {
    constructor() {
        super();
        this.onUse = this.onUse.bind(this);
    }

    onUse(e) { }
}


export class onHitEntityWrapper extends ItemComponent {
    constructor() {
        super();
        this.onHitEntity = this.onHitEntity.bind(this);
    }

    onHitEntity(e) { }
}

export class onConsumeWrapper extends ItemComponent {
    constructor() {
        super();
        this.onConsume = this.onConsume.bind(this);
    }

    /**
     * @abstract
     * @param {ItemComponentConsumeEvent} e 
     */
    onConsume(e) { }
}

export class onUseOnWrapper extends ItemComponent {
    constructor() {
        super();
        this.onUseOn = this.onUseOn.bind(this);
    }

    /**
     * @abstract
     * @param {ItemComponentUseOnEvent} e 
     */
    onUseOn(e) { }
}