function [p] = gui_mesh_save(p,mesh_save_command,parent)
% GUI_MESH_OPEN: Load an emse mesh (wfr) file into matlab workspace
% 
% Usage: [p] = gui_mesh_save(p,[command],[parent])
%
% p is a structure, generated by 'eeg_toolbox_defaults'
% command is either 'init' or 'save'
% parent is a handle to the gui that calls this gui, useful
% for updating the UserData field of the parent from this gui.
% The p structure is returned to the parent when the parent 
% handle is given.
%

% $Revision: 1.1 $ $Date: 2009-04-28 22:13:56 $

% Licence:  GNU GPL, no express or implied warranties
% History:  02/02 Darren.Weber_at_radiology.ucsf.edu
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ~exist('p','var'),
   [p] = eeg_toolbox_defaults;
elseif isempty(p),
   [p] = eeg_toolbox_defaults;
end

if ~exist('mesh_save_command','var'), mesh_save_command = 'init'; end

switch mesh_save_command,
    
case 'init',
    
    if exist('parent','var'),
        MESHSave = INIT(p,parent);
    else
        MESHSave = INIT(p);
    end
    
case 'save',
    
    MESHSave = get(gcbf,'Userdata');
    gui_mesh_save(MESHSave.p,'init',MESHSave.gui);
    
    if isequal(get(MESHSave.handles.Bhold,'Value'),1),
       [p] = gui_updateparent(MESHSave,0);
    else
       [p] = gui_updateparent(MESHSave);
        close gcbf;
    end
    
    fprintf('GUI_MESH_OPEN: Save As not yet implemented\n');
    
    
otherwise,
    
    close gcbf;
    
end

return



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [MESHSave] = INIT(p,parent),
    
    % GUI General Parameters
    
    GUIwidth  = 500;
    GUIheight = 120;
    
    GUI = figure('Name','MESH File Load','Tag','MESH_OPEN',...
                 'NumberTitle','off',...
                 'MenuBar','none','Position',[1 1 GUIwidth GUIheight]);
    movegui(GUI,'center');
    
    MESHSave.gui = GUI;
    
    Font.FontName   = 'Helvetica';
    Font.FontUnits  = 'Pixels';
    Font.FontSize   = 12;
    Font.FontWeight = 'normal';
    Font.FontAngle  = 'normal';
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Mesh Data Selection and Parameters

    G.Title_data = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[.01 .75 .17 .2],...
        'TooltipString','Mesh/Tesselation Formats (see, ''help mesh_open'')',...
        'String','Mesh Type:','HorizontalAlignment','left');
    
    switch p.mesh.type
        case 'BrainStorm',       meshType = 1;
        otherwise,               meshType = 1;
    end
    G.PmeshType = uicontrol('Tag','PmeshType','Parent',GUI,'Style','popupmenu',...
        'Units','Normalized',Font,  ...
        'Position',[.20 .75 .28 .2],...
        'String',{'BrainStorm'},'Value',meshType,...
        'TooltipString','Mesh/Tesselation Formats (see, ''help mesh_save'')',...
        'Callback',strcat('MESHSave = get(gcbf,''Userdata'');',...
                          'MESHSave.p.mesh.type = popupstr(MESHSave.handles.PmeshType);',...
                          'set(gcbf,''Userdata'',MESHSave); clear MESHSave;'));
                      
    G.Title_path = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[.01 .50 .17 .2],...
        'String','Path','HorizontalAlignment','left');
    G.EmeshPath = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
    	'Position',[.20 .50 .58 .2], 'String',p.mesh.path,...
        'Callback',strcat('MESHSave = get(gcbf,''Userdata'');',...
                          'MESHSave.p.mesh.path = get(MESHSave.handles.EmeshPath,''String'');',...
                          'set(gcbf,''Userdata'',MESHSave); clear MESHSave;'));
    
    G.Title_file = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[.01 .25 .17 .2],...
        'String','File','HorizontalAlignment','left');
    G.EmeshFile = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
    	'Position',[.20 .25 .58 .2], 'String',p.mesh.file,...
        'Callback',strcat('MESHSave = get(gcbf,''Userdata'');',...
                          'MESHSave.p.mesh.file = get(MESHSave.handles.EmeshFile,''String'');',...
                          'set(gcbf,''Userdata'',MESHSave); clear MESHSave;'));
    
    Font.FontWeight = 'bold';
    
    % BROWSE: Look for the data
    browsecommand = strcat('MESHSave = get(gcbf,''Userdata'');',...
        'cd(MESHSave.p.mesh.path);',...
        '[file, path] = uigetfile(',...
        '{''*.dat;*.txt'', ''ASCII Tesselations (*.dat,*.txt)'';', ...
        ' ''*.asc;*.tri'', ''FreeSurfer Tesselations (*.asc,*.tri)'';', ...
        ' ''*.wfr'', ''EMSE Tesselations (*.wfr)'';', ...
        ' ''*.mat'', ''BrainStorm Tesselations (*.mat)'';', ...
        ' ''*.*'',   ''All Files (*.*)''},', ...
        '''Select Mesh/Tesselation File'');',...
        'if ~isequal(path,0), MESHSave.p.mesh.path = path; end;',...
        'if ~isequal(file,0), MESHSave.p.mesh.file = file; end;',...
        'set(MESHSave.handles.EmeshPath,''String'',MESHSave.p.mesh.path);',...
        'set(MESHSave.handles.EmeshFile,''String'',MESHSave.p.mesh.file);',...
        'if ~isempty(findstr(file,''.dat'')) | ~isempty(findstr(file,''.txt'')), ',...
            'set(MESHSave.handles.PmeshType,''Value'',1); ',...
            'MESHSave.p.mesh.type = popupstr(MESHSave.handles.PmeshType);',...
        'elseif ~isempty(findstr(file,''.asc'')) | ~isempty(findstr(file,''.tri'')), ',...
            'set(MESHSave.handles.PmeshType,''Value'',2); ',...
            'MESHSave.p.mesh.type = popupstr(MESHSave.handles.PmeshType);',...
        'elseif findstr(file,''.wfr''), ',...
            'set(MESHSave.handles.PmeshType,''Value'',3); ',...
            'MESHSave.p.mesh.type = popupstr(MESHSave.handles.PmeshType);',...
        'else, ',...
            'set(MESHSave.handles.PmeshType,''Value'',4); ',...
            'MESHSave.p.mesh.type = popupstr(MESHSave.handles.PmeshType);',...
        'end;',...
        'set(gcbf,''Userdata'',MESHSave); clear MESHSave;');
    G.BmeshFile = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized',Font, ...
        'Position',[.01 .01 .17 .2], 'String','BROWSE',...
        'BackgroundColor',[0.8 0.8 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback', browsecommand );
    
    % SAVE AS: Load & convert the data!
    G.Bsave = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[.40 .01 .18 .2],...
        'String','SAVE AS','BusyAction','queue',...
        'TooltipString','Load and convert the mesh (not implemented yet).',...
        'BackgroundColor',[0.0 0.0 0.5],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback',strcat('MESHSave = get(gcbf,''Userdata'');',...
                          'p = mesh_write(MESHSave.p,''save'');',...
                          'clear MESHSave;'));
    
    % Cancel
    G.Bcancel = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[.80 .01 .18 .2],...
        'String','CANCEL','BusyAction','queue',...
        'TooltipString','Close, do not return the p struct.',...
        'BackgroundColor',[0.75 0.0 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback','gui_mesh_open([],''cancel'');');
    
    % Store userdata
    if exist('parent','var'), MESHSave.parent.gui = parent; end
    MESHSave.handles = G;
    MESHSave.p = p;
    set(GUI,'HandleVisibility','callback');
    set(GUI,'Userdata',MESHSave);
    
return
