import computeDestinationPoint from 'geolib/es/computeDestinationPoint';
import Parser from '../parser';
import { generateOverflyArc } from '../pathGenerators/generateOverflyArc';
import { computeSpeed } from '../utils/computeSpeed';

// 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 = {
    ...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,
    IsFAF: leg.IsFAF,
    IsMAP: leg.IsMAP,
  };
  line.push([targetFix.longitude, targetFix.latitude]);

  return [targetFix, line];
};
