function res = LEM_EQ_baseline_driver(num_deliveries,num_depot_sites,...
    num_UAS)
% LEM_EQ_baseline driver - earthquake FAA/NASA baseline scenario
% On input:
%     num_deliveries (int): number of requested deliveries
%     num_depot_sites (int): number of launch sites (random locs)
%     num_UAS (int): number of UAS
% On output:
%     res (): results of simulation
% Call:
%     res = LEM_EQ_baseline_driver(3000,10,40);
% Author:
%     T. Henderson
%     UU
%     Spring 2021
%

MIN_ALTITUDE = 467;
MAX_ALTITUDE = 534;
SPEED = 10;
HD = 6;
MAX_DELAY = 100;
LOAD_TIME = 300;
UNLOAD_TIME = 300;

res = [];

% Initialize time
t_start = 0;
t_end = 24*60*60;

% Initialize flights
flights = [];

% Initialize UAS info
for k = 1:num_UAS
    UAS(k).schedule = [0,0,0,0];
end

load('EB_ve.mat');   % Eastbench road intersections and lanes
%                      brings in: vertexes and edges
[num_vertexes,dummy] = size(vertexes);

x_min = min(vertexes(:,1));
x_max = max(vertexes(:,1));
y_min = min(vertexes(:,2));
y_max = max(vertexes(:,2));
del_x = 500;
del_y = 500;
launch_sites = vertexes;
land_sites = vertexes;
hd = 6;
% Create FAA/NASA  cells
cells = LEM_gen_FAA_cells(x_min,x_max,y_min,y_max,del_x,del_y,...
    launch_sites,land_sites,hd)
for k = 1:cells.M*cells.N
    cell_flights(k).flights = [];
end

% Set depot sites
p = randperm(num_vertexes);
depot_sites = p(1:num_depot_sites);
clear p

% Distribute UAS across depot sites
for k = 1:num_UAS
    index = rem(k-1,num_depot_sites) + 1;
    UAS(k).schedule(1,1) = depot_sites(index);
end

requests = zeros(num_vertexes,2);
num_requests = num_vertexes;
[vals,indexes] = sort(t_end*rand(num_vertexes,1));
requests(:,1) = indexes';
requests(:,2) = vals';

% repeat the deliveries num_deliveries times
all_requests = requests;
for d = 1:num_deliveries-1
    all_requests = [all_requests; requests];
end
requests = all_requests;
num_requests = length(requests(:,1));

%wb = waitbar(0,'EQFAA');
for r = 1:num_requests
    [r,num_requests]
%for r = 1:10
%    waitbar(r/num_requests,['Request: ',num2str(r)]);
    delivery_site = requests(r,1);
    request_time = requests(r,2);
    [UAS_index,avail_time] = LEM_EQ_get_UAS(UAS,request_time);
    launch_site = UAS(UAS_index).schedule(end,1);
    flights = LEM_EQ_FAA_reserve(flights,cells,launch_site,...
        delivery_site,MIN_ALTITUDE,MAX_ALTITUDE,SPEED,HD,request_time,...
        MAX_DELAY,UAS_index,cell_flights);
    if flights(end).type==1  % delivery OK; update & schedule return flight
        UAS(UAS_index).schedule(end,2) = delivery_site;
        UAS(UAS_index).schedule(end,3) = flights(end).start_time;
        UAS(UAS_index).schedule(end,4) = flights(end).end_time;
        UAS(UAS_index).schedule = [UAS(UAS_index).schedule;...
            delivery_site,0,0,0];
        launch_site = delivery_site;
        land_site = UAS(UAS_index).schedule(end-1,1);
        request_time = flights(end).end_time + UNLOAD_TIME;
        [flights,cell_flights] = LEM_EQ_FAA_reserve(flights,cells,...
            launch_site,land_site,MIN_ALTITUDE,MAX_ALTITUDE,SPEED,HD,...
            request_time,t_end,UAS_index,cell_flights);
        if flights(end).type==1
            UAS(UAS_index).schedule(end,2) = land_site;
            UAS(UAS_index).schedule(end,3) = flights(end).start_time;
            UAS(UAS_index).schedule(end,4) = flights(end).end_time;
            UAS(UAS_index).schedule = [UAS(UAS_index).schedule;...
            land_site,0,0,0];
        end
    end
    tch = 0;
end
%close(wb);
res.flights = flights;
res.UAS = UAS;

tch = 0;
