% SINGLEPLOT plots each input ao on a seperate figure.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SINGLEPLOT plots each input ao on a seperate figure.
%
% CALL:                   hfig = aoplotter.singlePlot(pl);
%
% OUTPUTS:
%             hfig - an array of figure handles
%
% <a href="matlab:utils.helper.displayMethodInfo('aoplotter', 'singlePlots')">Parameters</a>
%
% VERSION:     $Id: singlePlots.m,v 1.2 2011/03/28 12:45:46 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = singlePlots(varargin)
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  % Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end
  
  % Get the plotter calling object
  plotter = utils.helper.collect_objects(varargin(:), 'aoplotter');
  
  % Collect input plists
  pl = utils.helper.collect_objects(varargin(:), 'plist');
  pl = combine(pl, getDefaultPlist);
  
  hfig = [];
  for kk=1:numel(plotter.data)
    
    % get current data object
    d = plotter.data{kk};
    
    % Plot each figure on a seperate plot  
    cfig = figure;
    hls = plot(d.x, d.y);
    
    % Make axis labels
    setPlotLabels(plotter, cfig, d);
    
    % Apply the figure properties from the plotter
    applyPlotterPreferences(cfig, hls, plotter);
    
    % Apply procinfo properties
    applyLineProperties(plotter, hls, d);
    
    % Apply legend properties
    applyLegendProperties(plotter, d);
    
    % Store the handle
    hfig = [hfig cfig];
  end
  
  
end

function applyLegendProperties(plotter, d)
  
  legendFontSize = plotter.legendFontSize;
  legendLocation = plotter.legendLocation;
  includeLegend = plotter.includeLegend;
  includeDescription = plotter.includeDescription;
  
  if includeLegend
    
    % make legend
    name = utils.plottools.label(d.name);
    desc = utils.plottools.label(d.description);
  
    if includeDescription && ~isempty(desc)
      name = [name ': ' desc];
    end
    
    [legh,objh,outh,outm] = legend(name, 'Location', legendLocation);
    set(legh, 'FontSize', legendFontSize);
  end
  
end

% Apply the properties of the lines. First the values are taken from the
% plotter, but if the plotinfo is filled and the override is set, then
% those values will be used.
function applyLineProperties(plotter, hl, d)
  
  lineWidth        = plotter.lineWidth;
  linestyle        = plotter.lineStyle;
  markerSize       = plotter.markerSize;
  plotinfoOverride = plotter.plotinfoOverride;
  colors           = plotter.plotcolors;
  marker           = plotter.marker;
  color            = colors{1};

  % -- handle procinfo overrides
  pinfo = d.plotinfo;
  % line width
  if ~isempty(pinfo) && ~isempty(pinfo.find('linewidth')) && plotinfoOverride
    lineWidth = pinfo.find('linewidth');
  end
  % line style
  if ~isempty(pinfo) && ~isempty(pinfo.find('linestyle')) && plotinfoOverride
    linestyle = pinfo.find('linestyle');
  end
  % marker
  if ~isempty(pinfo) && ~isempty(pinfo.find('marker')) && plotinfoOverride
    marker = pinfo.find('marker');
  end
  % color
  if ~isempty(pinfo) && ~isempty(pinfo.find('color')) && plotinfoOverride
    color = pinfo.find('color');
  end

  % set color
  set(hl, 'Color', color)
  set(hl, 'LineStyle', linestyle);
  set(hl, 'LineWidth', lineWidth);
  set(hl, 'MarkerSize', markerSize);
  set(hl, 'Marker', marker);
end

function setPlotLabels(plotter, cfig, d)  
  hax = get(cfig, 'Children');
  [xlbl, ylbl, zlbl] = aoplotter.makeAxisLabel(d);
  h = xlabel(hax, xlbl);
  setLabelProperties(h, plotter);
  h = ylabel(hax, ylbl);
  setLabelProperties(h, plotter);
  h = zlabel(hax, zlbl);
  setLabelProperties(h, plotter);
end

function setLabelProperties(h, plotter)
  set(h, 'FontSize', plotter.labelFontSize);
  set(h, 'FontName', plotter.axisFontName);
end

function applyPlotterPreferences(hfig, hls, plotter)
    haxs = get(hfig, 'Children');
    for kk=1:numel(haxs)
      
      % Fonts and lines
      set(haxs(kk), 'FontSize', plotter.axisFontSize);
      set(haxs(kk), 'FontWeight', plotter.axisFontWeight);
      set(haxs(kk), 'FontName', plotter.axisFontName);
      set(haxs(kk), 'LineWidth', plotter.axisLineWidth);
      
      % Grids
      set(haxs(kk), 'GridLineStyle', plotter.gridLineStyle);
      set(haxs(kk), 'MinorGridLineStyle', plotter.minorGridLineStyle);
      
      if plotter.showXGrid
        set(haxs(kk), 'XGrid', 'on');
      end
      if plotter.showXMinorGrid
        set(haxs(kk), 'XMinorGrid', 'on');
      end
      if plotter.showYGrid
        set(haxs(kk), 'YGrid', 'on');
      end
      if plotter.showYMinorGrid
        set(haxs(kk), 'YMinorGrid', 'on');
      end
      
    end
  
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'plotter', 'ltpda', utils.const.categories.output, '$Id: singlePlots.m,v 1.2 2011/03/28 12:45:46 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function pl_default = getDefaultPlist()
  pl_default = plist();
  
end

% END
