import { system, world } from '@minecraft/server';
import { decrementStack, getOppositeDirection, DirectionType, cardinalSides, randomFunction } from './utils/helper';
import { directionToVector3 } from './utils/math';

function durabilityOnChanged(item, player, isHitEntity = false) {
    let level = item.getComponent("minecraft:enchantable")?.getEnchantment("unbreaking")?.level;

    function durability() {
        let durability = item.getComponent("minecraft:durability");

        const t = Math.floor(Math.random() * 100);

        if (t < durability.getDamageChance()) {
            if (!isHitEntity) durability.damage += 1;
            if (durability.damage >= durability.maxDurability) {
                player.playSound("random.break");
                if (!isHitEntity) player.getComponent('equippable').setEquipment('Mainhand', undefined)
            } else {
                if (!isHitEntity) player.getComponent('equippable').setEquipment('Mainhand', item)
            }
        } else {
            if (!isHitEntity) return;
            durability.damage -= 1;
            if (!isHitEntity) player.getComponent('equippable').setEquipment('Mainhand', item);
        }
    }

    const t = Math.floor(Math.random() * 10)
    if (level === 1 && t > 8) return;
    else if (level === 2 && t > 6) return;
    else if (level === 3 && t > 4) return;
    else durability();
}

world.beforeEvents.worldInitialize.subscribe(initEvent => { initEvent.itemComponentRegistry.registerCustomComponent('np_settings:trigger', {
  onUse: e => { e.source.runCommand("function open_setting_ui"); },
});

initEvent.itemComponentRegistry.registerCustomComponent('wp_witheredl_axe:trigger', {
  
  
  onHitEntity: e => { durabilityOnChanged(e.itemStack, e.source, true); },
  onMineBlock: e => { durabilityOnChanged(e.itemStack, e.source, false); },
});

initEvent.itemComponentRegistry.registerCustomComponent('wp_withered_sword:trigger', {
  
  
  onHitEntity: e => { durabilityOnChanged(e.itemStack, e.source, true); },
  onMineBlock: e => { durabilityOnChanged(e.itemStack, e.source, false); },
});

initEvent.itemComponentRegistry.registerCustomComponent('wp_withered_pickaxe:trigger', {
  
  
  onHitEntity: e => { durabilityOnChanged(e.itemStack, e.source, true); },
  onMineBlock: e => { durabilityOnChanged(e.itemStack, e.source, false); },
});

initEvent.itemComponentRegistry.registerCustomComponent('menu_note_intro:trigger', {
  onUse: e => { e.source.runCommand("function note_intro_flip"); },
});

initEvent.itemComponentRegistry.registerCustomComponent('menu_note_1:trigger', {
  onUse: e => { e.source.runCommand("function note_flip_1"); },
});

initEvent.itemComponentRegistry.registerCustomComponent('menu_note_3:trigger', {
  onUse: e => { e.source.runCommand("function note_flip_3"); },
});

initEvent.itemComponentRegistry.registerCustomComponent('menu_note_5:trigger', {
  onUse: e => { e.source.runCommand("function note_flip_5"); },
});

initEvent.itemComponentRegistry.registerCustomComponent('menu_note_6:trigger', {
  onUse: e => { e.source.runCommand("function note_flip_6"); },
});

initEvent.itemComponentRegistry.registerCustomComponent('menu_note_4:trigger', {
  onUse: e => { e.source.runCommand("function note_flip_4"); },
});

initEvent.itemComponentRegistry.registerCustomComponent('menu_note_2:trigger', {
  onUse: e => { e.source.runCommand("function note_flip_2"); },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_morph_item:trigger', {
  onUse: e => { e.source.runCommand("function mphw_enter"); },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_roar:trigger', {
  onUse: e => { 
const functions = ["mphw_roar_1", "mphw_roar_2"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_rage:trigger', {
  onUse: e => { 
const functions = ["mphw_heal"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_exit:trigger', {
  onUse: e => { 
const functions = ["mphw_end"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_cover:trigger', {
  onUse: e => { 
const functions = ["mphw_cover"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_scratch:trigger', {
  onUse: e => { 
const functions = ["hwmp_attack"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_grab:trigger', {
  onUse: e => { e.source.runCommand("function hwmp_grab1"); },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_trap:trigger', {
  onUse: e => { 
const functions = ["hwmp_place1", "hwmp_place2", "hwmp_place3", "hwmp_place4", "hwmp_place5"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_passive_off:trigger', {
  onUse: e => { 
const functions = ["mphw_start_aggressive"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_sleep:trigger', {
  onUse: e => { 
const functions = ["hwmp_sleep"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_passive:trigger', {
  onUse: e => { 
const functions = ["mphw_start_passive"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_ambi:trigger', {
  onUse: e => { 
const functions = ["hwmp_ambi"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_awake:trigger', {
  onUse: e => { 
const functions = ["hwmp_woken"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_cries:trigger', {
  onUse: e => { 
const functions = ["hwmp_chi", "hwmp_cow", "hwmp_sh", "hwmp_pig"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_break:trigger', {
  onUse: e => { 
const functions = ["hwmp_break_on"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_invis:trigger', {
  onUse: e => { 
const functions = ["hwmp_invis"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_torch:trigger', {
  onUse: e => { 
const functions = ["hwmp_torch"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_invis_off:trigger', {
  onUse: e => { 
const functions = ["hwmp_invis_off"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});

initEvent.itemComponentRegistry.registerCustomComponent('hw_mp_break_off:trigger', {
  onUse: e => { 
const functions = ["hwmp_break_off"];
          e.source.runCommand(`function ${randomFunction(functions)}`);
           },
});
 });

