% Input:
% Ijump: matrix (containing row-wise a list of sets which intersect the guard of a transition)
% qq: 2xn vector (encoding of transitions)
% tqq: 1xn vector (specifying which transitions are spontaneous)
% timestep: double
% N: integer
% elapsedtime: double
% fixpoint: uint
% qa = uint (id of current location)
% transitionqq: vector (containing the ids of the transitions to associate
% the corrsepondent entry from tqq with)

% Output:
% transition: uint
% setindex: uint
% spontaneTransition: bool

% Description:
% Checks if instead of a transition from Ijump(if available) a spontaneuous
% (previous) transition can be taken (in this case
% spontaneTransition = 1). Setindex describes the set with the "intersection"
% and transition the number of the transition (from qq)

function [transition,setindex,spontaneTransition] = checkForSpontaneousTransitions(Ijump,qq,tqq,timestep,N,elapsedtime,fixpoint,qa,transitionqq)
fprintf('- check for spontaneous transitions\n');

transition = -1;
setindex = -1;
spontaneTransition = 0;

N = double(N);

if ~isempty(tqq)    % there are spontaneous transitions
    transition =[];
    t_index = find(qq(1,:) == qa);
    [~,t_index] = ismember(t_index,transitionqq);
    for i=1:length(t_index)
        if t_index(i) ~= 0
            if tqq(t_index(i)) >0 || tqq(t_index(i)) == -1
                transition = [transition transitionqq(t_index(i))];
            end;
        end;
    end;
    
    valuestocheck = cell(size(transition,2),1);
    for i=1:length(transition)
        
        mapping = transition(i) == transitionqq;
        
        valuestocheck{i} = tqq(mapping);
    end;   
        
    %transition = transitionqq(candidates);
        
    if ~isempty(transition)
        for t=1:1:N

            if t == fixpoint
                candidates2 = tqq==-1;
                transition2 = find(qq(1,transitionqq(1,candidates2))==qa);
                if ~isempty(transition2)
                    setindex = fixpoint;
                    spontaneTransition = 1;
                    fprintf('  Take spontaneous transition %d triggered by fixpoint detection at %f !\n',transition2,(setindex-1)*timestep);
                    if length(transition2) > 1
                        error('There are more then one fixpoint triggered spontaneous transitions starting in one location!');
                    end;
                    transition = transition2;
                    return;
                else
                    fprintf('   There might be a modeling fault regarding spontaneous fixpoint triggered transitions! \n\n');
                end;
            end;

            for i=1:1:length(valuestocheck)

                    %check if Ijump can be taken first
                    if ~isempty(Ijump)

                        for j=1:size(Ijump,1)

                            if (Ijump(j,1)-1)*timestep + elapsedtime == valuestocheck{i}
                                if i == length(valuestocheck)  
                                    return;  %no further check required
                                end;
                            else
                                if t*timestep + elapsedtime >= valuestocheck{i} && t*timestep + elapsedtime < valuestocheck{i}+timestep
                                    transition = transition(i);
                                    setindex = t;
                                    spontaneTransition = 1;
                                    fprintf('  Take spontaneous transition: %d \nsetindex: %f\n\n',transition,setindex);
                                    return;
                                end;
                            end;
                        end;


                    else
                        if t*timestep + elapsedtime >= valuestocheck{i} && t*timestep + elapsedtime < valuestocheck{i}+timestep
                            transition = transition(i);
                            setindex = t;
                            spontaneTransition = 1;
                            fprintf('  Take spontaneous transition: %d \nsetindex: %f\n\n',transition,setindex);
                            return;
                        end;
                    end;
            end; 
        end;
    end;
end;