EFB/Models/RouteModel.cs
2022-02-28 21:53:28 +00:00

154 lines
5.2 KiB
C#

using EFB.Controllers.Form;
using EFB.Models.Route;
using System.Threading.Tasks;
namespace EFB.Models
{
public class RouteModel
{
/*
Route Model - This model contains implementations for different points along the Route
Route only becomes populated after route is recieved from autorouter API
*/
public WaypointModel Departure { get; set; } = null;
public WaypointModel Arrival { get; set; } = null;
public IWaypoint Current { get; set; } = null;
public uint Cruise { get; set; } = 0;
public RouteModel(string departure, string departureRoute, string arrival, string arrivalRoute, uint cruise)
{
if (FormAuthenticator.ValidateICAOCode(departure))
{
Departure = new WaypointModel(departure, departureRoute, 0, 0);
}
if (FormAuthenticator.ValidateICAOCode(arrival))
{
Arrival = new WaypointModel(arrival, arrivalRoute, 0, 0);
}
if (FormAuthenticator.ValidateCruiseAlt(cruise))
{
Cruise = cruise;
}
}
public IWaypoint Next()
{
if (Current.Next != null)
{
Current = Current.Next;
return Current;
}
return null;
}
public IWaypoint Previous()
{
if (Current.Previous != null)
{
Current = Current.Previous;
return Current;
}
return null;
}
//Generate a route Object
public static async Task<RouteModel> StringToRoute(string departure, string arrival, uint cruise, string routeString)
{
var navdataFetch = NavdataModel.Populate();
string[] routeTemp = routeString.Split(" ");
//Set departure and arrival route
string departureRoute = routeTemp[0];
string arrivalRoute = routeTemp[routeTemp.Length - 2];
RouteModel route = new RouteModel(departure, departureRoute, arrival, arrivalRoute, cruise);
route.Departure.Airway = routeTemp[0];
route.Current = route.Departure;
NavdataModel[] navdata = await navdataFetch;
navdata = NavdataModel.MergeSort(ref navdata, 0, navdata.Length - 1);
for (var i = 1; i < routeTemp.Length - 1; i += 2)
{//Already used first item, continue itterating over every other item
IWaypoint next;
NavdataModel currentWaypoint = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, routeTemp[i]);
if (currentWaypoint == null)
{
currentWaypoint = new NavdataModel(0, routeTemp[i], null, "0", "0");
}
//Populate 'next' waypoint
if (routeTemp[i].Length > 3)
{//waypoint Type
next = new WaypointModel(
routeTemp[i],
routeTemp[i + 1],
float.Parse(currentWaypoint.Longitude),
float.Parse(currentWaypoint.Latitude)
);
}
else
{//Navaid Type
next = new NavaidModel(
routeTemp[i],
routeTemp[i + 1],
float.Parse(currentWaypoint.Longitude),
float.Parse(currentWaypoint.Latitude)
);
}
next.Previous = route.Current;
route.Current.Next = next;
route.Current = next;
}
//Connect end of route (linked list)
route.Current.Airway = null;
route.Current.Next = route.Arrival;
route.Arrival.Previous = route.Current;
route.Current = route.Departure;
//Assign departure and arrival coordinate positions
NavdataModel departureNav = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, departure);
NavdataModel arrivalNav = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, arrival);
route.Departure.Latitude = float.Parse(departureNav.Latitude);
route.Departure.Longitude = float.Parse(departureNav.Longitude);
route.Arrival.Latitude = float.Parse(arrivalNav.Latitude);
route.Arrival.Latitude = float.Parse(arrivalNav.Longitude);
return route;
}
//Generate a route String
public static string ParseRoute(string route)
{
route.Replace('/', ' ');
var routeArr = route.Split(' ');
string finalRoute = "";
foreach (var item in routeArr)
{
var waypoint = item.Split('/')[0];
if (waypoint.Length <= 7 && waypoint.Length >= 3 && !waypoint.Contains('-'))
{
finalRoute += $"{waypoint} ";
if (waypoint.Length == 7 && finalRoute.Length > 8)
break;
}
}
return finalRoute;
}
}
}