function ui_main(varargin)

%  Initialize and hide the GUI as it is being constructed.
    frameWidth=450;
    frameHeight=450;
    
    frame = figure('Visible','off','Position',[0,0,frameWidth,frameHeight]);
    movegui(frame,'west'); % get this thing visible on smaller displays.
    set(frame,'Name','SVMCrossVal Decode Performance 4 SPM');
    set(frame,'NumberTitle','off');
    set(frame,'MenuBar','none');
    set(frame,'Resize','on');
    set(frame,'Units','normalized');
    set(frame,'Color','y');

    task = struct;
    
%     studyID = getPreviousStudyID();
    
%     StudyArgs = load_study(studyID);

    model = struct;
    
    model.txtBaseDir = createLabel(frame,[0 0.97 0.8 0.03],'');
    set(model.txtBaseDir,'BackgroundColor','w');
    set(model.txtBaseDir,'ForegroundColor','b');
    
    model.txtStudyID = createLabel(frame,[0.8 0.97 0.2 0.03],'');
    set(model.txtStudyID,'BackgroundColor','w');
    set(model.txtStudyID,'ForegroundColor','r');    

    TASK_HEIGHT = 1-0.13;
    
    % PREPROCESSING
    task.preprocessing   = uipanel(frame,'Title','Preprocessing','Position',[0 0.0 1 TASK_HEIGHT]);
    set(task.preprocessing,'BackgroundColor','w');
    set(task.preprocessing,'Units','normalized');
    
    model = createFirstStepPanel(model,task.preprocessing);
    
    % fill with data
%     model = scanDirs(model);
    
    % CLASSIFICATION
    task.classification = uipanel(frame,'Title','Classification','Position',[0 0.0 1 TASK_HEIGHT]);
    set(task.classification,'BackgroundColor','w');
    model = createSecondStepPanel(model,task.classification);
    
    % PLOT
    task.plot = uipanel(frame,'Title','Plot','Position',[0 0.0 1 TASK_HEIGHT]);
    set(task.plot,'BackgroundColor','w');
    model = createVisualStepPanel(model,task.plot);
    
    % TASK
    task.taskSwitch = uibuttongroup(frame,'Position',[0 1-0.13 1 0.10]);
    % controlls togglebuttons
        set(task.taskSwitch,'BackgroundColor','w');
        set(task.taskSwitch,'Units','normalized');
    
        btnSwitchPreprocessing = uicontrol(task.taskSwitch,'Style','pushbutton',...
            'String','ImageProcessing',...
            'Units','normalized','Position',[0.0 0.0 0.33 1]);
        set(btnSwitchPreprocessing,'Callback',{@cbSwitchTask,'PRE',task}); 
        set(btnSwitchPreprocessing,'Enable','on');

        btnSwitchClassify = uicontrol(task.taskSwitch,'Style','pushbutton',...
            'String','Decode',...
            'Units','normalized','Position',[0.33 0.0 0.33 1]);
        set(btnSwitchClassify,'Callback',{@cbSwitchTask,'CLASSIFY',task}); 
        set(btnSwitchClassify,'Enable','on');

        btnSwitchPlot = uicontrol(task.taskSwitch,'Style','pushbutton',...
            'String','Plot',...
            'Units','normalized','Position',[0.66 0.0 0.33 1]);
        set(btnSwitchPlot,'Callback',{@cbSwitchTask,'PLOT',task}); 
        set(btnSwitchPlot,'Enable','on');


    % menus

    savemenu = uimenu(frame,'Label','Save ...','Enable','on');
        uimenu(savemenu,'Label','Save All','Callback',{@mcb_save,model},'Enable','on');
        
    studySelectMenu = uimenu(frame,'Label','Study ...','Enable','on');
        uimenu(studySelectMenu,'Label','*new Study*','Callback',{@mcb_new_study,studySelectMenu,model});
        fillStudyMenu(studySelectMenu,model);
        
    updateMenu = uimenu(frame,'Label','update ...','Enable','off');
        uimenu(updateMenu,'Label','scan dirs','Callback',{@ssss,studySelectMenu});
        
%     newStudyMenu = uimenu(frame,'Label','new Study','Enable','on');

    load_study([getPreviousStudyID '.mat'],model);
    cbSwitchTask(0,0,'PRE',task);
    set(frame,'Visible','on');

end

%%%%% ui elements
function model = createFirstStepPanel(model,parent)

    main_grid = cell(2,4);
    main_grid{1,1} = [0 0.7 0.4 0.3];
    main_grid{1,2} = [0 0.5 0.5 0.2];
    main_grid{1,3} = [0 0.1 0.5 0.4];
    main_grid{1,4} = [0 0.0 1.0 0.1];
    
    main_grid{2,1} = [0.4 0.7 0.6 0.3];
    main_grid{2,2} = [0.5 0.5 0.5 0.2];
    main_grid{2,3} = [0.5 0.1 0.5 0.4];
    
    %Subjects
    pSubject = uipanel(parent,'Units','normalized','Position',cell2mat(main_grid(1,1)));
    set(pSubject,'Title','Subjects');
    set(pSubject,'BackgroundColor','w');
    
%     subjectNames = listDirNames(getBaseDir(model));
    model.subjectSelector = uicontrol(pSubject,'Style','listbox',...
                    'Min',1, 'Max',3,...
                    'Units','normalized',...
                    'Position',[0 0 1 1]);
    set(model.subjectSelector, 'FontName', 'FixedWidth');
    set(model.subjectSelector,'BackgroundColor','w');

  
    %Classes
    pClasses = uipanel(parent,'Units','normalized','Position',cell2mat(main_grid(1,2)));
    set(pClasses,'Title','Class Definitions');
    set(pClasses,'BackgroundColor','w');
        lClassDef = uicontrol(pClasses,...
            'Style','text',...
            'String',sprintf('<Label>,\t <[Event, Event, ..]>;'),...
            'Units','normalized',...
            'Position',[0 0.8 1 0.2]);
        set(lClassDef,'BackgroundColor','w');
        set(lClassDef,'HorizontalAlignment','left');

        
        model.txtClassDef = uicontrol(pClasses,'Style','edit',...
            'Units','normalized',...
            'Position',[0 0 1 0.8]);
        set(model.txtClassDef,'HorizontalAlignment','left');
        set(model.txtClassDef,'Max',20);
        set(model.txtClassDef,'Min',0);
        set(model.txtClassDef, 'FontName', 'FixedWidth');
        set(model.txtClassDef, 'BackgroundColor', 'w');


    %Timeline
    pPSTH        = uipanel(parent,'Title','PSTH Options','Position',cell2mat(main_grid(2,1)));
    set(pPSTH,'BackgroundColor','w');
        
        grid_h = 0.16;
        grid_w = 0.3;
        
        tl_grid = cell([3 6]);
        tl_grid{2,1} = [0.0 0.83 grid_w grid_h];
        tl_grid{2,1} = [0.4 0.83 grid_w grid_h];
        tl_grid{3,1} = [0.7 0.83 grid_w grid_h];
        
        tl_grid{1,2} = [0.0 0.66 grid_w grid_h];
        tl_grid{2,2} = [0.4 0.66 grid_w grid_h];
        tl_grid{3,2} = [0.7 0.66 grid_w grid_h];

        tl_grid{1,3} = [0.0 0.5 grid_w grid_h];
        tl_grid{2,3} = [0.4 0.5 grid_w grid_h];
        tl_grid{3,3} = [0.7 0.5 grid_w grid_h];
        
        tl_grid{1,4} = [0.0 0.33 grid_w grid_h];
        tl_grid{2,4} = [0.4 0.33 grid_w grid_h];
        tl_grid{3,4} = [0.7 0.33 grid_w grid_h];

        tl_grid{1,5} = [0.0 0.16 grid_w grid_h];
        tl_grid{2,5} = [0.4 0.16 grid_w grid_h];
        tl_grid{3,5} = [0.7 0.16 grid_w grid_h];
        
        tl_grid{1,6} = [0.0 0.0 0.5 grid_h];
        tl_grid{2,6} = [0.5 0.0 grid_w grid_h];
        tl_grid{3,6} = [0.75 0.0 grid_w grid_h];

        lStart      = createLabel(pPSTH, cell2mat(tl_grid(2,1)) ,'Start [sec]');
        lEnd        = createLabel(pPSTH, cell2mat(tl_grid(3,1)) ,'End [sec]');
        lPSTH       = createLabel(pPSTH, cell2mat(tl_grid(1,2)) ,'PSTH Range');
        lBaseline   = createLabel(pPSTH, cell2mat(tl_grid(1,3)) ,'Baseline');
        lTRF        = createLabel(pPSTH, cell2mat(tl_grid(1,5)) ,'TR Factor');

        model.txtPSTHStart         = createTextField(pPSTH,cell2mat(tl_grid(2,2)),'');
        model.txtPSTHEnd           = createTextField(pPSTH,cell2mat(tl_grid(3,2)),'');
        model.txtBaselineStart     = createTextField(pPSTH,cell2mat(tl_grid(2,3)),'');
        model.txtBaselineEnd       = createTextField(pPSTH,cell2mat(tl_grid(3,3)),'');
        model.txtTrFactor          = createTextField(pPSTH,cell2mat(tl_grid(2,5)),'');
        model.labelTR              = createTextField(pPSTH,cell2mat(tl_grid(3,5)),'');
        
        % images
        pImage = uipanel(parent,'Title','Image Options','Position',cell2mat(main_grid(2,2)));
        set(pImage,'BackgroundColor','w');

        createLabel(pImage,[0.0 0.5 1 0.5],'Select Image Base');
        
        model.imageTypeSelection = uicontrol(pImage,'Style','popupmenu',...
        'Units','normalized',...
        'Position',[0.0 0.0 1 0.5]);
        set(model.imageTypeSelection,'BackgroundColor','w');
        
        % coordinate Table
        pVoxel = uipanel(parent,'Title','ROI','Position',cell2mat(main_grid(1,3)));
        set(pVoxel,'BackgroundColor','w');
        lVoxelDef = createLabel(pVoxel, [0 0.9 1 0.1],'<ROI Name> [+ radius (mm)];');
        model.txtVoxelDef = createTextField(pVoxel,[0 0 1 0.9],'');
        set(model.txtVoxelDef,'HorizontalAlignment','left');
        set(model.txtVoxelDef,'Max',20);
        set(model.txtVoxelDef,'Min',0);
        set(model.txtVoxelDef, 'FontName', 'FixedWidth');
        
        %normalizations
        pNorm = uipanel(parent,'Title','Normalization','Position',cell2mat(main_grid(2,3)));
        set(pNorm,'BackgroundColor','w');
        
        createLabel(pNorm,[0 0.75 1 0.25],'psth norm4SVM');
        
        
        norm1Model = {'none','mean','minmax'};

        
        model.selNormPST = uicontrol(pNorm,'Style','popupmenu',...
            'Units','normalized',...
            'Position',[0.0 0.5 1 0.25],...
            'String',norm1Model,...
            'UserData',norm1Model);
         set(model.selNormPST,'BackgroundColor','w');   
        
        model.chkColBias = uicontrol(pNorm,'Style','checkbox',...
            'Units','normalized',...
            'Position',[0.0 0.1 1 0.25],...
            'String','column Bias removal',...
            'Enable','on');
         set(model.chkColBias,'BackgroundColor','w');   
        
        %buttons
        pButtons = uipanel(parent,'Position',cell2mat(main_grid(1,4)));
        set(pButtons,'BackgroundColor','w');
        
        btnRunButton1 = uicontrol(pButtons,'String','run coord-Table',...
            'Units','normalized','Position',[0 0 0.33 1]);
        set(btnRunButton1,'Callback',{@cbRunPreprocessing,model,'COORD'}); % set here, because of model.    
        set(btnRunButton1,'Enable','on');
        
        btnRunButton2 = uicontrol(pButtons,'String',sprintf('load ROI for FBS'),...
            'Units','normalized','Position',[0.33 0 0.33 1]);
        set(btnRunButton2,'Callback',{@cbRunPreprocessing,model,'FBS'}); % set here, because of model.  
        set(btnRunButton2,'Enable','on');
        
        btnRunButton3 = uicontrol(pButtons,'String','run ROI-Image processing',...
            'Units','normalized','Position',[0.66 0 0.33 1]);
        set(btnRunButton3,'Callback',{@cbRunPreprocessing,model,'ROI'}); % set here, because of model.    
        set(btnRunButton3,'Enable','on');
end

function model = createSecondStepPanel(model,parent)
basecolor = 'w';

pTime = uipanel(parent,'Units','normalized','Position',[0.0 0.8 1 0.2]);
    set(pTime,'Title','Decode Timeframe Options');
    set(pTime,'BackgroundColor',basecolor);
    grid_h = 0.3;
    grid_w = 0.3;
    time_grid = cell([3 3]);
    time_grid{1,1} = [0.0 0.63 grid_w grid_h];
    time_grid{2,1} = [0.4 0.63 grid_w grid_h];
    time_grid{3,1} = [0.7 0.63 grid_w grid_h];
    time_grid{1,2} = [0.0 0.33 grid_w grid_h];
    time_grid{2,2} = [0.4 0.33 grid_w grid_h];
    time_grid{3,2} = [0.7 0.33 grid_w grid_h];
    time_grid{1,3} = [0.0 0.03 grid_w grid_h];
    time_grid{2,3} = [0.4 0.03 grid_w grid_h];
    time_grid{3,3} = [0.7 0.03 grid_w grid_h];

    lStart      = createLabel(pTime, cell2mat(time_grid(2,1)) ,'Start [sec]');
    lEnd        = createLabel(pTime, cell2mat(time_grid(3,1)) ,'End [sec]');
    lFrameShift = createLabel(pTime, cell2mat(time_grid(1,2)),'Frame Shift');
    lFramsSize  = createLabel(pTime, cell2mat(time_grid(1,3)),'Frame Size');
    
    model.txtFrameShiftStart   = createTextField(pTime,cell2mat(time_grid(2,2)),'');
    model.txtFrameShiftEnd     = createTextField(pTime,cell2mat(time_grid(3,2)),'');
    model.txtFrameShiftDur     = createTextField(pTime,cell2mat(time_grid(2,3)),'');

    
pSVM = uipanel(parent,'Units','normalized','Position',[0 0.4 0.5 0.4]);
    set(pSVM,'Title','SVM Classification');
    set(pSVM,'BackgroundColor',basecolor);

    model.txtSVMopts = createTextField(pSVM,[0 0.83 1 0.16],'');
    set(model.txtSVMopts,'HorizontalAlignment','left');
    
    model.txtSVMnfold = createTextField(pSVM,[0.0 0.66 0.5 0.16],'');
    createLabel(pSVM,[0.5 0.50 0.5 0.25 ],'-Fold CrossVal');
    
    model.chkSVMrnd = uicontrol(pSVM,'Style','checkbox','Units','normalized','Position',[0.1 0.50 0.9 0.16]);
    set(model.chkSVMrnd,'String','Randomize Datapoints');
    set(model.chkSVMrnd,'BackgroundColor','w');
%     set(model.chkSVMrnd,'Value',StudyArgs.svmrnd);
    
    btnRunSVM = uicontrol(pSVM,'String','run batchmode SVM Crossvalidation',...
        'Units','normalized',...
        'Position',[0 0.25 1 0.25]);
    set(btnRunSVM,'Enable','on');
    
    btnRunXSVM = uicontrol(pSVM,'String','run SVM X-Subject validation',...
        'Units','normalized',...
        'Position',[0 0.0 1 0.25]);
    set(btnRunXSVM,'Enable','on');
    
pSOM = uipanel(parent,'Units','normalized','Position',[0.5 0.4 0.5 0.4]);
    set(pSOM,'Title','SOM Classification');
    set(pSOM,'BackgroundColor',basecolor);

    createLabel(pSOM,[0 0.75 0.3 0.20],'Size:');
    model.txtSomM = createTextField(pSOM,[0.3 0.75 0.25 0.2],'');
    somsizemal = createLabel(pSOM,[0.55 0.75 0.2 0.20],'x');
    set(somsizemal,'HorizontalAlignment','center');
    model.txtSomN = createTextField(pSOM,[0.75 0.75 0.25 0.20],'');
    latticeModel = {'rect','hexa'};
    model.selSomLattice = uicontrol(pSOM,'Style','popupmenu',...
        'Units','normalized',...
        'Position',[0.0 0.5 0.3 0.20],...
        'String',latticeModel,...
        'UserData',latticeModel);
    set(model.selSomLattice,'BackgroundColor','w');

    model.txtSOMnfold = createTextField(pSOM,[0.5 0.50 0.25 0.20],'');
%     set(model.txtSOMnfold,'Enable','off');
    createLabel(pSOM,[0.75 0.5 0.25 0.20 ],'-Fold CrossVal');
       

    btnRunSOM = uicontrol(pSOM,'String','run SOM Crossvalidation',...
        'Units','normalized',...
        'Position',[0.0 0.25 1 0.25]);
    set(btnRunSOM,'Enable','on');

    btnRunXSOM = uicontrol(pSOM,'String','run SOM X-Subject validation',...
        'Units','normalized',...
        'Position',[0.0 0.0 1 0.25]);
    set(btnRunXSOM,'Enable','on');
    
pSearchlight = uipanel(parent,'Units','normalized','Position',[0.0 0.1 0.5 0.3]);
    set(pSearchlight,'Title','Spatiotemporal FB classification');
    set(pSearchlight,'BackgroundColor',basecolor);
    
    lSearchligh = createLabel(pSearchlight, [0 0.6 0.5 0.3],'Searchlight Radius');
    model.txtSearchlightRadius = createTextField(pSearchlight,[0.5 0.6 0.5 0.3],'');
    
    lFBSTiming = createLabel(pSearchlight, [0 0.3 0.5 0.3],'Optional Timeline ( e.g. ''-5 0 5 10 15'')');
    model.txtSearchlightTimeline = createTextField(pSearchlight,[0.5 0.3 0.5 0.3],'');

    btnRunFBS = uicontrol(pSearchlight,'String','run Spatiotemporal FB classification',...
        'Units','normalized',...
        'Position',[0.0 0.0 1 0.3]);
    set(btnRunFBS,'Enable','on');
    

% button callbacks set here, because of model.
    set(btnRunSVM, 'Callback',{@cbRunDecode,model,'SVM'}); 
    set(btnRunXSVM,'Callback',{@cbRunDecode,model,'XSVM'}); 
    set(btnRunSOM, 'Callback',{@cbRunDecode,model,'SOM'});
    set(btnRunXSOM,'Callback',{@cbRunDecode,model,'XSOM'});
    set(btnRunFBS, 'Callback',{@cbRunDecode,model,'FBS'});
end

function model = createVisualStepPanel(model,parent)

    grid_h = 0.25;
    grid_w = 0.5;
    plot_grid = cell([3 3]);
    plot_grid{1,1} = [0.0 0.63 grid_w grid_h];
    plot_grid{1,2} = [0.0 0.33 grid_w grid_h];
    plot_grid{1,3} = [0.0 0.03 grid_w grid_h];
    plot_grid{2,1} = [0.25 0.63 grid_w grid_h];
    plot_grid{2,2} = [0.25 0.33 grid_w grid_h];
    plot_grid{2,3} = [0.25 0.03 grid_w grid_h];

    pButtonPane = uipanel(parent,'Units','normalized','Position',[0 0.5 1 0.5]);
%     set(pButtonPane,'Title','Plot');
    set(pButtonPane,'BackgroundColor','w');
    
    btnPlot03 = uicontrol(pButtonPane,'String','plot PSTH',...
        'Units','normalized',...
        'Position',cell2mat(plot_grid(2,1)));
    set(btnPlot03,'Callback',{@cbPlot,model,'psth'}); 
    set(btnPlot03,'Enable','on');
  
    btnPlot01 = uicontrol(pButtonPane,'String','plot performance and SE',...
        'Units','normalized',...
        'Position',cell2mat(plot_grid(2,2)));
    set(btnPlot01,'Callback',{@cbPlot,model,'simple'});
    set(btnPlot01,'Enable','on');
    
    btnPlot02 = uicontrol(pButtonPane,'String','plot subject performance and mean with SE',...
        'Units','normalized',...
        'Position',cell2mat(plot_grid(2,3)));
    set(btnPlot02,'Callback',{@cbPlot,model,'x-subject-val'});
    set(btnPlot02,'Enable','on');

    
end

%%%%% ui callbacks

function cbSwitchTask(src,evnt,task,taskpanel)
set(taskpanel.preprocessing,'Visible','off');
set(taskpanel.classification,'Visible','off');
set(taskpanel.plot,'Visible','off');
switch task
    case 'PRE'
        set(taskpanel.preprocessing,'Visible','on');

    case 'CLASSIFY'
        set(taskpanel.classification,'Visible','on');

    case 'PLOT'
        set(taskpanel.plot,'Visible','on');
end
% assignin('base','model',model);
end

function cbRunPreprocessing(src,evnt,model,task)
main(model,'pre',task);
end

function cbRunDecode(src,evnt,model,task)
main(model,'decode',task);
end

function cbPlot(src,evnt,model,type)
main(model,'plot',type);
end

%%%%%%%%%% menu callbacks

function mcb_save(src,evnt,model)
studyID = get(model.txtStudyID,'String');
saveStudy(studyID,model);
end

function mcb_new_study(src,evnt,studymenu,uimodel)
display('CREATE STUDY');
    ui_createStudy(studymenu,uimodel);
end