import { MolangVariableMap, Vector3 } from "@minecraft/server";
import { Vector3Add, vector3 } from "./vectorUtils";
import { PARTICLES, spawnParticle } from "./particleUtils";

const arrowTemplate: Vector3[] = [
  vector3(0, 0, 0),

  vector3(1, 1, 0),
  vector3(-1, 1, 0),

  vector3(1.5, 2, 0),
  vector3(-1.5, 2, 0),

  vector3(2, 3, 0),
  vector3(-2, 3, 0),

  vector3(0, 1, 0),
  vector3(0, 2, 0),
  vector3(0, 3, 0),
  vector3(0, 4, 0),
  vector3(0, 5, 0),
];

let angleOffset = 0;
let heightBobbing = 0;
let tickCounter = 0;

function rotate(pos: Vector3, angle: number) {
  const x = pos.x;
  const z = pos.z;

  const newX = x * Math.cos(angle) - z * Math.sin(angle);
  const newZ = x * Math.sin(angle) + z * Math.cos(angle);

  return vector3(newX, pos.y, newZ);
}

const particleData = new MolangVariableMap();
particleData.setColorRGB("variable.color", {
  red: 1,
  green: 1,
  blue: 1,
});

function drawArrow(offsetPos: Vector3) {
  tickCounter++;
  if (tickCounter % 2 == 0) {
    angleOffset += 0.1;
    heightBobbing += 0.3;
    arrowTemplate.forEach((pos) => {
      const rotatedPos = rotate(pos, angleOffset);
      const finalPos = Vector3Add(offsetPos, rotatedPos);
      finalPos.y += Math.sin(heightBobbing) / 2;
      spawnParticle(finalPos, PARTICLES.point, particleData);
    });
  }
}

export { drawArrow };
