import type { MapProjection } from "../geometry/mapProjection";
import type { MapLine } from "../model/mapTypes";

export function drawTracks(ctx: CanvasRenderingContext2D, lines: MapLine[], projection: MapProjection) {
  ctx.save();
  ctx.lineCap = "round";
  ctx.lineJoin = "round";
  ctx.strokeStyle = "rgba(104, 195, 189, 0.76)";
  ctx.lineWidth = 1.2;

  const wrapThreshold = projection.worldSize / 2;
  for (const line of lines) {
    if (line.points.length < 2) continue;
    ctx.beginPath();
    let prevX: number | undefined;
    for (const point of line.points) {
      const screen = projection.project(point);
      if (prevX === undefined || Math.abs(screen.x - prevX) > wrapThreshold) {
        ctx.moveTo(screen.x, screen.y);
      } else {
        ctx.lineTo(screen.x, screen.y);
      }
      prevX = screen.x;
    }
    ctx.stroke();
  }

  ctx.restore();
}

