import * as mc from "@minecraft/server"

const scythe_range = 4;
const scythe_damage = 6;
const scythe_vertical_knockback = 0.5;
const scythe_horizontal_knockback = 0.5;

const mace_range = 2;
const mace_reach = 2;
const mace_damage = 7;
const mace_vertical_knockback = 1.75;
const mace_horizontal_knockback = 0.75;

const halberd_range = 7;
const halberd_reach = 4;
const halberd_damage = 8;
const halberd_vertical_knockback = 0.4;
const halberd_horizontal_knockback = 0.0;

const SOUND_SCYTHE = "weapon.pb_dino:scythe.hit";
const SOUND_MACE = "weapon.pb_dino:mace.hit";
const SOUND_HALBERD = "weapon.pb_dino:halberd.hit";
const SOUND_OPTIONS = { pitch: 1.0, volume: 100.0, };

itemUseListener();

function itemUseListener() {
  mc.world.afterEvents.itemUse.subscribe((eventData) => {
    let player = eventData.source;
    let itemUse = eventData.itemStack;
    if (itemUse === undefined) return;

    if (itemUse.typeId == "pb_dino:fossilbone_scythe") {
      const cooldown = itemUse.getComponent("minecraft:cooldown");
      if (cooldown.getCooldownTicksRemaining(player) > 78) attackFossilboneScythe(player)
    }
    if (itemUse.typeId == "pb_dino:sauroscale_mace") {
      const cooldown = itemUse.getComponent("minecraft:cooldown");
      if (cooldown.getCooldownTicksRemaining(player) > 78) attackSauroscaleMace(player)
    }
    if (itemUse.typeId == "pb_dino:ancient_obsidian_halberd") {
      const cooldown = itemUse.getComponent("minecraft:cooldown");
      if (cooldown.getCooldownTicksRemaining(player) > 78) attackAncientObsidianHalberd(player)
    }
  })
}

function attackFossilboneScythe(player){
  player.playAnimation(
    "animation.pb_dino.fossilbone_scythe.attack2_3p",
    {
      blendOutTime: 1,
      stopExpression: "query.any_animation_finished"
    }
  );
  
  mc.system.runTimeout(() => {
    let evt_location = player.location;
    let dimension = player.dimension.id;
  
    mc.world.playSound(SOUND_SCYTHE, player.location, SOUND_OPTIONS);
    mc.world.getDimension(dimension).spawnParticle('pb_dino:slash_storm_360', evt_location);
    for (let entity of mc.world.getDimension(dimension).getEntities({ location: evt_location,  families: ["mob"], maxDistance: scythe_range})) 
    {   
        mc.world.getDimension(dimension).spawnParticle('pb_dino:phys_hit', entity.location);
        entity.applyKnockback(entity.location.x - evt_location.x, entity.location.z - evt_location.z, scythe_horizontal_knockback , scythe_vertical_knockback);
        entity.applyDamage(scythe_damage,{cause: mc.EntityDamageCause.entityAttack, damagingEntity: player});
    }
  }, 5);
}

function attackSauroscaleMace(player){
  player.playAnimation(
    "animation.pb_dino.sauroscale_mace.attack2_3p",
    {
      blendOutTime: 1,
      stopExpression: "query.any_animation_finished"
    }
  );

  mc.system.runTimeout(() => {
    mc.world.playSound(SOUND_MACE, player.location, SOUND_OPTIONS);
  }, 4);  

  
  mc.system.runTimeout(() => {
    let evt_location = player.location;
    let dimension = player.dimension.id;
  
    let view_vector = player.getViewDirection();
    view_vector = {x: view_vector.x, y: 0, z: view_vector.z};
    const loc = {x: evt_location.x+(view_vector.x*mace_reach), y: evt_location.y, z: evt_location.z+(view_vector.z*mace_reach)};

    for (let entity of mc.world.getDimension(dimension).getEntities({ location: loc,  families: ["mob"], maxDistance: mace_range})) 
      {   
          mc.world.getDimension(dimension).spawnParticle('pb_dino:phys_hit', entity.location);
          entity.applyKnockback(entity.location.x - loc.x, entity.location.z - loc.z, mace_horizontal_knockback , mace_vertical_knockback);
          entity.applyDamage(mace_damage,{cause: mc.EntityDamageCause.entityAttack, damagingEntity: player});
          entity.addEffect('slowness',2*20,{amplifier:4,showParticles:false});
      }

  }, 10);  
}

function attackAncientObsidianHalberd(player){
  player.playAnimation(
    "animation.pb_dino.sauroscale_mace.attack_3p",
    {
      blendOutTime: 1,
      stopExpression: "query.any_animation_finished"
    }
  );

  mc.system.runTimeout(() => {
    mc.world.playSound(SOUND_HALBERD, player.location, SOUND_OPTIONS);
  }, 6);  

  
  mc.system.runTimeout(() => {
    let evt_location = player.location;
    let dimension = player.dimension.id;
  
    let view_vector = player.getViewDirection();
    view_vector = {x: view_vector.x, y: 0, z: view_vector.z};
    const loc = {x: evt_location.x+(view_vector.x*halberd_reach), y: evt_location.y, z: evt_location.z+(view_vector.z*halberd_reach)};

    mc.world.getDimension(dimension).spawnParticle('pb_dino:ground_shockwave', loc);
    mc.world.getDimension(dimension).spawnParticle('pb_dino:nova_cloud', loc);

    for (let entity of mc.world.getDimension(dimension).getEntities({ location: loc,  families: ["mob"], maxDistance: halberd_range})) 
      {   
          mc.world.getDimension(dimension).spawnParticle('pb_dino:phys_hit', entity.location);
          entity.applyKnockback(entity.location.x - loc.x, entity.location.z - loc.z, halberd_horizontal_knockback , halberd_vertical_knockback);
          entity.applyDamage(halberd_damage,{cause: mc.EntityDamageCause.entityAttack, damagingEntity: player});
          entity.addEffect('slowness',5*20,{amplifier:2,showParticles:false});
      }

  }, 14);  
}