function [GUI] = gui_eeg_ascii_parameters(parent)

% gui_eeg_ascii_parameters - Define EEG ascii parameters
% 
% Usage: [GUI] = gui_eeg_ascii_parameters(parentgui)
%
% parentgui     - is a handle to the parent gui that calls
%                 this gui; it will contain a userdata property
%                 that contains the p structure
%
% In the event that the parentgui doesn't contain the p
% struct, this function calls eeg_toolbox_defaults 
% to create the p struct.
%
% The GUI return value is a handle to this gui (useful in
% 'waitfor' commands) and p is as above.  This function can
% update the p struct of the parentgui userdata too.
%

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

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

% reset the gui_struct parent field to the handles of the input gui_struct

if exist('parent','var'),
    if ~isempty(parent),
        ASCIIParam.parent.gui = parent;
        ParentUserdata = get(parent,'Userdata');
        if isfield(ParentUserdata,'p'),
            if ~isempty(ParentUserdata.p),
                ASCIIParam.p = ParentUserdata.p;
            else
                ASCIIParam.p = eeg_toolbox_defaults('create');
            end
        else
            ASCIIParam.p = eeg_toolbox_defaults('create');
        end
    else
        ASCIIParam.p = eeg_toolbox_defaults('create');
    end
end

% GUI General Parameters
GUIwidth  = 300;
GUIheight = 250;
GUI = figure('Name','ERP PARAMETERS','Tag','ERP_PARAMETERS',...
             'NumberTitle','off','HandleVisibility','callback',...
             'MenuBar','none','Position',[1 1 GUIwidth GUIheight]);
movegui(GUI,'center');

ASCIIParam.gui = GUI;

Font.FontName   = 'Helvetica';
Font.FontUnits  = 'Pixels';
Font.FontSize   = 12;
Font.FontWeight = 'normal';
Font.FontAngle  = 'normal';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Voltage Data Parameters

G.TvoltChannels = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
    'Position',[.05 .8 .45 .1],'String','Channels','HorizontalAlignment','left');
G.EvoltChannels = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
	'Position',[.51 .8 .4 .1], 'String',num2str(ASCIIParam.p.volt.channels),...
    'Callback',strcat('ASCIIParam = get(gcbf,''Userdata'');',...
    'ASCIIParam.p.volt.channels = str2num(get(ASCIIParam.handles.EvoltChannels,''String''));',...
    'set(gcbf,''Userdata'',ASCIIParam); clear ASCIIParam;'));

G.TvoltagePoints = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
    'Position',[.05 .7 .45 .1],'String','Sample Points *','HorizontalAlignment','left',...
    'TooltipString','Essential');
G.EvoltagePoints = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
	'Position',[.51 .7 .4 .1], 'String',num2str(ASCIIParam.p.volt.points),...
    'Callback',strcat('ASCIIParam = get(gcbf,''Userdata'');',...
    'ASCIIParam.p.volt.points = str2num(get(ASCIIParam.handles.EvoltagePoints,''String''));',...
    'set(gcbf,''Userdata'',ASCIIParam); clear ASCIIParam;'));

G.TvoltSampleHz = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
    'Position',[.05 .6 .45 .1],'String','Sample Rate (Hz) *','HorizontalAlignment','left',...
    'TooltipString','Essential');
G.EvoltSampleHz = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
	'Position',[.51 .6 .4 .1], 'String',num2str(ASCIIParam.p.volt.sampleHz),...
    'TooltipString','1000 / Sample Rate (msec)',...
    'Callback',strcat('ASCIIParam = get(gcbf,''Userdata'');',...
            'ASCIIParam.p.volt.sampleHz = str2num(get(ASCIIParam.handles.EvoltSampleHz,''String''));',...
            'ASCIIParam.p.volt.sampleMsec = 1000 ./ ASCIIParam.p.volt.sampleHz;',...
            'set(ASCIIParam.handles.EvoltSampleMsec,''String'',num2str(ASCIIParam.p.volt.sampleMsec));',...
            'set(gcbf,''Userdata'',ASCIIParam); clear ASCIIParam;'));

G.TvoltSampleMsec = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
    'Position',[.05 .5 .45 .1],'String','Sample Rate (msec) *','HorizontalAlignment','left',...
    'TooltipString','Essential');
G.EvoltSampleMsec = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
	'Position',[.51 .5 .4 .1], 'String',num2str(ASCIIParam.p.volt.sampleMsec),...
    'TooltipString','1000 / Sample Rate (Hz)',...
    'Callback',strcat('ASCIIParam = get(gcbf,''Userdata'');',...
            'ASCIIParam.p.volt.sampleMsec = str2num(get(ASCIIParam.handles.EvoltSampleMsec,''String''));',...
            'ASCIIParam.p.volt.sampleHz = 1000 ./ ASCIIParam.p.volt.sampleMsec;',...
            'set(ASCIIParam.handles.EvoltSampleHz,''String'',num2str(ASCIIParam.p.volt.sampleHz));',...
            'set(gcbf,''Userdata'',ASCIIParam); clear ASCIIParam;'));

G.TvoltEpochStart = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
    'Position',[.05 .4 .45 .1],'String','Epoch Start (msec) *','HorizontalAlignment','left',...
    'TooltipString','Essential');
G.EvoltEpochStart = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
	'Position',[.51 .4 .4 .1], 'String',num2str(ASCIIParam.p.volt.epochStart),...
    'TooltipString','Prestimulus Baseline Time (msec)',...
    'Callback',strcat('ASCIIParam = get(gcbf,''Userdata'');',...
    'ASCIIParam.p.volt.epochStart = str2num(get(ASCIIParam.handles.EvoltEpochStart,''String''));',...
    'if ~isempty(ASCIIParam.p.volt.points) & ~isempty(ASCIIParam.p.volt.sampleMsec), ',...
        'ASCIIParam.p.volt.epochEnd = ASCIIParam.p.volt.epochStart + (ASCIIParam.p.volt.points .* ASCIIParam.p.volt.sampleMsec);',...
        'set(ASCIIParam.handles.EvoltEpochEnd,''String'',num2str(ASCIIParam.p.volt.epochEnd));',...
    'end; ',...
    'set(gcbf,''Userdata'',ASCIIParam); clear ASCIIParam;'));

G.TvoltEpochEnd = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
    'Position',[.05 .3 .45 .1],'String','Epoch End (msec) *','HorizontalAlignment','left',...
    'TooltipString','Essential');
G.EvoltEpochEnd = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
	'Position',[.51 .3 .4 .1], 'String',num2str(ASCIIParam.p.volt.epochEnd),...
    'TooltipString','Poststimulus Epoch Time (msec)',...
    'Callback',strcat('ASCIIParam = get(gcbf,''Userdata'');',...
    'ASCIIParam.p.volt.epochEnd = str2num(get(ASCIIParam.handles.EvoltEpochEnd,''String''));',...
    'set(gcbf,''Userdata'',ASCIIParam); clear ASCIIParam;'));

G.TvoltSweeps = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
    'Position',[.05 .2 .45 .1],'String','Accepted Sweeps','HorizontalAlignment','left');
G.EvoltSweeps = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
	'Position',[.51 .2 .4 .1], 'String',num2str(ASCIIParam.p.volt.sweeps),...
    'Callback',strcat('ASCIIParam = get(gcbf,''Userdata'');',...
    'ASCIIParam.p.volt.sweeps = str2num(get(ASCIIParam.handles.EvoltSweeps,''String''));',...
    'set(gcbf,''Userdata'',ASCIIParam); clear ASCIIParam;'));

G.Tessential = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
    'Position',[.05 .1 .45 .1],'String','( * are essential )','HorizontalAlignment','left');


Font.FontWeight = 'bold';

% OK: Return the parameters!
donecommand = strcat('ASCIIParam = get(gcbf,''Userdata''); ',...
 	'if isempty(ASCIIParam.p.volt.sampleHz) | isempty(ASCIIParam.p.volt.epochStart) | isempty(ASCIIParam.p.volt.points),',...
        'help = helpdlg(''Please Specify Essential (*) Parameters'',''HELP'');',...
        'movegui(help,''center''); waitfor(help); clear ASCIIParam help;',...
    'else, ',...
        'ASCIIParam.p.volt.timeArray = zeros(ASCIIParam.p.volt.points,1);',...
        't = ASCIIParam.p.volt.epochStart;',...
        'for i=1:ASCIIParam.p.volt.points, ',...
            'ASCIIParam.p.volt.timeArray(i,1) = t;',...
            't = t + ASCIIParam.p.volt.sampleMsec;',...
        'end; clear i t;',...
        'if isfield(ASCIIParam,''parent''),',...
            'if isfield(ASCIIParam.parent,''gui''),',...
                'parent = get(ASCIIParam.parent.gui,''UserData'');',...
                'if isfield(parent,''p'') & isfield(ASCIIParam,''p''),',...
                    'parent.p = ASCIIParam.p;',...
                    'set(ASCIIParam.parent.gui,''UserData'',parent);',...
                    'clear parent;',...
                'end; ',...
            'end; ',...
        'end; ',...
        'close gcbf; clear ASCIIParam;',...
    'end;');




G.Bdone = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
    'Position',[.51 .1 .19 .1],...
    'String','OK','BusyAction','queue',...
    'TooltipString','Update the ascii parameters.',...
    'BackgroundColor',[0.0 0.5 0.0],...
    'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
    'Callback',donecommand);
                  
% Cancel
G.Bquit = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
    'Position',[.71 .1 .2 .1],...
    'String','CANCEL','BusyAction','queue',...
    'BackgroundColor',[0.75 0.0 0.0],...
    'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
    'Callback','close gcbf;');


% Update the gui_struct handles for this gui
ASCIIParam.handles = G;
set(ASCIIParam.gui,'Userdata',ASCIIParam);

uiwait(GUI);

return
