function UR_USS_23(t)
% UR_USS_23 - behavior for USS 23
% On input:
%     t (float): current time
% On output:
%     N/A
% Call:
%     UR_USS_23(178);
% Author:
%     T. Henderson
%     University of Utah
%     Summer 2019
%

USS_ID = 23;
FLIGHT = 'flight';
REGISTER = 'register';
DOT = '.';
UNDERSCORE = '_';
NUM_OPERATORS = 1;
NUM_PLATFORMS = 100;
MIN_ADVANCE_NOTICE = 2; % provide 60 minutes advance notice
TIME_SPREAD = .5;  % allow 2-hour window to scehdule flight
TIME_OFFSET = 0;  % request in next 4 hours + MAX_ADVANCE_NOTICE

REQUEST_PROB = 1; %0.5;
NUM_SPEEDS = 3;
COMM_DIRECTORY = 1;
COMM_SEND = 2;
COMM_RECEIVE = 3;
COMM_REGISTER = 4;
M_USS2FM = 1;
M_FM2USS = 2;
M_REGISTER = 1;
M_REQUEST = 2;

persistent req_no requests flights operators platforms speeds 
persistent launch_vertexes land_vertexes
persistent num_launch_vertexes num_land_vertexes
persistent comms my_comm FM_comm awaiting_register
persistent my_name my_FM_id reset_platforms

if isempty(req_no)
    
    req_no = 0;
    requests = [];
    flights = [];
    my_name = 'UR_USS_23';
    fid = fopen('LAUNCH_SITES','r');
    launch_vertexes = fscanf(fid,'%d');
    fclose(fid);
    num_launch_vertexes = length(launch_vertexes);
    fid = fopen('LAND_SITES','r');
    land_vertexes = fscanf(fid,'%d');
    fclose(fid);
    num_land_vertexes = length(land_vertexes);
    operators = 2*[1:NUM_OPERATORS];
    platforms = zeros(NUM_PLATFORMS,1);
    reset_platforms = [];
    speeds = [1,1,1];
%    speeds = [1,2,3];
    comms = [];
    % Set communications
    my_comm = UR_comms(COMM_REGISTER,my_name,[]);
    FM_comm = UR_comms(COMM_DIRECTORY,'flight-manager',[]);
    mess.type = M_USS2FM;
    mess.code = my_comm;
    mess.action = M_REGISTER;
    mess.data = my_name;
    UR_comms(COMM_SEND,[my_comm,FM_comm],mess);
    awaiting_register = 1;
    my_FM_id = 0;
end

%display('USS_23');

% No actions until register acknowledgment from flight manager
if awaiting_register==1
    message = UR_comms(COMM_RECEIVE,[my_comm,FM_comm],[]);
    if ~isempty(message)
        mess = message.mess;
        switch mess.type
            case M_FM2USS
                if mess.action==M_REGISTER
                    my_FM_id = mess.data;
                end
                awaiting_register = 0;
        end
    end
end

% Reset platforms after flight
if ~isempty(reset_platforms)
%    cur_date = date;
%    cur_time = clock;
%    time_index = UR_time_index(cur_date,cur_time(4),cur_time(5));
%    index = find(reset_platforms(:,1)==time_index);
    index = find(reset_platforms(:,1)==t);
    if ~isempty(index)
        platforms(reset_platforms(index,2)) = 0;
        reset_platforms(index,:) = [];
    end
end

% Look for request acknowledgements from flight manager
res = UR_comms(COMM_RECEIVE,[my_comm,FM_comm],[]);
if ~isempty(res)
    if res.sender==FM_comm
        mess = res.mess;
        if mess.type==M_FM2USS&mess.action==M_REQUEST
            flight_plan = mess.data;
            pf_id = flight_plan(1,7);
            platforms(pf_id) = 1;
            end_time = flight_plan(end,2);
            reset_platforms = [reset_platforms; end_time, pf_id];
        end
    end
end

% Generate requests
if rand<REQUEST_PROB&awaiting_register==0
    req_no = req_no + 1;
%    cur_date = date;
%    cur_time = clock;
%    time_index = UR_time_index(cur_date,cur_time(4),cur_time(5));
%    t_start = time_index + MIN_ADVANCE_NOTICE + 60*rand*TIME_OFFSET;
%    t_end = t_start + 60*TIME_SPREAD;
    t_start = t + MIN_ADVANCE_NOTICE + 60*rand*TIME_OFFSET;
    t_end = t_start + 60*TIME_SPREAD;
    op_id = operators(randi(NUM_OPERATORS));
    % Get an available platform
    indexes = find(platforms==0);
    if ~isempty(indexes)
        platform_id = indexes(randi(length(indexes)));
        launch_vertex = launch_vertexes(randi(num_launch_vertexes));
        land_vertex = land_vertexes(randi(num_land_vertexes));
        speed = speeds(randi(NUM_SPEEDS));
        mess.type = M_USS2FM;
        mess.code = my_comm;
        mess.action = M_REQUEST;
        mess.data = [USS_ID,speed,op_id,platform_id,launch_vertex,...
            land_vertex,t_start,t_end,req_no];
        UR_comms(COMM_SEND,[my_comm,FM_comm],mess);
    end
end
