import * as geolib from 'geolib';
import Parser from '../parser.ts';
import { generateOverflyArc } from '../pathGenerators/generateOverflyArc.ts';
import { computeSpeed } from '../utils/computeSpeed.ts';

// NOTE: No wind adjustments to be made, no clue how *that* would draw
export const TerminatorsVA = (
  leg: VATerminalEntry,
  previousFix: NavFix,
  lastCourse: number
): [NavFix?, LineSegment[]?] => {
  const speed = computeSpeed(leg, previousFix);
  const crsIntoEndpoint = leg.Course.toTrue(previousFix);

  // Compute overfly
  const [line, arcEnd, _lastCourse] = generateOverflyArc(crsIntoEndpoint, lastCourse, previousFix, speed, leg.TurnDir);
  lastCourse = _lastCourse;

  // Compute intercept of crs from arc end and expected altitude
  const targetFix: NavFix = {
    ...geolib.computeDestinationPoint(
      arcEnd,
      (
        ((leg.Alt.parseAltitude() - (previousFix.altitude ?? 0)) / Parser.AC_VS) *
        ((previousFix.speed ? previousFix.speed : Parser.AC_SPEED) / 60)
      ).toMetre(),
      crsIntoEndpoint
    ),
    name: leg.Alt,
    isFlyOver: true,
    altitude: leg.Alt ? leg.Alt.parseAltitude() : previousFix.altitude,
    speed: speed,
    speedConstraint: leg.SpeedLimit,
    altitudeConstraint: leg.Alt,
  };
  line.push([targetFix.longitude, targetFix.latitude]);

  return [targetFix, line];
};
