function possible = LEM_MLSD(possible0,requests,f0,lane_lengths,...
    reservations,ht,hd,lanes)
% LEM_MLSD - multi-speed lane strategic deconfliction flight reservation
% On input:
%     possible0 (1x2 vector): min and max possible start times
%     requests (requests struct): request info
%     f0 (int): index in requests for flight to be reserved
%     lane_lengths (1xn vector): length of lanes
%     reservations (reservations struct): flight reservation info
%     ht (float): headway time
% On output:
%     possible (interval set): possible launch intervals
% Call:
%     pos = LEM_MLSD([0,30],requests,k,[10,10,10,10],reserv,2);
% Author:
%     T. Henderson
%     UU
%     Fall 2020/modified Spring 2023
%

possible = [];

path = requests(f0).path;
num_lanes = length(path);
speeds = requests(f0).speeds;
times = lane_lengths(path)./speeds;
constraint_lanes = LEM_constraint_lanes(lanes);
intervals = possible0;
c = 0;
total_time = 0;
while ~isempty(intervals)&c<num_lanes
    c = c + 1;
    lane_index = path(c);
    c_length = lane_lengths(lane_index);
    c_speed = speeds(c);
    if c>1
        offset = times(c-1);   % total_time?
    else
        offset = 0;
    end
    [num_intervals,dummy] = size(intervals);
    intervals = intervals + offset;
    c_flights = reservations(lane_index).flights;
    [num_c_flights,~] = size(c_flights);
    if num_c_flights>0
        for f = 1:num_c_flights
            f_min_speed = Inf;
            f_id = c_flights(f,1);
            f_t1 = c_flights(f,2);
            f_t2 = c_flights(f,3);
            f_speed = c_flights(f,4);
            f_min_speed = min(f_min_speed,f_speed);
            f_req = requests(f_id);
            f_path = f_req.path;
            f_lane_index = find(lane_index==f_path);
            if f_lane_index>1
                f_min_speed = min(f_min_speed,f_req.speeds(f_lane_index-1));
            end
            if f_lane_index<length(f_path)
                f_min_speed = min(f_min_speed,f_req.speeds(f_lane_index+1));
            end
            tr1 = min(intervals(:,1));
            tr2 = max(intervals(:,2));
            tr1e = tr1 + times(c);
            tr2e = tr2 + times(c);
            if ~((f_t1+ht<=tr1&f_t2+ht<=tr1e)|(f_t1-ht>=tr2&f_t2-ht>=tr2e))
                new_intervals = [];
                for k = 1:num_intervals
                    k_intervals = LEM_OK_sched_req_enum(f_t1,f_t2,f_min_speed,...
                        intervals(k,1),intervals(k,2),c_speed,c_length,hd);
                    new_intervals = LEM_merge_intervals(k_intervals,...
                        new_intervals);
                end
                intervals = new_intervals;
                [num_intervals,~] = size(intervals);
            end
        end
        tch = 0;
    end
    total_time = total_time + times(c);
end
total_time = total_time - times(end);
[num_intervals,~] = size(intervals);
for k = 1:num_intervals
    intervals(k,:) = intervals(k,:) - [total_time,total_time];
end

possible = intervals;
