function other_flights = UTM_conflicting_flights(flights,lanes,...
    lane_degrees,co_lanes,f_td)
% UTM_conflicting_flights - flights in lanes leading to this lane (both at
%    the lane entry point and lane exit point
% On input:
%     flights (flight struct): flight info
%     lanes (nx6 array): lane entry/exit points by row
%     co_lanes (co_lanes struct): lane connection info
%     fp (int): current flight index
%     lane_number (int): lane index of flight fp
% On output:
%     p_flights (vector): potentially conflicted flight indexes
% Call:
%     pf = UTM_conflicting_flights(flights,lanes,co_lanes,5,12);
% Author:
%     T. Henderson
%     UU
%     Summer 2020
%

UTM_load_vars;

other_flights = [];

if flights(f_td).status~=IN_FLIGHT
    return
end

f_td_lane_index = flights(f_td).lane_index;
lane1 = flights(f_td).traj(f_td_lane_index);

% check flights in same lane
f_list = UTM_flights_in_lane(flights,lane1);
index = find(f_td==f_list(:,1));
f_list = f_list(1:index-1,:);
if ~isempty(f_list)
    other_flights = [f_list(end,1), lane1];
    return
end

% check previous lanes
entry_in_lanes = co_lanes(lane1).entry_in;
if ~isempty(entry_in_lanes)
    num_entry_in_lanes = length(entry_in_lanes);
    for k = 1:num_entry_in_lanes
        lane_k = entry_in_lanes(k);
        f_list = UTM_flights_in_lane(flights,lane_k);
        if ~isempty(f_list)
            other_flights = [other_flights; f_list(end,1),lane_k];
        end
    end
end

return

f_td_lane_index = flights(f_td).lane_index;
lane_length1 = flights(f_td).lane_lengths(f_td_lane_index);
lane1 = flights(f_td).traj(f_td_lane_index);
dist1 = flights(f_td).lane_dists(f_td_lane_index);
%speed1 = flights(f_td).lane_speeds(f_td_lane_index);
%TOA1 = flights(f_td).TOA(f_td_lane_index);
option = UTM_deconflict_option(lane_degrees,lane1,dist1);
%te1 = TOA1;

switch option
    case {1,2,9,10,13,14}  % launch lane, 3M, between lane entry and exit points
        
        % set up f_td info
        lane2 = flights(f_td).traj(f_td_lane_index+1);
%        P1 = lanes(lane1,1:3);
%        P2 = lanes(lane1,4:6);
%        dirP = P2 - P1;
%        dirP = dirP/norm(dirP);
%        O1 = P1 + dist1*dirP;
%        V1 = speed1*dirP;
%        ts1 = cur_time;
%        te1 = TOA1;
        
        % set up other flights
%        O2 = [];
%        V2 = [];
%        ts2 = [];
%        te2 = [];
        other_flights = [];
        
        % same lane flights
        [min_d,f_ip] = UTM_im_prec_flight(flights,lane1,f_td);
        if f_ip>0
            other_flights = [other_flights; f_ip, lane1];
%            f_ip_lane_index = flights(f_ip).lane_index;
%            f_ip_speed1 = flights(f_ip).lane_speeds(f_ip_lane_index);
%            f_ip_dist1 = flights(f_ip).lane_dists(f_ip_lane_index);
%            f_ip_TOA1 = flights(f_ip).TOA(f_ip_lane_index);
%            f_ip_loc = P1 + f_ip_dist1*dirP;
%            O2 = [O2;f_ip_loc];
%            V2 = [V2; f_ip_speed1*dirP];
%            ts2 = [ts2; cur_time];
%            te2 = [te2; f_ip_TOA1];
        end
        
        % other merging lane flights
        exit_in_lanes = co_lanes(lane1).exit_in;
        if ~isempty(exit_in_lanes)
            num_exit_in_lanes = length(exit_in_lanes);
            for k = 1:num_exit_in_lanes
                lane_k = exit_in_lanes(k);
                f_list = UTM_flights_in_lane(flights,lane_k);
                if ~isempty(f_list)
                    num_f_list = length(f_list(:,1));
                    for p = 1:num_f_list
                        f = f_list(p,1);
                        other_flights = [other_flights; f, lane_k];
 %                       f_dist = f_list(p,2);
 %                       f_TOA1 = f_list(p,3);
 %                       f_lane_index = flights(f).lane_index;
 %                       f_speed1 = flights(f).lane_speeds(f_lane_index);
 %                       Q1 = lanes(lane_k,1:3);
 %                       Q2 = lanes(lane_k,4:6);
 %                       dirQ = Q2 - Q1;
 %                       dirQ = dirQ/norm(dirQ);
 %                       loc2 = Q1 + f_dist*dirQ;
 %                       O2 = [O2; loc2];
 %                       V2 = [V2; f_speed1*dirQ];
 %                       ts2 = [ts2; cur_time];
 %                       te2 = [te2; f_TOA1];
                    end
                end
            end
        end
        
        % last flights into lane2
        f_info = UTM_flights_in_lane(flights,lane2);
        last_flight = [];
        if ~isempty(f_info)
            f1 = f_info(1,1);
            last_flight = f1;
            other_flights = [other_flights; f1, lane2];
%            f1_dist = f_info(1,2);
%            f1_TOA = f_info(1,3);
%            f1_lane_index = flights(f1).lane_index;
%            Q1 = lanes(lane2,1:3);
%            Q2 = lanes(lane2,4:6);
%            dirQ = Q2 - Q1;
%            dirQ = dirQ/norm(dirQ);
%            f1_loc = Q1 + f1_dist*dirQ;
%            f1_speed = flights(f1).lane_speeds(f1_lane_index);
%            O2 = [O2; f1_loc];
%            V2 = [V2; f1_speed*dirQ];
%            ts2 = [ts2; cur_time];
%            te2 = [te2; te1];
        end
        
    case {3,4,11,12,15,16}  % launch lane, 3D
        % set up f_td info
        lane2 = flights(f_td).traj(f_td_lane_index+1);
%        P1 = lanes(lane1,1:3);
%        P2 = lanes(lane1,4:6);
%        dirP = P2 - P1;
%        dirP = dirP/norm(dirP);
%        O1 = P1 + dist1*dirP;
%       V1 = speed1*dirP;
%        ts1 = cur_time;
%        te1 = TOA1;
        
        % set up other flights
%        O2 = [];
%        V2 = [];
%        ts2 = [];
%        te2 = [];
        other_flights = [];
        
        % all same lane flights
        f_list = UTM_flights_in_lane(flights,lane1);
        index = find(f_list(:,1)==f_td);
        f_list = f_list(index+1:end,:);
        if ~isempty(f_list)
            num_f_list = length(f_list(:,1));
            for p = 1:num_f_list
                f = f_list(p,1);
                other_flights = [other_flights; f, lane1];
%                f_dist = f_list(p,2);
%                f_TOA1 = f_list(p,3);
%                f_lane_index = flights(f).lane_index;
%                f_speed1 = flights(f).lane_speeds(f_lane_index);
%                f_loc1 = P1 + f_dist*dirP;
%                O2 = [O2; f_loc1];
%                V2 = [V2; f_speed1*dirP];
%                ts2 = [ts2; cur_time];
%                te2 = [te2; f_TOA1];
            end
        end
        
        % other diverging lane flights
        exit_out_lanes = co_lanes(lane1).exit_out;
        if ~isempty(exit_out_lanes)
            num_exit_out_lanes = length(exit_out_lanes);
            for k = 1:num_exit_out_lanes
                lane_k = exit_out_lanes(k);
                f_list = UTM_flights_in_lane(flights,lane_k);
                if ~isempty(f_list)
                    f = f_list(1,1);
                    other_flights = [other_flights; f, lane_k];
 %                   f_dist = f_list(1,2);
 %                   f_TOA1 = f_list(1,3);
 %                   f_lane_index = flights(f).lane_index;
 %                   f_speed1 = flights(f).lane_speeds(f_lane_index);
 %                   Q1 = lanes(lane_k,1:3);
 %                   Q2 = lanes(lane_k,4:6);
 %                   dirQ = Q2 - Q1;
 %                   dirQ = dirQ/norm(dirQ);
 %                   loc2 = Q1 + f_dist*dirQ;
 %                   O2 = [O2; loc2];
 %                   V2 = [V2; f_speed1*dirQ];
 %                   ts2 = [ts2; cur_time];
 %                   te2 = [te2; f_TOA1];
                end
            end
        end
        last_flight = [];
        
    case {5,6,7,8}  % landing lane
        % set up f_td info
%        P1 = lanes(lane1,1:3);
%        P2 = lanes(lane1,4:6);
%        dirP = P2 - P1;
%        dirP = dirP/norm(dirP);
%        O1 = P1 + dist1*dirP;
%        V1 = speed1*dirP;
%        ts1 = cur_time;
%        te1 = TOA1;
        
        % same lane flights
        [min_d,f_ip] = UTM_im_prec_flight(flights,lane1,f_td);
        if f_ip>0
            other_flights = [other_flights; f_ip, lane1];
%            f_ip_lane_index = flights(f_ip).lane_index;
%            f_ip_speed1 = flights(f_ip).lane_speeds(f_ip_lane_index);
%            f_ip_dist1 = flights(f_ip).lane_dists(f_ip_lane_index);
%            f_ip_TOA1 = flights(f_ip).TOA(f_ip_lane_index);
%            f_ip_loc = P1 + f_ip_dist1*dirP;
%            O2 = f_ip_loc1;
%            V2 = f_ip_speed1*dirP;
%            ts2 = cur_time;
%            te2 = f_ip_TOA1;
        end
        
    case {17,18}  % single lane into single lane

        % set up f_td info
%        P1 = lanes(lane1,1:3);
%        P2 = lanes(lane1,4:6);
%        dirP = P2 - P1;
%        dirP = dirP/norm(dirP);
%        O1 = P1 + dist1*dirP;
%        V1 = speed1*dirP;
%        ts1 = cur_time;
%        te1 = TOA1;
        
        % get immediately preceding flight in lane
        [min_d,f_ip] = UTM_im_prec_flight(flights,lane1,f_td);
        
        if f_ip>0
            other_flights = [f_ip, lane1];
%            f_ip_lane_index = flights(f_ip).lane_index;
%            f_dist = flights(f_ip).lane_dists(f_ip_lane_index);;
%            f_TOA1 = flights(f_ip).TOA(f_ip_lane_index);
%            f_speed1 = flights(f_ip).lane_speeds(f_ip_lane_index);
%            O2 = P1;
%            V2 = f_speed1*dirP;
%            ts2 = cur_time;
%            te2 = f_TOA1;
%            lane2 = flights(f_ip).traj(f_ip_lane_index+1);
%            f_TOD2 = flights(f_ip).TOD(f_lane_index+1);
%            f_speed2 = flights(f_ip).lane_speeds(f_ip_lane_index+1);
%            Q1 = lanes(lane2,1:3);
%            Q2 = lanes(lane2,4:6);
%            dirQ = Q2 - Q1;
%            dirQ = dirQ/norm(dirQ);
%            O22 = Q1;
%            V22 = f_speed2*dirQ;
%            ts22 = f_TOD2;
        end
            
end
