function def=dopOSCCIeventCheck(def,varargin)
% dopOSCCI Mark II: dopOSCCIeventCheck ~ 3-Jan-2011 (last edit)
% >> dgEventCheck (from dopOSCCIgui)
% slightly tricky function which checks and corrects the relationship
% between event timing:
% - separation
% - epoch
% - base
% - poi
%
% Rules:
% upper epoch <= separation
%
% lower base & lower poi >= lower epoch
%
% upper base & upper poi <= upper epoch
%
% lower epoch, base, & poi < upper epoch, base, & poi
%
% > adjustments are made by setting the values to the available limits
% (e.g., separation for upper epoch or epoch limits for base & poi)
% if epoch, base, & poi within comparisons need adjusting, upper value is
% set to lower + 5.  If this places upper outside of separation, then lower
% value is reduced by 5...until we have good numbers.
%
% dropped: upper base <= lower poi (might not be essential) - especially if you
% want to have base after poi...
%
% function dg=dgEventCheck(dg)
% which event number are we refering to?
% dg.tmp.ud=get(dg.tmp.h,'UserData');
% dg.tmp.k=dg.tmp.ud(1);
% k=dg.tmp.k;
try
    if isempty(varargin)
        k=1;
    elseif isstruct(varargin{1})
        dg=varargin{1};
        % which event number are we refering to?
        dg.tmp.ud=get(dg.tmp.h,'UserData');
        dg.tmp.k=dg.tmp.ud(1);
        k=dg.tmp.k;
    else
        k=varargin{1};
    end
    if ~isfield(def,'CheckRep')
        def.CheckRep=0;
    end
    evChk.less={... % less than
        'epoch2','sep1';
        'base2','epoch2';
        'poi1','epoch2';
        'poi2','epoch2';
        }; %'base2','poi1'
    evChk.greater={... % greater than
        'base1','epoch1';
        'poi1','epoch1'};
    evChk.int={... % internally correct (greater than)
        'epoch2','epoch1';
        'base2','base1';
        'poi2','poi1'};
    
    evChk.pos={'Lower','Upper'};
    evChk.hd=[1 3]; % handle number
    evChk.intIncrease=3;%[7 5]; % interval increase, if upper less than lower, increase by x seconds
    evChk.intAdj=0; % if interval adjustment is made, rerun dbEventCheck, to make sure value hasn't been increased outside limits
    
    evChk.fields={'less','greater','int'};
    evChk.symbol={'>','<','<='}; % if this is the case, problem
    for i=1:numel(evChk.fields);
        
        evChk.fn=evChk.fields{i}; % field name
        evChk.adj=evChk.fields{i};
        if strcmp(evChk.adj,'int');
            evChk.adj='greater'; % same as less, 'less than'
        end
        fprintf('\t%s\n',evChk.fn);
        for j=1:size(evChk.(evChk.fn),1)
            if ~and(def.evType(k)==0,... % base only - don't examine poi
                    or(~isempty(strfind(evChk.(evChk.fn){j,1},'poi')),...
                    ~isempty(strfind(evChk.(evChk.fn){j,2},'poi')))) && ...
                    ~and(def.evType(k)==1,... % poi only - don't examine base
                    and(~isempty(strfind(evChk.(evChk.fn){j,1},'base')),...
                    ~isempty(strfind(evChk.(evChk.fn){j,2},'base'))))
                % create n1, n1num, n2 & n2num variables
                % where n = name
                for jj=1:2
                    evChk.(['n',num2str(jj)])=evChk.(evChk.fn){j,jj}; % name 1
                    evChk.(['n',num2str(jj),'num'])=str2double(evChk.(['n',num2str(jj)])(end));
                    evChk.(['n',num2str(jj)])=evChk.(['n',num2str(jj)])(1:end-1); % remove the number from end
                end
                fprintf('%s\n',['Checking ',evChk.n1,' vs ',evChk.n2]);
                if eval([num2str(def.(evChk.n1)(k,evChk.n1num)),evChk.symbol{i},num2str(def.(evChk.n2)(k,evChk.n2num))])
                    
                    % second value description
                    evChk.n2descrip=[evChk.pos{evChk.n2num},' '];
                    if strcmp(evChk.n2,'sep')
                        evChk.n2descrip='';
                    end
                    evChk.newValue=def.(evChk.n2)(k,evChk.n2num);
                    if strcmp(evChk.fn,'int')
                        % set lower to upper - adj
                        %                     def.(evChk.n1)(k,evChk.n1num)=def.(evChk.n2)(k,evChk.n2num)-evChk.intIncrease;
                        evChk.newValue=def.(evChk.n2)(k,evChk.n2num)-evChk.intIncrease;
                        %                     evChk.newValue=evChk.newValue-evChk.intIncrease;%(evChk.n2num);
                        evChk.intAdj=1;
                        if evChk.newValue>def.sep
                            while evChk.newValue>def.sep % reduce lower
                                def.(evChk.n1)(k,evChk.n1num)=def.(evChk.n2)(k,evChk.n2num)-evChk.intIncrease;%(evChk.n2num);
                                evChk.newValue=def.(evChk.n1)(k,evChk.n1num);
                                evChk.newValue=evChk.newValue-evChk.intIncrease;%(evChk.n2num);
                            end
                            if ~isempty(varargin) && isstruct(varargin{1})
                                set(dg.event.(evChk.n2).edit.h(k,evChk.hd(evChk.n2num)),...
                                    'string',num2str(def.(evChk.n2)(k,evChk.n2num)));
                            end
                        end
                        evChk.warn=sprintf('%s\n%s\n',...
                            [evChk.pos{evChk.n1num},' ',evChk.n1,' (',...
                            num2str(def.(evChk.n1)(k,evChk.n1num)),') must be ',...
                            evChk.adj,' than or equal to ',...
                            evChk.n2descrip,evChk.n2,' value (',...
                            num2str(def.(evChk.n2)(k,evChk.n2num)),')'],...
                            ['Setting ',evChk.pos{evChk.n2num},' ',evChk.n2,' to ',...
                            num2str(evChk.newValue)]);
                        disp(evChk.warn);
                        warndlg(evChk.warn,[upper(evChk.n2(1)),evChk.n2(2:end),' Adjustment:'],'modal');
                        if ~isempty(varargin) && isstruct(varargin{1})
                            set(dg.event.(evChk.n2).edit.h(k,evChk.hd(evChk.n2num)),...
                                'String',num2str(evChk.newValue));
                        end
                        def.(evChk.n2)(k,evChk.n2num)=evChk.newValue;
                    else
                        evChk.warn=sprintf('%s\n%s\n',...
                            [evChk.pos{evChk.n1num},' ',evChk.n1,' (',...
                            num2str(def.(evChk.n1)(k,evChk.n1num)),') must be ',...
                            evChk.adj,' than or equal to ',...
                            evChk.n2descrip,evChk.n2,' value (',...
                            num2str(def.(evChk.n2)(k,evChk.n2num)),')'],...
                            ['Setting ',evChk.pos{evChk.n1num},' ',evChk.n1,' to ',...
                            num2str(evChk.newValue)]);
                        disp(evChk.warn);
                        warndlg(evChk.warn,[upper(evChk.n1(1)),evChk.n1(2:end),' Adjustment:'],'modal');
                        if ~isempty(varargin) && isstruct(varargin{1})
                            set(dg.event.(evChk.n1).edit.h(k,evChk.hd(evChk.n1num)),...
                                'String',num2str(evChk.newValue));
                        end
                        def.(evChk.n1)(k,evChk.n1num)=evChk.newValue;
                    end
                end
            end
        end
    end
    
    if evChk.intAdj && def.CheckRep<5
        def.CheckRep=def.CheckRep+1;
        def=dopOSCCIeventCheck(def,k);
    else
        def=rmfield(def,'CheckRep');
    end
    clear k
catch err
    fprintf('\t%s\n','Error...');
    % who = set of available variables as a cell array
    dopOSCCIcrash(err,def,mfilename);
end
end