function n_flight = LEM_deconflict(s_flights,flight,cell_flights,hd)
% LEM_deconflict - uses FAA-NASA approach for deconfliction
% On input:
%     s_flights (flights struct): scheduled flights
%     flight (flight struct): new flight
%     cell_flights (cell_flights struct): flights through each cell
%     hd (float): headway distance
% On output:
%     n_flight (flight struct): new scheduled flight
% Call:
%     n_flight = LEM_deconflict(s_flights,n_flight,cell_flights,h_x);
% Author:
%     T. Henderson
%     UU
%     Spring 2020
%

OFFSET = 5;  % time delay increment

n_flight = flight;
num_s_flights = length(s_flights);

if num_s_flights<1
    return
end

fcells = flight.path;
num_fcells = length(fcells);
traj1 = flight.path_vertexes;
d11 = norm(traj1(1,4:6)-traj1(1,1:3));
d12 = norm(traj1(2,4:6)-traj1(2,1:3));
d13 = norm(traj1(3,4:6)-traj1(3,1:3));
s1 = flight.speed;

OK = zeros(num_s_flights,1);
done = 0;
while done==0
    for c = 1:num_fcells
        cf = cell_flights(fcells(c)).flights;
        num_cf = length(cf);
        for k = 1: num_cf
            traj2 = s_flights(cf(k)).path_vertexes;
            s2 = s_flights(cf(k)).speed;
            d21 = norm(traj2(1,4:6)-traj2(1,1:3));
            d22 = norm(traj2(2,4:6)-traj2(2,1:3));
            d23 = norm(traj2(3,4:6)-traj2(3,1:3));
            ind1 = 0;
            while ind1<3
                ind1 = ind1 + 1;
                ind2 = 0;
                p11 = traj1(ind1,1:3);
                p12 = traj1(ind1,4:6);
                if ind1==1
                    t11 = n_flight.start_time;
                    t12 = t11 + d11/s1;
                elseif ind1==2
                    t11 = n_flight.start_time + d11/s1;
                    t12 = t11 + (d11+d12)/s1;
                else
                    t11 = n_flight.start_time + (d11+d12)/s1;
                    t12 = (d11+d12+d13)/s1;
                end
                while ind2<3
                    ind2 = ind2 + 1;
                    p21 = traj2(ind2,1:3);
                    p22 = traj2(ind2,4:6);
                    if ind2==1
                        t21 = s_flights(cf(k)).start_time;
                        t22 = t21 + d21/s2;
                    elseif ind2==2
                        t21 = s_flights(cf(k)).start_time + d21/s2;
                        t22 = t21 + (d21+d22)/s2;
                    else
                        t21 = s_flights(cf(k)).start_time + (d21+d22)/s2;
                        t22 = t21 + (d21+d22+d23)/s2;
                    end
                    b = 0;
                    if ~(t12<t21|t22<t11)
                        [b,min_d,t_cpa] = LEM_CPA(p11,p12,s1,t11,p21,...
                            p22,s2,t21,hd);
                    end
                    if b==1
                        ind1 = 3;
                        ind2 = 3;
                        c = num_fcells;
                        n_flight.start_time = n_flight.start_time + OFFSET;
                        if n_flight.start_time>n_flight.start_interval(2)
                            n_flight.type = 0;
                            done = 1;
                        end
                    end
                end
            end
        end
        if c==num_fcells&done==0
            done = 1;
        end
    end
end
