function error = preferencesForm(hTsgGUI)
%
% Input
% -----
% hTsgGUI ............ Handel to the main user interface
%
% Output
% ------
% error .............. 1: OK ; -1 : an error occured

% $Id$

error = -1;

% Get the data from the application GUI
% -------------------------------------
tsg = getappdata(hTsgGUI, 'tsg_data');

% set default uicontrol size
% --------------------------
height = 0.03;
length = .15;

% Define uicontrol start position
% -------------------------------
left   = .01;
bottom = .95;
inc_x  =  3 * length;
inc_y  =  0.01;

% preferences Uicontrols in a new figure
% ---------------------------------
hPreferencesFig = figure(...
  'BackingStore','off',...
  'Name', 'TSGQC PREFERENCES', ...
  'NumberTitle', 'off', ...
  'Resize', 'on', ...
  'Menubar','none', ...
  'Toolbar', 'none', ...
  'Tag', 'TSG_PREFERENCES', ...
  'Visible','on',...
  'WindowStyle', 'modal', ...
  'Units', 'normalized',...
  'Position',get(hTsgGUI,'Position'), ...
  'Color', get(0, 'DefaultUIControlBackgroundColor'));

% display Climatology TITLE
% -------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize, 'FontWeight', 'bold',...
  'HorizontalAlignment', 'Left', ...
  'Position',[left, bottom, 2*length, height],'String', 'Climatology');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display climatology text
% -------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', ...
  'Fontsize', tsg.fontSize-1, ...
  'HorizontalAlignment', 'left', ...
  'Position',[left, bottom, length, height], ...
  'TooltipString', 'select climatology origin', ...
  'String', 'Climatology');

% display climatology uicontrol
% -----------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'BackgroundColor', 'w', ...
  'Style', 'popupmenu', ...
  'Fontsize', tsg.fontSize-2, ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.levitus_version, ...
  'Value', tsg.preference.levitus_value, ...
  'Tag', 'PREFERENCES_LEVITUS_VALUE');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display climatology depth text
% -------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', ...
  'Fontsize', tsg.fontSize-1, ...
  'HorizontalAlignment', 'left', ...
  'Position',[left, bottom, length, height], ...
  'TooltipString', 'select climatology depth', ...
  'String', 'Climatology depth');

% display climatology depth  uicontrol
% ------------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'BackgroundColor', 'w', ...
  'Style', 'popupmenu', ...
  'Fontsize', tsg.fontSize-2, ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.levitus_depth_string, ...
  'Value', tsg.preference.levitus_depth_value, ...
  'Tag', 'PREFERENCES_LEVITUS_DEPTH');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display conventions field if exist
% ----------------------------------
% uicontrol(...
%   'Parent', hPreferencesFig, ...
%   'Units', 'normalized', ...
%   'Style', 'text', ...
%   'Fontsize', tsg.fontSize-2, ...
%   'HorizontalAlignment', 'left', ...
%   'Position', [left + 2*length, bottom, length, height], ...
%   'String', char(s.conventions));

% display Calibration TITLE
% --------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize, 'FontWeight', 'bold',...
  'HorizontalAlignment', 'Left', ...
  'Position',[left, bottom, 2*length, height], 'String', 'Calibration coefficient');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display coefficient type text
% -----------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize-1, ...
  'HorizontalAlignment', 'left', 'Position',[left, bottom, length, height], ...
  'TooltipString', 'select coefficients type', ...
  'String', 'Coefficients type');

% display coefficient type uicontrol
% ----------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'BackgroundColor', 'w', 'Style', 'popupmenu', 'Fontsize', tsg.fontSize-2, ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.coeff_type_string , ...
  'Value', tsg.preference.coeff_type_value , ...
  'Tag', 'PREFERENCES_COEFF_TYPE');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display Figure TITLE
% --------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize, 'FontWeight', 'bold',...
  'HorizontalAlignment', 'Left', ...
  'Position',[left, bottom, 2*length, height], 'String', 'Figure');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display connected line uicontrol
% --------------------------------

% display connected line text
% -------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', ...
  'Fontsize', tsg.fontSize-1, 'HorizontalAlignment', 'left', ...
  'Position',[left, bottom, length, height], ...
  'TooltipString', 'select plot with connected line', ...
  'String', 'Plot with connectd line');

uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', 'BackgroundColor', 'w', ...
  'Style', 'popupmenu', 'Fontsize', tsg.fontSize-2, ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.plot_connected_string , ...
  'Value', tsg.preference.plot_connected_value , ...
  'Tag', 'PREFERENCES_PLOT_CONNECTED_LINE');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display map resolution text
% -----------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize-1, ...
  'HorizontalAlignment', 'left', 'Position',[left, bottom, length, height], ...
  'TooltipString', 'select map resolution', ...
  'String', 'Map Resolution');

% display map resolution uicontrol
% --------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'BackgroundColor', 'w', 'Style', 'popupmenu',   'Fontsize', tsg.fontSize-2, ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.map_resolution_string, ...
  'Value', tsg.preference.map_resolution, ...
  'Tag', 'PREFERENCES_MAP_RESOLUTION');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display QC Automatic Test TITLE
% --------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize, 'FontWeight', 'bold',...
  'HorizontalAlignment', 'Left', ...
  'Position',[left, bottom, 2*length, height], ...
  'String', 'Min/Max values for automatic QC');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display ship speed min text
% -----------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', ...
  'Fontsize', tsg.fontSize-1, ...
  'HorizontalAlignment', 'left', ...
  'Position',[left, bottom, length, height], ...
  'TooltipString', 'select ship speed min (knots)', ...
  'String', 'Ship Speed Min (knots)');

% display ship speed min uicontrol
% --------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'BackgroundColor', 'w', ...
  'Style', 'popupmenu', ...
  'Fontsize', tsg.fontSize-2, ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.ship_speed_min_string, ... 
  'Value', tsg.preference.ship_speed_min, 'Tag', 'PREFERENCES_SHIP_SPEED_MIN');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display FLOW min text
% ----------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize-1, 'HorizontalAlignment', 'left', ...
  'Position',[left, bottom, length, height], ...
  'TooltipString', 'Select min Flow (liter/min)', 'String', 'Flow Min (l/minute)');

% display FLOW min uicontrol
% -------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', 'Style', 'Edit', ...
  'Fontsize', tsg.fontSize-2, 'BackgroundColor', 'w', ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.flow_min_string, 'Tag', 'PREFERENCES_FLOW_MIN');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display PRESS min text
% ----------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize-1, 'HorizontalAlignment', 'left', ...
  'Position',[left, bottom, length, height], ...
  'TooltipString', 'Select min pressure pump', 'String', 'Pressure Min (l/minute)');

% display PRESS min uicontrol
% -------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', 'Style', 'Edit', ...
  'Fontsize', tsg.fontSize-2, 'BackgroundColor', 'w', ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.press_min_string, 'Tag', 'PREFERENCES_PRESS_MIN');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display SSPS min text
% ----------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize-1, 'HorizontalAlignment', 'left', ...
  'Position',[left, bottom, length, height], ...
  'TooltipString', 'Select min SSPS', 'String', 'SSPS min');

% display SSPS min uicontrol
% -------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', 'Style', 'Edit', ...
  'Fontsize', tsg.fontSize-2, 'BackgroundColor', 'w', ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.ssps_min_string, 'Tag', 'PREFERENCES_SSPS_MIN');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display SSPS max text
% ----------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize-1, 'HorizontalAlignment', 'left', ...
  'Position',[left, bottom, length, height], ...
  'TooltipString', 'Select max SSPS', 'String', 'SSPS max');

% display SSPS max uicontrol
% -------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', 'Style', 'Edit', ...
  'Fontsize', tsg.fontSize-2, 'BackgroundColor', 'w', ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.ssps_max_string, 'Tag', 'PREFERENCES_SSPS_MAX');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display SSTP min text
% ----------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize-1, 'HorizontalAlignment', 'left', ...
  'Position',[left, bottom, length, height], ...
  'TooltipString', 'Select min SSTP', 'String', 'SSTP min (°C)');

% display SSTP min uicontrol
% -------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', 'Style', 'Edit', ...
  'Fontsize', tsg.fontSize-2, 'BackgroundColor', 'w', ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.sstp_min_string, 'Tag', 'PREFERENCES_SSTP_MIN');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% display SSTP max text
% ----------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', ...
  'Style', 'Text', 'Fontsize', tsg.fontSize-1, 'HorizontalAlignment', 'left', ...
  'Position',[left, bottom, length, height], ...
  'TooltipString', 'Select max SSTP', 'String', 'SSTP max (°C)');

% display SSTP max uicontrol
% -------------------------------
uicontrol(...
  'Parent', hPreferencesFig, ...
  'Units', 'normalized', 'Style', 'Edit', ...
  'Fontsize', tsg.fontSize-2, 'BackgroundColor', 'w', ...
  'HorizontalAlignment', 'right', ...
  'Position', [left + length, bottom, length, height], ...
  'String', tsg.preference.sstp_max_string, 'Tag', 'PREFERENCES_SSTP_MAX');

bottom = bottom - inc_y - height;
if bottom < .1
  bottom = 0.95;
  left = left + inc_x;
end

% CONTINUE PUSH BUTTON
% --------------------
% The Continue (valid)  button
uicontrol( ...
  'Parent', hPreferencesFig, ...
  'Style','pushbutton',...
  'Fontsize', tsg.fontSize,...
  'Units', 'normalized', ...
  'Position',[.01, .01, .1, .04],...
  'String','Continue',...
  'Interruptible','off',...
  'BusyAction','cancel',...
  'Tag','PREF_CONTINUE_BUTTON',...
  'Callback', @continueCallback);

% CANCEL PUSH BUTTON
% ------------------
% The cancel  button
uicontrol( ...
  'Parent', hPreferencesFig, ...
  'Style','pushbutton',...
  'Fontsize', tsg.fontSize,...
  'Units', 'normalized', ...
  'Position',[.2, .01, .1, .04],...
  'String','Cancel',...
  'Interruptible','off',...
  'BusyAction','cancel',...
  'Tag','PREF_CANCEL_BUTTON',...
  'Callback', @cancelCallback);

% Build structure container for graphic handles with tag property defined.
% ------------------------------------------------------------------------
prefs = guihandles(hPreferencesFig);

% record data struct in application data object
% ---------------------------------------------
% guidata(hHeaderFig, data);

% stop execution until push-button Continue was press
% ---------------------------------------------------
uiwait(hPreferencesFig);

%% Nested callback

% -----------------------------------------------------------------------
% Continue action, get uicontrol fields and populate tsg structure
% -----------------------------------------------------------------------
  function continueCallback(obj, event)

    % Save tsg structure
    % ------------------
    setappdata(hTsgGUI, 'tsg_data', tsg);

    % get the corresponding string from uicontrol using guihandles
    % and save value in tsg.preferences struct
    % ------------------------------------------------------------
    tsg.preference.levitus_value = ...
      get(prefs.PREFERENCES_LEVITUS_VALUE, 'value');
    tsg.preference.levitus_depth_value = ...
      get(prefs.PREFERENCES_LEVITUS_DEPTH, 'value');
    tsg.preference.coeff_type_value = ...
      get(prefs.PREFERENCES_COEFF_TYPE, 'value');
    % Figure
    tsg.preference.plot_connected_value = ...
      get(prefs.PREFERENCES_PLOT_CONNECTED_LINE, 'value');
    tsg.preference.map_resolution = ...
      get(prefs.PREFERENCES_MAP_RESOLUTION, 'value');
    % QC Tests
    tsg.preference.flow_min_string = ...
      get(prefs.PREFERENCES_FLOW_MIN, 'string');
    tsg.preference.press_min_string = ...
      get(prefs.PREFERENCES_PRESS_MIN, 'string');
    tsg.preference.ship_speed_min = ...
      get(prefs.PREFERENCES_SHIP_SPEED_MIN, 'value');
    tsg.preference.ssps_min_string = ...
      get(prefs.PREFERENCES_SSPS_MIN, 'string');
    tsg.preference.ssps_max_string = ...
      get(prefs.PREFERENCES_SSPS_MAX, 'string');
    tsg.preference.sstp_min_string = ...
      get(prefs.PREFERENCES_SSTP_MIN, 'string');
    tsg.preference.sstp_max_string = ...
      get(prefs.PREFERENCES_SSTP_MAX, 'string');
    
    % Save tsg structure
    % ------------------
    setappdata(hTsgGUI, 'tsg_data', tsg);
    
    % fill tsg calibration variables
    % ------------------------------
    updateTsgStructWithCalCoeff(hTsgGUI);

    % close windows (replace call to uiresume(hHeaderFig))
    % ----------------------------------------------------
    close(hPreferencesFig);

    % flushes the event queue and updates the figure window
    % -----------------------------------------------------
     drawnow;
    
    % refresh QC statistic panel
    % --------------------------
    display_QC( hTsgGUI );

    % return with no error code (true)
    % --------------------------------
    error = 1;

    return

  end
% -----------------------------------------------------------------------
% Cancel button, no action
% -----------------------------------------------------------------------
  function cancelCallback(obj, event)

    % close windows
    % -------------
    close(hPreferencesFig);

    % flushes the event queue and updates the figure window
    % -----------------------------------------------------
    drawnow;

    % return error code (no change)
    % -----------------------------
    error = -1;

  end % end of cancelCallback nested function

end