classdef stopSignHandler < handle
    properties
        scene
        stop_duration_rule
    end
    
    methods
        function obj = stopSignHandler(scene, stop_duration_rule)
            obj.scene = scene;
            obj.stop_duration_rule = stop_duration_rule;
        end
        function [propositions, ap_names, time_already_in_stop] = traj2Propositions(obj, ts, xs, ~, time_already_in_stop_in)
            propositions = zeros(size(xs,1),2);
            ap_names = ['s', 'a'];
            stop_sign_init_time = inf;
            
            time_already_in_stop = 0;
            if nargin >= 5
                time_already_in_stop = time_already_in_stop_in;
            end
%             disp('blah2')
%             disp(time_already_in_stop )
            
            is_a = false;
            for i = 1:size(xs,1)
                is_s = obj.scene.isCarFrontInStopRegion(xs(i,:));
                
                if is_s
                    % in stop region
%                     disp('blah3')
%                     disp([ts(i), stop_sign_init_time ])
                    if stop_sign_init_time == inf 
                        % first time in stop region
                        stop_sign_init_time = ts(i) - time_already_in_stop;
                        is_a = false;
                        if time_already_in_stop > obj.stop_duration_rule
                            is_a = true;
                        end
                    elseif ts(i) - stop_sign_init_time > obj.stop_duration_rule
                        
                        % been in stop region long enough to leave
                        is_a = true;
                    end
                    time_already_in_stop = ts(i) - stop_sign_init_time;
                else
                    stop_sign_init_time = inf;
                    time_already_in_stop = 0;
                    is_a = false;
                end
                
                
                % if not in stop region and acceptable to go, no longer
                % need is_a = true. 
                % FIXME: Does this need to be set after setting 
                % propositions(i,:)?
%                 if ~is_s && is_a
%                     is_a = false;
%                     stop_sign_init_time = inf;
%                 end
                
                propositions(i,:) = [is_s, is_a];
            end
%             disp(propositions)
        end
    end
    
end