% BUILDPARAMSPANEL builds a parameter panel for the given plist.
% 
% M Hewitson
% 
% $Id: buildParamsPanel.m,v 1.5 2011/04/08 08:56:34 hewitson Exp $
% 

function buildParamsPanel(ph, ii, varargin)

  FONTSIZE = 12;
  
  pmarg = 0.025;

  keyl = pmarg;
  keyh = 0.1;
  keyw = 0.3;
  keyb = 1-pmarg-keyh;

  vall = pmarg*2 + keyw;
  valw = 0.5;

  al   = vall + valw + pmarg;
  aw   = 0.05;

  delete(get(ph, 'Children'))

  if numel(varargin) > 0
    activated = varargin{1};
    if strcmpi(activated, 'on')
      chkVal = 1;
    else
      chkVal = 0;
    end
  else
    activated = 'on';
    chkVal = 1;
  end
  
  pancomps = {};

  if isa(ii, 'minfo')
    % we give a pop-up menu of sets
    eh = uicontrol(ph,'Style','popupmenu',...
      'String',ii.sets,...
      'units', 'normalized', ...
      'BackgroundColor', 'w', ...
      'Fontsize', FONTSIZE, ...
      'Enable', 'on', ...
      'Position',[vall keyb valw keyh]);

    set(eh, 'Callback', {@ltpdv_preproc_sets_callback, ii});
  else
    pl = ii;
    
    if numel(pl.params) == 0
        sth = uicontrol(ph,'Style','text',...
          'String', 'contains no parameters',...
          'Units', 'normalized', ...
          'BackgroundColor', 'w', ...
          'Fontsize', FONTSIZE, ...
          'Position',[keyl keyb 2*keyw keyh]);
    end
    % check for any unsupported parameters
    unsupported = false;
    for j=1:numel(pl.params)
      val = pl.params(j).getVal;
      if isa(val, 'timespan')
        unsupported = true;
      end
    end
    if unsupported
        sth = uicontrol(ph,'Style','text',...
          'String', ['contains unsupported parameter of type: ' class(val)],...
          'Units', 'normalized', ...
          'BackgroundColor', 'w', ...
          'Fontsize', FONTSIZE, ...
          'Position',[keyl keyb 2*keyw keyh]);
      
    else
      % Build parameters
      for j=1:numel(pl.params)

        % Get key and val
        key = pl.params(j).key;
        val = pl.params(j).getVal;

        switch class(val)
          case 'char'
            valstr = val;
          case 'double'
            valstr = mat2str(val);
          case 'specwin'
            valstr = specwin.getTypes;
          case 'logical'
            if val
              valstr = 'true';
            else
              valstr = 'false';
            end
          otherwise
            valstr = char(val);
        end

        % key text
        sth = uicontrol(ph,'Style','text',...
          'String', key,...
          'Units', 'normalized', ...
          'BackgroundColor', 'w', ...
          'Fontsize', FONTSIZE, ...
          'Position',[keyl keyb keyw keyh]);

        % val edit
        if ischar(valstr) || isa(valstr, 'time') || isa(valstr, 'sym')
          if isa(valstr, 'time') || isa(valstr, 'sym')
            valstr = char(valstr);
          end
          eh = uicontrol(ph,'Style','edit',...
            'String', valstr,...
            'units', 'normalized', ...
            'BackgroundColor', 'w', ...
            'Fontsize', FONTSIZE, ...
            'Enable', activated, ...
            'Position',[vall keyb valw keyh]);
        elseif iscell(valstr)
          eh = uicontrol(ph,'Style','popupmenu',...
            'String',valstr,...
            'units', 'normalized', ...
            'BackgroundColor', 'w', ...
            'Fontsize', FONTSIZE, ...
            'Enable', activated, ...
            'Position',[vall keyb valw keyh]);
        else
          valstr
          error('### Unknown type for value string.');
        end
        setappdata(eh, 'valClass', class(val));
        % Activate
        ah = uicontrol(ph,'Style','checkbox',...
          'String','',...
          'units', 'normalized', ...
          'BackgroundColor', get(ph, 'BackgroundColor'), ...
          'Fontsize', FONTSIZE, ...
          'Value', chkVal, 'Position', [al keyb aw keyh], ...
          'Callback', {@ltpdv_preproc_param_act, ph});

        pancomps = [pancomps; {sth, eh, ah}];

        % Do next line
        keyb = keyb - pmarg - keyh;

      end
    end
  end

  setappdata(ph, 'pancomps', pancomps);


end
