import * as mc from "@minecraft/server";
import { NAMESPACE } from "./anvilConstants";
import { dinoList, binoculars } from "./constants";

export function startUsing(event) {
  const { itemStack, source } = event;

  if (itemStack && itemStack.typeId === binoculars) {
    mc.system.run(() => {
      source.playSound("item.spyglass.use", { volume: 2.0, pitch: 1.25 });
      source.onScreenDisplay.setHudVisibility(mc.HudVisibility.Hide, [mc.HudElement.StatusEffects]);
      source.addTag(`${NAMESPACE}_using_binoculars`);
    });
  }
}

export function stopUsing(event) {
  const { itemStack, source } = event;

  if (itemStack && itemStack.typeId === binoculars) {
    mc.system.run(() => {
      source.playSound("item.spyglass.stop_using", { volume: 2.0, pitch: 1.25 });
      source.onScreenDisplay.setHudVisibility(mc.HudVisibility.Reset, [mc.HudElement.StatusEffects]);
      source.removeEffect("slowness");
      source.removeTag(`${NAMESPACE}_using_binoculars`);
    });
  }
}

export function tick1() {
  for (const player of mc.world.getAllPlayers()) {
    // Binoculars anims
    const mainHand = player.getComponent("equippable").getEquipment(mc.EquipmentSlot.Mainhand);
    if (mainHand && mainHand.typeId === binoculars) {
      if (player.hasTag(`${NAMESPACE}_using_binoculars`)) {
        player.playAnimation("animation.hf_da.player.using_binoculars", {
          stopExpression: "v.is_first_person || !q.is_item_name_any('slot.weapon.mainhand', 'hf_da:binoculars')",
        });
      } else {
        player.playAnimation("animation.hf_da.player.holding_binoculars", {
          stopExpression: "v.is_first_person || !q.is_item_name_any('slot.weapon.mainhand', 'hf_da:binoculars')",
        });
      }
    }

    // Binoculars logic
    if (player.hasTag(`${NAMESPACE}_using_binoculars`)) {
      player.addEffect("slowness", 5, { amplifier: 5, showParticles: false });
      const dino = player.getEntitiesFromViewDirection({ families: ["dinosaur"], maxDistance: 64 })[0];
      if (dino && dino.entity && dino.entity.isValid() && dino.entity.typeId.startsWith(NAMESPACE)) {
        const [ns, dinoName] = dino.entity.typeId.split(":");
        const information = [
          { translate: `${NAMESPACE}.binoculars_info.title.name` },
          { text: `${dinoList[dinoName].name}` },
          { text: `\n` },
          { translate: `${NAMESPACE}.binoculars_info.title.personality` },
          { translate: `${dinoList[dinoName].personality}` },
          { text: `\n` },
          { translate: `${NAMESPACE}.binoculars_info.title.activity` },
          { translate: `${dinoList[dinoName].activity}` },
          { text: `\n` },
          { translate: `${NAMESPACE}.binoculars_info.title.mountable` },
          { translate: `${dinoList[dinoName].mountable}` },
          { text: `\n` },
          { translate: `${NAMESPACE}.binoculars_info.title.tameable` },
          { translate: `${dinoList[dinoName].tameable}` },
          { text: `\n` },
          { translate: `${NAMESPACE}.binoculars_info.title.danger_level` },
          { translate: `${dinoList[dinoName].danger_level}` },
        ];
        player.onScreenDisplay.setActionBar(information);
      } else {
        player.onScreenDisplay.setActionBar([{ translate: `message.${NAMESPACE}.binoculars.no_target` }]);
      }
    }
  }
}
