import { TAGS, ARMOR_EFFECT_STATS } from "../data.js";
import { playerUseSpecialSword, playerHoldSpecialWeapon } from "./weaponHandler.js";
import { system } from "@minecraft/server";

export function handlePlayerSpawn(e) {
  const player = e.player;
  player.clearDynamicProperties();
  player.runCommand("/scoreboard objectives add 4kswppoint dummy");
  player.removeTag("4kswphalberd");
  player.runCommand("/execute as @s unless entity @e[type=4ks_wp:item_checker] run summon 4ks_wp:item_checker");
  player.runCommand("/event entity @e[type=4ks_wp:invisible_hitter,r=3] 4ks_wp:despawn");
  player.runCommand("/give @s[tag=!4kswpinit] 4ks_wp:guidebook");
  player.addTag("4kswpinit");
}

export function checkPlayerItems(player) {
  handleCriticalDamage(player);
  handleSpecialWeapons(player);
  handleCutlassDoubleJump(player);
  handleCrossbowHolding(player);
}

function handleCriticalDamage(player) {
  if (player.isFalling && !player.isOnGround && 
    !player.isInWater && !player.isClimbing) {
    player.setDynamicProperty("4ks_we:critical_damage", true);
  } else {
    player.setDynamicProperty("4ks_we:critical_damage", false);
  }
}

function handleSpecialWeapons(player) {
  if (player.hasTag(TAGS.USE_ITEM)) {
    playerUseSpecialSword(player);
  } else {
    playerHoldSpecialWeapon(player);
  }
}

function handleCutlassDoubleJump(player) {
  if (player.isJumping && player.hasTag("4ksscutlass")) {
    if (player.getDynamicProperty("4ks_wp:jump_delay")) return;
    
    if (player.getDynamicProperty("4ks_wp:jump") == undefined) {
      player.setDynamicProperty("4ks_wp:jump", system.currentTick);
      return;
    }
    
    const previousTick = player.getDynamicProperty("4ks_wp:jump");
    const different = system.currentTick - previousTick;

    if (different <= 2) return;
    if (different > 8) {
      player.setDynamicProperty("4ks_wp:jump", undefined);
      return;
    }

    player.applyKnockback(0, 0, 0, 0.56);
    player.setDynamicProperty("4ks_wp:jump_delay", true);
  }
  else if (player.isOnGround && player.getDynamicProperty("4ks_wp:jump_delay")) {
    if (player.getDynamicProperty("4ks_wp:jump") != undefined) {
      player.setDynamicProperty("4ks_wp:jump_delay", false);
      player.setDynamicProperty("4ks_wp:jump", undefined);
    }
  }
}

function handleCrossbowHolding(player) {
  const equippable = player.getComponent("minecraft:equippable");
  const mainHand = equippable.getEquipment("Mainhand");
  
  if (mainHand?.typeId.includes("4ks_wp:") && mainHand?.typeId.includes("_loaded")) {
    if (player.getDynamicProperty("4ks_wp:holding_crossbow")) return;
    player.setDynamicProperty("4ks_wp:holding_crossbow", true);
    player.runCommand(`/playanimation @s animation.player.crossbow_hold null 0 \"v.is_first_person || !query.is_item_name_any('slot.weapon.mainhand', 0, '${mainHand.typeId}')\"`)
  } else {
    if (player.getDynamicProperty("4ks_wp:holding_crossbow")) {
      player.setDynamicProperty("4ks_wp:holding_crossbow", false);
    }
  }
} 