% HIST2M writes a new m-file that reproduces the analysis described in the history object.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: HIST2M writes a new m-file that reproduces the analysis described
%              in the history object.
%
% CALL:        cmds = hist2m(h);
%
% INPUT:       h    - history object
%
% OUTPUT:      cmds - cell array with the commands to reproduce the data
%
% VERSION:     $Id: hist2m.m,v 1.49 2011/03/29 13:40:16 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function cmds = hist2m(varargin)
  
  import utils.const.*
  utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
  
  % Get history objects
  hists = varargin{1};
  
  if nargin >= 2
    pl = combine(varargin{2}, getDefaultPlist());
  else
    pl  = getDefaultPlist();
  end
  
  % Created contains the epochtime in millisecounds and CREATED2INT define
  % the number of the last digits
  CREATED2INT = 1e7;
  
  % get a node list
  utils.helper.msg(msg.PROC1, 'extracting node list from history');
  [n,a, nodes] = getNodes(hists, pl.find('stop_option'));
  utils.helper.msg(msg.PROC1, 'converting history nodes to commands');
  % loop over nodes and convert to commands
  i = 1;
  cmds = {};
  while i <= length(nodes)
    v      = nodes(i).n;
    idx    = find([nodes(:).pn] == i);
    pl     = nodes(i).pl;
    aoName = mod(nodes(i).h.proctime,CREATED2INT);
    hi     = [nodes(idx).h];
    iNames = zeros(size(hi));
    for j=1:length(hi)
      created = hi(j).proctime; %get(hi(j), 'created');
      iNames(j) =  mod(created,CREATED2INT);
    end
    cmd = writeCmd(char(nodes(i).names), pl, aoName, iNames, nodes(i).h.methodInfo.mclass);
    if ~iscell(cmd), cmd = {cmd}; end
    utils.helper.msg(msg.PROC2, 'wrote command for node %d [%s]', i, char(nodes(i).names));
    for kk=1:numel(cmd)
      utils.helper.msg(msg.PROC3, 'command: %s', cmd{kk});
    end
    cmds = {cmds{:} cmd{:}};
    i = i + 1;
  end
  
  % now find commands that are duplicated after the '=' and remap those
  utils.helper.msg(msg.PROC1, 'fixing duplicate commands');
  ncmds = length(cmds);
  for j = 1:ncmds
    cmdj = cmds{j};
    % now inspect all other commands prior to this one
    for k = j+1:ncmds
      cmdk = cmds{k};
      if strcmp(cmdj, cmdk)
        cmds{j} = '';
      end
    end
  end
  % remove empty commands
  cmds = cmds(~strcmp('', cmds));
  
  utils.helper.msg(msg.PROC1, 'writing output line');
  % add the final command to produce a_out
  alast = deblank(strtok(cmds{1}, '='));
  cmds  = [cellstr(sprintf('a_out = %s;', alast)) cmds];
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                               Local Functions                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
% FUNCTION:    writeCmd                                                       %
%                                                                             %
% DESCRIPTION: write a command-line                                           %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function cmd = writeCmd(name, pl, aon, ains,methodclass)
  
  ainsStr = '';
  % for i=length(ains):-1:1
  ni = length(ains);
  for i=1:ni
    ainsStr = [ainsStr sprintf('a%d, ', ains(i))];
  end
  
  name = strrep(name, '\_', '_');
  
  if ~isempty(pl)
    % convert plist to commands
    cmd = plist2cmds(pl);
    % the last command will go as input to the method command
    [s,plstr] = strtok(cmd{1});
    plstr = strtrim(plstr);
    cmd = [ sprintf('a%d = %s(%s%s); %% %s', ...
      aon, name, ainsStr, strtrim(plstr(2:end-1)),methodclass) cmd(2:end)];
  else
    ainsStr = ainsStr(1:end-2);
    cmd = sprintf('a%d = %s(%s);', aon, name, ainsStr);
  end
  
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getDefaultPlist
%
% DESCRIPTION: Get Default Plist
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function plo = getDefaultPlist()
  plo = plist('stop_option', 'full');
end

