% TRI_CY_Evaluate_Patient - evaluate whether k patient can insert before
% ptr patient, return 1 if it can. Otherwise, return 0.
% Call:
%     TRI_CY_Evaluate_Patient(insert_patient, scheduled_patient, TB, 0)
% 
function result = TRI_CY_Evaluate_Patient(insert_patient, ...
    scheduled_patient, slots, model, combine_time)

scheduled_patient_start = scheduled_patient.start;
scheduled_patient_state = scheduled_patient.state;
scheduled_patient_priority = scheduled_patient.pri;
scheduled_patient_arrive = scheduled_patient.arrive;
scheduled_patient_slot = slots(scheduled_patient_priority, :);
scheduled_patient_duration = scheduled_patient.duration;

insert_patient_arrive = insert_patient.arrive;
insert_patient_duration = insert_patient.duration;
insert_patient_priority = insert_patient.pri;


if scheduled_patient_start < insert_patient_arrive
    result = 0;
    return
end


if scheduled_patient_state == 1
    
    % min_delay method
    if model == 0
        result = 1;
    % fairness method
    elseif model == 1
        result = 0;
    %combine method
    elseif model == 2
        delay_time = scheduled_patient_start + insert_patient_duration ...
            - scheduled_patient_arrive;
        if delay_time < combine_time
            result = 1;
        else
            result = 0;
        end
    end
    
    return
end

if scheduled_patient_start + insert_patient_duration > ...
    scheduled_patient_arrive + scheduled_patient_slot(2)
    result = 0;
    return
else
    if insert_patient_priority < scheduled_patient_priority
        result = 1;
        return
    %{
    elseif insert_patient_priority == scheduled_patient_priority
        if insert_patient_duration < scheduled_patient_duration
            result = 1;
            return
        end
    %}
    end
    result = 0;
end