function n_flight = UAM_deconflict_Ben(s_flights,flight,del_t,h_d)
% UAM_deconflict - uses FAA-NASA approach for deconfliction
% On input:
%     s_flights (flights struct): scheduled flights
%     flight (flight struct): new flight
%     del_t (float): time step
%     h_d (float): distance headway
% On output:
%     n_flight (flight struct): new scheduled flight
% Call:
%     n_flight = UAM_deconflict(s_flights,n_flight,del_t,h_t);
% Author:
%     T. Henderson
%     UU
%     Spring 2020
%

DELAY = 0.01;

d_count = 0;
grid_count = 0'

n_flight = flight;
n_flight.grid_count = 0;
n_flight.pinch_count = 0;
n_flight.space_count = 0;
n_flight.time_count = 0;
decon = 1;
start_time = n_flight.start_time;
n_grid_els = n_flight.grid_els;
if isempty(s_flights)
    return
end

num_flights = length(s_flights);
speed = n_flight.speed;
traj = n_flight.traj;
O11 = traj(1,1:3);
V11 = traj(1,4:6) - traj(1,1:3);
V11 = speed*V11/norm(V11);
O12 = traj(2,1:3);
V12 = traj(2,4:6) - traj(2,1:3);
V12 = speed*V12/norm(V12);
O13 = traj(3,1:3);
V13 = traj(3,4:6) - traj(3,1:3);
V13 = speed*V13/norm(V13);

%h_d = speed*h_t;
total_dist = 0;
for t = 1:length(traj(:,1))
    total_dist = total_dist + norm(traj(t,4:6)-traj(t,1:3));
end
time_required = total_dist/speed;
end_time = start_time + time_required;
n_flight.end_time = end_time;
possible = [];
for f = 1:num_flights
    f_grid_els = s_flights(f).grid_els;
    int_set = intersect(n_grid_els,f_grid_els);
    grid_count = grid_count + length(int_set);
    if ~isempty(int_set)
        possible = [possible,f];
    end
end
num_possible = length(possible);
if num_possible==0
    return
end
n_flight.d_count = 0;
s_flights = s_flights(possible);
num_s_flights = length(s_flights);
done = 0;
while done==0  % shift start time until no pinch issues
    done = 1;
    for f = 1:num_s_flights
        d_count = d_count + 9;
        flight_path = n_flight.flight_path;
        traj2 = s_flights(f).traj;
        flight_path2 = s_flights(f).flight_path;
        speed2 = s_flights(f).flight_path(1,3);
        O21 = traj2(1,1:3);
        V21 = traj2(1,4:6) - traj2(1,1:3);
        V21 = speed2*V21/norm(V21);
        O22 = traj2(2,1:3);
        V22 = traj2(2,4:6) - traj2(2,1:3);
        V22 = speed2*V22/norm(V22);
        O23 = traj2(3,1:3);
        V23 = traj2(3,4:6) - traj2(3,1:3);
        V23 = speed2*V23/norm(V23);
        ts11 = flight_path(1,1);
        te11 = flight_path(1,2);
        ts21 = s_flights(f).flight_path(1,1);
        te21 = s_flights(f).flight_path(1,2);
        ts12 = flight_path(2,1);
        te12 = flight_path(2,2);
        ts22 = s_flights(f).flight_path(2,1);
        te22 = s_flights(f).flight_path(2,2);
        ts13 = flight_path(3,1);
        te13 = flight_path(3,2);
        ts23 = s_flights(f).flight_path(3,1);
        te23 = s_flights(f).flight_path(3,2);
        min_d = zeros(1,9);
        min_t = zeros(1,9);
        [min_d(1),min_t(1)] = trajMinDist(O11,V11,ts11,te11,O21,V21,...
            ts21,te21);
        [min_d(2),min_t(2)] = trajMinDist(O12,V12,ts12,te12,O21,V21,...
            ts21,te21);
        [min_d(3),min_t(3)] = trajMinDist(O13,V13,ts13,te13,O21,V21,...
            ts21,te21);
        [min_d(4),min_t(4)] = trajMinDist(O11,V11,ts11,te11,O22,V22,...
            ts22,te22);
        [min_d(5),min_t(5)] = trajMinDist(O12,V12,ts12,te12,O22,V22,...
            ts22,te22);
        [min_d(6),min_t(6)] = trajMinDist(O13,V13,ts13,te13,O22,V22,...
            ts22,te22);
        [min_d(7),min_t(7)] = trajMinDist(O11,V11,ts11,te11,O23,V23,...
            ts23,te23);
        [min_d(8),min_t(8)] = trajMinDist(O12,V12,ts12,te12,O23,V23,...
            ts23,te23);
        [min_d(9),min_t(9)] = trajMinDist(O13,V13,ts13,te13,O23,V23,...
            ts23,te23);
        if min(min_d)<h_d
            n_flight = UAM_shift_start(n_flight,DELAY);
            done = 0;
        end
    end
end
n_flight.d_count = d_count;

tch = 0;
