function dop=dopGraph(dop,varargin)
% dopOSCCI Mark II: dopGraph~ 11-10-2010 (last edit)
%
% make graphs, display/save
%
% graphs include:
% 1. raw data (left, right, and event markers)
% 2. normalised data (left, right, raw events, selected events, activation
%       rejection levels) ~ base and epoch details
% 3. Individual Average (left, right, & dif - 2 subplots)
% 4. Individual Epochs (left, right, & dif - 2 subplots)
% 5. Individual Behavioural (left, right, & dif = 2 subplots)
% 6. Overall Average (left, right, & dif = 2 subplots) all available data
%
try
    if sum(dop.sum.skip==[0 2]) && ~dop.sum.quit && sum(sum(dop.def.graphs))
        %% list of graph abbreviations
        dop.dg.graphAbb=dop.def.graphAbb;
        dop.dg.graphAbb(end)=[]; % in regular graphing, don't do overall
        % if extra input, then list is defined by this
        if ~isempty(varargin)
            dop.dg.graphAbb=[];
            for i=1:numel(varargin)
                dop.dg.graphAbb{i}=varargin{i};
            end
        end
        for i=1:length(dop.dg.graphAbb)
            dop.dg.gi=find(strcmp(dop.dg.graphAbb{i},dop.def.graphAbb));% graph i
            if sum(dop.def.graphs(:,dop.dg.gi))
                dop.dg.grabb=dop.dg.graphAbb{i}; % graph abbreviation
                dop.dg.graphName=dop.def.graphNames{dop.dg.gi}; % graph abbreviation
                
                %% session check
                if or(or(~dop.def.sesFileChk,... % not running sessions
                        strcmp(dop.dg.grabb,dop.def.graphAbb{end})),... % asking for overall graphs
                        or(dop.def.sesFileChk && dop.ses.session==dop.ses.nSes+1 && dop.dg.gi>2,... % combined session
                        dop.def.sesFileChk && dop.ses.session<=dop.ses.nSes)) % session but not combined
                    %         dop.dg.h=feval(['dopGraph',num2str(i),dop.dg.grabb],...
                    %             dop.tmp.rawData,'sampleRate',dop.def.downHertz,...
                    %             'indName',dop.sum.fileTxt,'graphName',dop.dg.grap
                    %             hName);
                    switch dop.dg.grabb
                        %% Raw
                        case dop.def.graphAbb{1} % 'Raw'
                            [dop.dg.h dop.dg.names]=dopGraph1Raw(dop.raw.data,...%dop.tmp.rawData,...
                                'sampleRate',dop.data.rawInfo.sampleRate,...%dop.def.downHertz,...
                                'indName',dop.sum.fileTxt,...
                                'graphAbb',dop.dg.grabb,...
                                'graphName',dop.dg.graphName,...
                                'fileType',dop.def.fileType,...
                                'info',dop.data.rawInfo);
                            %                 dop.dg.h=feval(['dopGraph',num2str(i),dop.dg.grabb],...
                            %                     dop.tmp.rawData,'sampleRate',dop.def.downHertz,...
                            %                     'indName',dop.sum.fileTxt,'graphName',dop.dg.graphName);
                            %% Norm
                        case dop.def.graphAbb{2} % 'Norm'
                            [dop.dg.h dop.dg.names]=dopGraph2Norm(dop.inData(:,1:2,:),...
                                'mk',dop.mk,...
                                'sampleRate',dop.def.downHertz,...
                                'indName',dop.sum.fileTxt,...
                                'graphAbb',dop.dg.grabb,...
                                'graphName',dop.dg.graphName,...
                                'actRej',dop.def.actRej,...
                                'epoch',dop.def.epoch,...
                                'base',dop.def.base,...
                                'evType',dop.def.evType);
                            %% individual
                        case dop.def.graphAbb{3} % 'ind'
                            if ~dop.sum.skip
                                dop.tmp.evs=dop.stack.evNames;%fieldnames(dop.stack);
                                dop.tmp.h=0; % tmp handle
                                %                                 dop.dg.epSel=dop.def.saveEpochSelectionLabels{find(dop.def.saveEpochSelectionGraph(1:end-1),1,'first')};
                                %                                 dop.dg.scrnCol=find(strcmp(dop.dg.epSel,dop.scrn.labels)); % scrn column is slightly fluid in terms of position
                                %                                 dop.dg.indName=dop.sum.fileTxt;
                                for jj=1:numel(dop.def.saveEpochSelectionGraph)
                                    if dop.def.saveEpochSelectionGraph(jj);  dop.dg.epSel=jj;
                                        dop.tmp.epSelLabel=dop.def.saveEpochSelectionLabels{dop.dg.epSel};
                                        if ~strcmp(dop.tmp.epSelLabel,'bal') || ...
                                                isfield(dop,'ses') && isfield(dop.ses,'session') && dop.ses.session==dop.ses.nSes+1
                                            dop.dg.scrnCol=find(strcmp(dop.tmp.epSelLabel,dop.scrn.labels)); % scrn column is slightly fluid in terms of position
                                            dop.dg.indName=[dop.sum.fileTxt,'_',dop.tmp.epSelLabel];
                                            for j=1:length(dop.tmp.evs)
                                                if dop.def.evType(j)
                                                    dop.tmp.te=dop.tmp.evs{j}; % temp event
                                                    dop.dg.evScrn=logical(dop.scrn.(dop.tmp.te)(:,dop.dg.scrnCol));
                                                    
                                                    dop.dg.scrn=dop.dg.evScrn;
                                                    dop.dg.indNamePlus=dop.dg.indName;
                                                    dop.dg.li=[];%dop.li.(dop.tmp.te).all.all;
                                                    
                                                    
                                                    %                                         if isfield(dop.li.(dop.tmp.te),'scrn');
                                                    %                                             dop.dg.li=dop.li.(dop.tmp.te).scrn.all;
                                                    %                                         elseif isfield(dop.li.(dop.tmp.te),'all');
                                                    %                                             dop.dg.li=dop.li.(dop.tmp.te).all.all;
                                                    %                                         end
                                                    if isfield(dop.li.(dop.tmp.te),dop.tmp.epSelLabel);
                                                        dop.dg.li=dop.li.(dop.tmp.te).(dop.tmp.epSelLabel).all;
                                                    end
                                                    dop.dg.data=dop.stack.(dop.tmp.te)(dop.dg.scrn,:,:);
                                                    dop.dg.baseData=[];
                                                    if dop.def.evType(j)==1
                                                        dop.dg.mn=dop.def.evNames{dop.def.evMatch(j)}; % base name
                                                        dop.dg.baseData=dop.stack.(dop.dg.mn)(dop.dg.scrn,:,:);
                                                    end
                                                    dop=lateralityGraph(dop,j);
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                            %% epoch
                        case dop.def.graphAbb{4} % 'epoch'
                            if ~dop.sum.skip
                                dop.tmp.evs=dop.stack.evNames;%fieldnames(dop.stack);
                                dop.tmp.h=0; % tmp handle
                                %                                 dop.dg.epSel=dop.def.saveEpochSelectionLabels{find(dop.def.saveEpochSelectionGraph(1:end-1),1,'first')};
                                %                                 dop.dg.scrnCol=find(strcmp(dop.dg.epSel,dop.scrn.labels)); % scrn column is slightly fluid
                                
                                dop.dg.indName=dop.sum.fileTxt;
                                dop.dg.liVars={'LImean','LIpeakSample','LIpeakSec'};
                                for j=1:length(dop.tmp.evs)
                                    dop.tmp.te=dop.tmp.evs{j}; % temp event
                                    if dop.def.evType(j)
                                        for jj=1:size(dop.stack.(dop.tmp.te),1)
                                            %                                     dop.dg.evScrn=logical(dop.scrn.(dop.tmp.te)(:,dop.dg.scrnCol));
                                            fprintf('\t\t%s\n',['Epoch ',num2str(jj)]);
                                            dop.dg.scrn=zeros(1,size(dop.stack.(dop.tmp.te),1));
                                            dop.dg.scrn(jj)=1;dop.dg.scrn=logical(dop.dg.scrn);
                                            dop.dg.indNamePlus=[dop.dg.indName,' Epoch ',num2str(jj)];
                                            for jjj=1:numel(dop.dg.liVars)
                                                dop.dg.li.(dop.dg.liVars{jjj})=...
                                                    dop.li.(dop.tmp.te).all.epoch.(dop.dg.liVars{jjj})(jj);
                                            end
                                            dop.dg.data=dop.stack.(dop.tmp.te)(dop.dg.scrn,:,:);
                                            dop.dg.baseData=[];
                                            if dop.def.evType(j)==1
                                                dop.dg.mn=dop.def.evNames{dop.def.evMatch(j)}; % base name
                                                dop.dg.baseData=dop.stack.(dop.dg.mn)(dop.dg.scrn,:,:);
                                            end
                                            dop=lateralityGraph(dop,j);
                                        end
                                    end
                                end
                            end
                            %% behvioural
                        case dop.def.graphAbb{5} % 'beh'
                            if ~dop.sum.skip
                                if dop.def.behFileChk
                                    dop.tmp.evs=dop.stack.evNames;%fieldnames(dop.stack);
                                    dop.tmp.h=0; % tmp handle
                                    %                                     dop.dg.epSel=dop.def.saveEpochSelectionLabels{find(dop.def.saveEpochSelectionGraph(1:end-1),1,'first')};
                                    %                                     dop.dg.scrnCol=find(strcmp(dop.dg.epSel,dop.scrn.labels)); % scrn column is slightly fluid
                                    %                                     dop.dg.indName=dop.sum.fileTxt;
                                    for jjjj=1:numel(dop.def.saveEpochSelectionGraph)
                                        if dop.def.saveEpochSelectionGraph(jjjj);  dop.dg.epSel=jjjj;
                                            dop.tmp.epSelLabel=dop.def.saveEpochSelectionLabels{dop.dg.epSel};
                                            if ~strcmp(dop.tmp.epSelLabel,'bal') || ...
                                                    isfield(dop,'ses') && isfield(dop.ses,'session') && dop.ses.session==dop.ses.nSes+1
                                                dop.dg.scrnCol=find(strcmp(dop.tmp.epSelLabel,dop.scrn.labels)); % scrn column is slightly fluid in terms of position
                                                dop.dg.indName=[dop.sum.fileTxt,'_',dop.tmp.epSelLabel];
                                                for j=1:length(dop.tmp.evs)
                                                    if dop.def.evType(j)
                                                        dop.tmp.te=dop.tmp.evs{j}; % temp event
                                                        dop.dg.evScrn=logical(dop.scrn.(dop.tmp.te)(:,dop.dg.scrnCol));
                                                        
                                                        for jj=1:numel(dop.beh.behNames); dop.dg.mn=dop.beh.behNames{jj}; % behaviour name
                                                            for jjj=1:numel(dop.beh.levels.(dop.dg.mn)); dop.dg.bl=dop.beh.levels.(dop.dg.mn)(jjj); % behaviour level
                                                                dop.dg.scrn=and(dop.dg.evScrn,dop.beh.filter.(dop.dg.mn)(:,jjj));
                                                                dop.dg.indNamePlus=[dop.dg.indName,' ',dop.dg.mn,' ',...
                                                                    num2str(dop.dg.bl)];
                                                                dop.dg.li=[];%dop.beh.li.(dop.dg.mn)(jjj).(dop.tmp.te).all.all;
                                                                if isfield(dop.beh.li.(dop.dg.mn)(jjj).(dop.tmp.te),dop.tmp.epSelLabel);
                                                                    dop.dg.li=dop.beh.li.(dop.dg.mn)(jjj).(dop.tmp.te).(dop.tmp.epSelLabel).all;
                                                                    %                                                     elseif isfield(dop.beh.li.(dop.dg.mn)(jjj).(dop.tmp.te),'all');
                                                                    %                                                         dop.dg.li=dop.beh.li.(dop.dg.mn)(jjj).(dop.tmp.te).all.all;
                                                                end
                                                                if sum(dop.dg.scrn)
                                                                    dop.dg.data=dop.stack.(dop.tmp.te)(dop.dg.scrn,:,:);
                                                                    dop.dg.baseData=[];
                                                                    if dop.def.evType(j)==1
                                                                        dop.dg.mn=dop.def.evNames{dop.def.evMatch(j)}; % base name
                                                                        dop.dg.baseData=dop.stack.(dop.dg.mn)(dop.dg.scrn,:,:);
                                                                    end
                                                                    dop=lateralityGraph(dop,j);
                                                                else
                                                                    fprintf('\t%s\n\t\t%s\n',...
                                                                        ['No epochs available for ',dop.dg.indNamePlus],...
                                                                        'Therefore no graph can be made');
                                                                end
                                                            end
                                                        end
                                                    end
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                            %% overall
                        case dop.def.graphAbb{6} % 'overall'
                            if ~dop.sum.skip
                                fprintf('\t%s\n',dop.dg.grabb);
                                if isfield(dop,'grd') && sum(dop.grd.saveGraphDataChs(1:3))==3
                                    dop.tmp.h=0;
                                    for ii=1:dop.grd.n
                                        if isfield(dop.grd.info{ii},'evName')
                                            dop.tmp.te=dop.grd.info{ii}.evName; % temp event name
                                            dop.tmp.ej=find(strcmp(dop.tmp.te,dop.def.evNames)); % event j index
                                            if dop.def.evType(dop.tmp.ej)
                                                dop.tmp.et=dop.def.evType(dop.tmp.ej); % event type
                                                dop.dg.indNamePlus=dop.grd.info{ii}.graphName;% get the file name for labelling
                                                dop.dg.data=dop.grd.data(ii).LR;
                                                dop.dg.li=[];
                                                dop.tmp.li=['li',num2str(ii)];
                                                if isfield(dop.grd,dop.tmp.li) %&& size(dop.grd.li)<=ii
                                                    dop.dg.li=dop.grd.(dop.tmp.li).all;
                                                end
                                                dop.dg.baseData=[];
                                                % find base data
                                                if dop.def.evType(dop.tmp.ej)==1
                                                    %                                                 dop.dg.mn=dop.def.evNames{dop.def.evMatch(dop.tmp.ej)}; % base name
                                                    dop.dg.mn=dop.def.evNames{dop.def.evMatch(dop.tmp.ej)}; % match name
                                                    dop.dg.mnum=0;
                                                    for j=1:dop.grd.n % for each data file
                                                        if j~=ii && strcmp(dop.grd.info{j}.evName,dop.dg.mn)...
                                                                && strcmp(dop.grd.info{j}.graphName,dop.grd.info{ii}.graphName)
                                                            dop.dg.mnum=j;
                                                            break
                                                        end
                                                    end
                                                    if dop.dg.mnum
                                                        %                                                     dop.tmp.evType=dop.def.evType(dop.tmp.evNum);
                                                        dop.dg.baseData=dop.grd.data(dop.dg.mnum).LR(:,:,:); %dop.filter.(dop.dg.mn).epoch - should be the whole lot
                                                        %                                                     dop.grd.(dg.tmp.ind)=dopDataIndexCalc(dop.grd.data(i).LR,... % data
                                                        %                                                         dop.filter.(dop.tmp.en),... filtersv'screen',dis.scrn,...
                                                        %                                                         'sampleRate',dop.def.downHertz,...
                                                        %                                                         'filterZero',find(dop.filter.(dop.tmp.en).zeroPt,1,'first'),...);
                                                        %                                                         'evType',dop.tmp.evType,'baseLRdata',...
                                                        %                                                         dop.grd.data(dop.dg.mnum).LR(:,dop.filter.(dop.dg.mn).base,:)); % base event data with base filter
                                                    else
                                                        fprintf('\t%s %s %s\n\t%s\n','No match found for:',...
                                                            dop.def.evNames{dop.tmp.ej},dop.grd.info{dop.tmp.ej}.graphName,...
                                                            dop.grd.fileList{dop.tmp.ej});
                                                    end
                                                end
                                                if dop.def.evType(dop.tmp.ej)==2 || ...
                                                        dop.def.evType(dop.tmp.ej)==1 && ~isempty(dop.dg.baseData)
                                                    dop=lateralityGraph(dop,dop.tmp.ej);
                                                end
                                            end
                                            %                                                 switch dop.tmp.et
                                            %                                                     case 2
                                            %
                                            %
                                            %                                                     case 1
                                            %                                                         if isfield(dop.grd,dop.tmp.li) %&& size(dop.grd.li)<=ii
                                            %                                                             dop.dg.li=dop.grd.(dop.tmp.li).all;
                                            %                                                         end
                                            %
                                            %                                                         dop=lateralityGraph(dop,dop.tmp.ej);
                                            %                                                 end
                                        else
                                            
                                        end
                                        
                                    end
                                else
                                    fprint('\t%s\n\t\t%s\n\t\t%s\n',...
                                        'Need to run following functions before dopGraph(dop,''Overall'')',...
                                        ' - dopGraphDataRead',' - dopGraphDataFileInfo');
                                end
                            end
                    end
                    if isfield(dop.dg,'h')
                        %% do we want to save it?
                        if dop.def.graphs(1,dop.dg.gi)
                            %                 for j=1:length(dop.dg.names)
                            fprintf('\t%s\n',['Saving : ',dop.dg.graphName]);
                            dop=dopGraphSave(dop);
                            %                 end
                        end
                        
                        %% do we want to see it?
                        if dop.def.graphs(2,dop.dg.gi)
                            fprintf('\t%s\n','Making visible:')
                            for j=1:length(dop.dg.h)
                                set(dop.dg.h(j),'Visible','On');%,'CloseRequestFcn',@closeFig);
                                %                     set(gl.h(j),'CloseRequestFcn',@closeFig);
                            end
                            try
                                fprintf('\t%s\n','Waiting for button press to close...');
                                waitforbuttonpress;
                            catch err
                                dop.tmp.err=err;
                                fprintf('\t%s\n','> figure deleted/closed: continuing ');
                            end
                            %                 uiwait(dop.dg.h(1));
                            %                 while any(ishghandle(dop.dg.h(1),'figure'))
                            %                 end
                            %                 waitforbuttonpress;
                            %                 pause; % or close...
                        end
                        
                        
                        %% close figure/s
                        for j=1:length(dop.dg.h)
                            if any(ishghandle(dop.dg.h(j),'figure'))
                                close(dop.dg.h(j));
                            end
                        end
                        dop.dg=rmfield(dop.dg,{'h','names'});
                    end
                end
            end
        end
        
        %         dop=rmfield(dop,'dg');
    end
catch err
    %% catch dopOSCCI error
    %
    save(dopOSCCIdebug(mfilename));rethrow(err);
    %     dopOSCCIcrash(err,dop,mfilename);
end
end
%% ------------------------------------------------------------------------
%% embedded functions
%% lateralityGraph
function dop=lateralityGraph(dop,j)
switch dop.def.evType(j)
    case 1 % stim only - make 2-part graph
        %         dop.tmp.bn=dop.def.evNames{dop.def.evMatch(j)}; % base name
        %         dop.tmp.bd=dop.stack.(dop.tmp.bn)(dop.dg.scrn,:,:);
        dop.tmp.h=dop.tmp.h+1;
        [dop.dg.h(dop.tmp.h) dop.dg.names(dop.tmp.h)]=dopGraph3laterality(...
            'data',dop.dg.data,...dop.stack.(dop.tmp.te)(dop.dg.scrn,:,:),...
            'evName',{dop.tmp.te},...
            'sampleRate',dop.def.downHertz,...
            'indName',dop.dg.indNamePlus,...%dop.sum.fileTxt,...
            'graphAbb',dop.dg.grabb,...
            'graphName',dop.dg.graphName,...
            'epoch',dop.def.epoch([dop.def.evMatch(j) j],:),...
            'baseData',dop.dg.baseData,...dop.stack.(dop.tmp.bn)(dop.dg.scrn,:,:),... % use same screen
            'baseName',{dop.dg.mn},...
            'base',dop.def.base(dop.def.evMatch(j),:,:),... % use match base
            'poi',dop.def.poi(j,:),...
            'zeroPt',[dop.filter.zeroPt.(dop.dg.mn) dop.filter.zeroPt.(dop.tmp.te)],...
            'li',dop.dg.li,...%dop.li.(dop.tmp.te),...
            'actWin',dop.def.actWin(j));%,'test');
        
    case 2 % stim & base
        dop.tmp.h=dop.tmp.h+1;
        [dop.dg.h(dop.tmp.h) dop.dg.names(dop.tmp.h)]=dopGraph3laterality(...
            'data',dop.dg.data,...dop.stack.(dop.tmp.te)(dop.dg.scrn,:,:),...
            'evName',{dop.tmp.te},...
            'sampleRate',dop.def.downHertz,...
            'indName',dop.dg.indNamePlus,...dop.sum.fileTxt,...
            'graphAbb',dop.dg.grabb,...
            'graphName',dop.dg.graphName,...
            'epoch',dop.def.epoch(j,:),...
            'base',dop.def.base(j,:),'poi',dop.def.poi(j,:),...
            'zeroPt',dop.filter.zeroPt.(dop.tmp.te),...
            'li',dop.dg.li,...dop.li.(dop.tmp.te),...
            'actWin',dop.def.actWin(j,:)); %'test'
end
%                     [dop.dg.h dop.dg.names]=dopGraph3lateralityStack(...
%                         'stack',dop.stack,...
%                         'sampleRate',dop.def.downHertz,...
%                         'indName',dop.sum.fileTxt,...
%                         'graphName',dop.dg.graphName,...
%                         'epoch',dop.def.epoch,...
%                         'base',dop.def.base,'poi',dop.def.poi,...
%                         'zeroPt',dop.filter.zeroPt,'li',dop.li,...
%                         'actWin',dop.def.actW
%                         in);
end



