function [u,t_min] = LEM_get_UAS(UAS,pt,vertexes)
% LEM_get_UAS - get next available UAS for delivery
% On input:
%     UAS (UAS struct): UAS info
%       .schedule (nx4 array): 
%         each row: launch index, land index, launch time, land inde
%     pt (3x1 vector): goal vertex
%     vertexes (kx3 array): all vertexes (indexed by launch index)
% On output:
%     u (int): UAS index
%     t_min (float): available launch time
% Call:
%     [u,t] = LEM_get_UAS(UAS,vertexes(27,:),vertexes);
% Author:
%     T. Henderson
%     UU
%     Spring 2021
%

LOAD_TIME = 300;

num_UAS = length(UAS);
available = zeros(num_UAS,1);

for k = 1:num_UAS
    sched = UAS(k).schedule;
    [num_flights,dummy] = size(sched);
    if num_flights==1
        t_avail = sched(end,end) + LOAD_TIME;
    else
        t_avail = sched(end-1,end) + LOAD_TIME;
    end
    available(k) = t_avail;
end
[vals,indexes] = sort(available);
u = indexes(1);
t_min = vals(1);
return

u_list = find(vals==min(vals));
ind2 = indexes(u_list);
min_d = Inf;
min_index = 0;
num_ind2 = length(ind2);
for k = 1:num_ind2
    indk = ind2(k);
    v1 = vertexes(UAS(indk).schedule(1,1),:);
    if norm(v1-pt)<min_d
        min_d = norm(v1-pt);
        min_index = indk;
    end
end
u = min_index;
t_min = vals(1);
tch = 0;
