function [EventsFUN,EventsValue]=sdeeventsfun(solver,tspan,y0,options)
%SDEEVENTSFUN  Process events function arguments for SDE solvers and functions.
%
%   See also:
%       SDEARGUMENTS, SDEARGUMENTS_PROCESS, SDEOUTPUTFUN, SDERANDFUN, SDEGET,
%       FUNCTION_HANDLE
        
%   Andrew D. Horchler, horchler @ gmail . com, Created 5-2-13
%   Revision: 1.2, 5-2-13


EventsFUN = sdeget(options,'EventsFUN',[],'flag');
if ~isempty(EventsFUN)
    if ~isa(EventsFUN,'function_handle')
        error('SDETools:sdeeventsfun:EventsFUNNotAFunctionHandle',...
              'EventsFUN, if specified, must be a function handle.  See %s.',...
              solver);
    end
    
    % Check output of EventsFUN at initial condition and save value
    try
        [EventsValue,isterminal,direction] = EventsFUN(tspan(1),y0);
    catch err
        switch err.identifier
            case 'MATLAB:TooManyInputs'
                error('SDETools:sdeeventsfun:EventsFUNTooFewInputs',...
                      'EventsFUN must have at least two inputs.  See %s.',...
                      solver);
            case 'MATLAB:TooManyOutputs'
                error('SDETools:sdeeventsfun:EventsFUNNoOutput',...
                     ['The output of EventsFUN was not specified. EventsFUN '...
                      'must return three non-empty vectors.  See %s.'],solver);
            case 'MATLAB:unassignedOutputs'
                error('SDETools:sdeeventsfun:EventsFUNUnassignedOutput',...
                     ['The first output of EventsFUN was not assigned.  '...
                      'See %s.'],solver);
            case 'MATLAB:minrhs'
                error('SDETools:sdeeventsfun:EventsFUNTooManyInputs',...
                     ['EventsFUN requires one or more input arguments '...
                      '(parameters) that were not supplied.  See %s.'],solver);
            otherwise
                rethrow(err);
        end
    end
    if ~isvector(EventsValue) || isempty(EventsValue)...
            || ~all(isfinite(EventsValue)) 
        error('SDETools:sdeeventsfun:InvalidEventsValue',...
             ['The first output of EventsFUN, ''Value'', must be a '...
              'non-empty finite vector.  See %s.'],solver);
    end
    if ~isvector(isterminal)...
            || ~any(length(isterminal) == [length(EventsValue) 1])
        error('SDETools:sdeeventsfun:EventsIsterminalDimensionMismatch',...
             ['The second output of EventsFUN, ''IsTerminal'', must be a '...
              'scalar or a vector the same length as the first output.  '...
              'See %s.'],solver);
    end
    if ~all(isterminal == 0 | isterminal == 1)
        error('SDETools:sdeeventsfun:InvalidEventsIsterminal',...
             ['The elements of the second output of EventsFUN, '...
              '''IsTerminal'', must be equal to 0 (false) or 1 (true).  '...
              'See %s.'],solver);
    end
    if ~isempty(direction)
        if ~isvector(direction)...
                || ~any(length(direction) == [length(EventsValue) 1])
            error('SDETools:sdeeventsfun:EventsDirectionDimensionMismatch',...
                 ['If the third output of EventsFUN, ''Direction'', is not '...
                  'specified as the empty matrix, [], it must be a scalar '...
                  'or a vector the same length as first output.  See %s.'],...
                  solver);
        end
        if ~all(direction == 0 | direction == 1 | direction == -1)
            error('SDETools:sdeeventsfun:InvalidEventsDirection',....
                 ['The third output of EventsFUN, ''Direction'', must be '...
                  'equal to 0 (default), 1, or -1.  See %s.'],solver);
        end
    end
else
    EventsValue = [];
end