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


export function checkPlayerMovement(player) {
  if (!player.hasTag("4kscheckmovement")) return

  const playerVelocity = player.getVelocity()
  if (player.isSprinting || player.isSneaking || player.isJumping || (playerVelocity.x != 0 || playerVelocity.z != 0 || playerVelocity.y != 0)) {
    player.removeTag("4kscheckmovement")
    player.runCommand(`/playanimation @s animation.4ks_wp.vanilla_revert animation.4ks_wp.none 0.0 "(v.actions_and_stuff ?? 0) || !v.is_first_person || !q.equipped_item_any_tag('slot.weapon.mainhand','4ks_wp:custom_tools')" ctrl.4ks_wp.vanilla_revert`)
    player.runCommand(`/playanimation @s animation.4ks_wp.none animation.4ks_wp.vanilla_revert 0.0 "!(v.actions_and_stuff ?? 0) && v.is_first_person &&q.equipped_item_any_tag('slot.weapon.mainhand','4ks_wp:custom_tools')" ctrl.4ks_wp.vanilla_revert`)
  }
}

export async 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");
  player.addTag("4kscheckmovement")
}

export function checkPlayerItems(player) {
  handleCriticalDamage(player);
  handleSpecialWeapons(player);
  handleCutlassDoubleJump(player);
  handleCrossbowHolding(player);
  handleDragonWeapons(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 handleDragonWeapons(player) {
  const equippable = player.getComponent("minecraft:equippable");
  const mainHand = equippable.getEquipment("Mainhand");

  if (mainHand?.typeId.includes("4ks_wp:") && mainHand?.typeId.includes("dragon_")
    && !player.hasTag(TAGS.SCULK_ARMOR) &&
    (!mainHand.typeId.includes("_pickaxe") && !mainHand.typeId.includes("_shovel") && !mainHand.typeId.includes("_hoe")
      && !mainHand.typeId.includes("_helmet") && !mainHand.typeId.includes("_chestplate") && !mainHand.typeId.includes("_leggings") && !mainHand.typeId.includes("_boots")
      && !mainHand.typeId.includes("_ingot") && !mainHand.typeId.includes("_scrap") && !mainHand.typeId.includes("_block"))) {
    if (!player.hasTag("4ksdragonsword")) {
      player.runCommand("/effect @s strength infinite 0 true")
      player.addTag("4ksdragonsword")
    }
  } else {
    if (!player.hasTag(TAGS.DRAGON_ARMOR) && !player.hasTag(TAGS.CUSTOM_MAIN_HAND_SWORD) && player.hasTag("4ksdragonsword")) {
      player.removeTag("4ksdragonsword")
      player.runCommand("/effect @s strength 0 0 true")
    }
  }
}

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);
    }
  }
} 