function flights = LEM_requests2cells(requests,cells,hd,del_t)
% LEM_requests2cells - convert flight requests to flight paths in cells
% On input:
%     requests (requests struct): requests info
%     cells (cells struct): grid info
%     hd (float): headway distance
%     del_t (float): time step in 
% On output:
%     flights (flights struct): flight info
% Call:
%     flights = LEM_requests2cells(requests,cells,hd);
% Author:
%     T. Henderson
%     UU
%     Fall 2020
%

LOWER_ALTITUDE = 467;
UPPER_ALTITUDE = 534;

num_flights = length(requests);
launch_vertexes = cells.launch_sites;
num_launch_vertexes = length(launch_vertexes);
land_vertexes = cells.land_sites;
num_land_vertexes = length(land_vertexes);

flights = [];

for f = 1:num_flights
    f;
    launch_index = requests(f).launch_index;
    launch_pt = launch_vertexes(launch_index,:);
    land_index = requests(f).land_index;
    land_pt = land_vertexes(land_index,:);
    speed = requests(f).speed;
    min_start = requests(f).launch_interval(1);
    max_start = requests(f).launch_interval(2);
    flights(f).speed = speed;
    flights(f).start_time = min_start;
    flights(f).start_interval = [min_start,max_start];
    pt1 = launch_pt;
    altitude = LOWER_ALTITUDE + rand*(UPPER_ALTITUDE-LOWER_ALTITUDE);
    pt4 = land_pt;
    pt2 = [pt1(1:2),altitude];
    pt3 = [pt4(1:2),altitude];
    path_vertexes = [pt1,pt2;pt2,pt3;pt3,pt4];
    flights(f).path_vertexes = path_vertexes;
    len1 = norm(pt2-pt1);
    len2 = norm(pt3-pt2);
    len3 = norm(pt4-pt3);
    total_len = len1 + len2 + len3;
    plan = zeros(3,4);
    plan(1,1) = flights(f).start_time;
    plan(1,2) = plan(1,1) + len1/flights(f).speed;
    plan(1,3) = flights(f).speed;
    plan(1,4) = 1;
    plan(2,1) = plan(1,2);
    plan(2,2) = plan(2,1) + len2/flights(f).speed;
    plan(2,3) = flights(f).speed;
    plan(2,4) = 2;
    plan(3,1) = plan(2,2);
    plan(3,2) = plan(3,1) + len3/flights(f).speed;
    plan(3,3) = flights(f).speed;
    plan(3,4) = 3;
    flights(f).plan = plan;
    path_vertexes = [pt1 pt2; pt2 pt3; pt3 pt4];
    flights(f).route = [pt1,pt2,plan(1,1:2),plan(1,3);...
        pt2,pt3,plan(2,1:2),plan(2,3); pt3,pt4,plan(3,1:2),plan(3,3)];
%    flights(f).d_count = 0;
%    flights(f).grid_count = 0;
    flights(f).decon_time = 0;
    flights(f).end_time = flights(f).start_time...
        + total_len/flights(f).speed;
    index1 = find(launch_vertexes==launch_index);
    index2 = find(land_vertexes==land_index);
    flights(f).launch_index = requests(f).launch_index;
    flights(f).land_index = requests(f).land_index;
    flights(f).telemetry = [pt1,0 0 1,flights(f).speed];
    flights(f).type = 1;
    flights(f).path = cells.cell_lists(launch_index,land_index).cell_els;
end

s_flights = [];
f = 0;
wb = waitbar(0,'FAA-NASA Deconfliction');
while f<num_flights
    waitbar(f/num_flights);
    f = f + 1;
    [f,num_flights];
    if ~isempty(flights(f).path_vertexes)
        n_flight = flights(f);
        tic;
        n_flight = LEM_deconflict(s_flights,n_flight,hd);
        n_flight.decon_time = toc;
        s_flights = [s_flights;n_flight];
    end
end
close(wb);

flights = s_flights;

tch = 0;

