% GERERATE_REPORT reads all unit test and generates a report.

% TODO
% 
%   + Format UTP table by hand - not using \utp
%   + move code block to tables in appendix with same numbering as UTP tables
%   + add pdf link in UTP table to code block
%   + keep code indentation from m-file
%   + wrap strings using utils.prog.wrapstring (or similar) - add ... 
% 

function report = generate_report()

  setappdata(0, 'ltpda_verbose_level', -1);

  pdfFilename = 'report/report.pdf';
  texFilename = 'report/ltpda_report_template.html';
  repFilename = 'report/report.html';

  load resultsave.mat results
  % results = utp_run('utp_ao_log10');
  disp(results(1).a);
  disp(results(1).s);
  disp(results(1).fcn);
  disp(results(1).num);
  disp(results(1).date);

  inFile = fopen(texFilename, 'r');

  methodDescription = '';
  testDescription = '';
  syntaxDescription = '';
  algoDescription = '';
  syntaxCode = '';
  algoCode = '';

  writeDirectly = true;
  outputTex = '';

  % Get the rest:
  while ~feof(inFile)
    line = fgetl(inFile);

    if strfind(line, '<!-- Report /-->')
      writeDirectly = false;
      preTests = outputTex;
      outputTex = '';
    end

    if strfind(line, '<!-- EndReport /-->')
      writeDirectly = true;
    end

    if strfind(line, '<!-- MethodDescription /-->')
      methodDescription = '';
      while isempty(strfind(line, '<!-- EndMethodDescription /-->'))
        methodDescription = sprintf('%s%s\n', methodDescription, line);
        line = fgetl(inFile);
      end
      methodDescription = sprintf('%s%s\n', methodDescription, line);
    end

    if strfind(line, '<!-- TestDescription /-->')
      testDescription = '';
      while isempty(strfind(line, '<!-- EndTestDescription /-->'))
        testDescription = sprintf('%s%s\n', testDescription, line);
        line = fgetl(inFile);
      end
      testDescription = sprintf('%s%s\n', testDescription, line);
    end

    if strfind(line, '<!-- SyntaxDescription /-->')
      syntaxDescription = '';
      while isempty(strfind(line, '<!-- EndSyntaxDescription /-->'))
        syntaxDescription = sprintf('%s%s\n', syntaxDescription, line);
        line = fgetl(inFile);
      end
      syntaxDescription = sprintf('%s%s\n', syntaxDescription, line);
    end

    if strfind(line, '<!-- AlgoDescription /-->')
      algoDescription = '';
      while isempty(strfind(line, '<!-- EndAlgoDescription /-->'))
        algoDescription = sprintf('%s%s\n', algoDescription, line);
        line = fgetl(inFile);
      end
      algoDescription = sprintf('%s%s\n', algoDescription, line);
    end

    if strfind(line, '<!-- SyntaxCode /-->')
      syntaxCode = '';
      while isempty(strfind(line, '<!-- EndSyntaxCode /-->'))
        syntaxCode = sprintf('%s%s\n', syntaxCode, line);
        line = fgetl(inFile);
      end
      syntaxCode = sprintf('%s%s\n', syntaxCode, line);
    end

    if strfind(line, '<!-- AlgoCode /-->')
      algoCode = '';
      while isempty(strfind(line, '<!-- EndAlgoCode /-->'))
        algoCode = sprintf('%s%s\n', algoCode, line);
        line = fgetl(inFile);
      end
      algoCode = sprintf('%s%s\n', algoCode, line);
    end

    if writeDirectly
      outputTex = sprintf('%s%s\n', outputTex, line);
    end
  end
  postTests = outputTex;

  fclose(inFile);

  % All vars are ready, now read test files:

  outFile = fopen(repFilename, 'wt');
  filenames = {};
  filenames{end+1} = 's';
  for i=1:length(results)
    positions = strfind(results(i).fcn, '_');
    lastUnderscore = positions(length(positions)) -1;
    if ~strcmp(filenames{end}, sscanf(results(i).fcn, '%c', lastUnderscore))
      filenames{end+1} = sscanf(results(i).fcn, '%c', lastUnderscore);
    end
  end

  % Print the intro
  fprintf(outFile, '%s', preTests);
  temp = '';

  for i=2:length(filenames)
    filename = ['utps/ao/' filenames{i} '.m'];
    if exist(filename, 'file')
      utpRead = fopen(filename);

      headerNumber = 1;

      % Get header:
      if ~feof(utpRead), header = fgetl(utpRead); end
      header = remComment(header);
      header = escTex(header);

      % Get rest:
      while ~feof(utpRead)
        line = fgetl(utpRead);
        line = strtrim(line);

        % if strfind(line, 'function result')

        if strfind(line, '<MethodDescription>')
          line = fgetl(utpRead);
          utpMethodDescription = '';
          while isempty(strfind(line, '</MethodDescription>'))
            utpMethodDescription = sprintf('%s%s\n', utpMethodDescription, line);
            line = remComment(fgetl(utpRead));
          end
          temp = strrep(methodDescription, '%%%CONTENT%%%', utpMethodDescription);
          temp = strrep(temp, '%%%HEADER%%%', strrep(filenames{i}, '_', ' '));
          fprintf(outFile, '%s', temp);
        end

        if strfind(line, '<TestDescription>')
          line = fgetl(utpRead);
          utpTestDescription = '';
          while isempty(strfind(line, '</TestDescription>'))
            utpTestDescription = sprintf('%s%s\n', utpTestDescription, line);
            line = remComment(fgetl(utpRead));
          end
          temp = strrep(testDescription, '%%%CONTENT%%%', utpTestDescription);
          temp = strrep(temp, '%%%HEADER%%%', sprintf('%s %2g', strrep(filenames{i}, '_', ' '), headerNumber));
          fprintf(outFile, '%s', temp);
        end

        if strfind(line, '<SyntaxDescription>')
          line = remComment(fgetl(utpRead));
          utpSyntaxDescription = '';
          while isempty(strfind(line, '</SyntaxDescription>'))
            if ~strcmp(strtrim(line), '')
              utpSyntaxDescription = sprintf('%s%s <br />\n', utpSyntaxDescription, line);
            end
            line = remComment(fgetl(utpRead));
          end
          fprintf(outFile, '%s', strrep(syntaxDescription, '%%%CONTENT%%%', utpSyntaxDescription));
        end

        if strfind(line, '<AlgoDescription>')
          line = remComment(fgetl(utpRead));
          utpAlgoDescription = '';
          while isempty(strfind(line, '</AlgoDescription>'))
            if ~strcmp(strtrim(line), '')
              utpAlgoDescription = sprintf('%s%s <br />\n', utpAlgoDescription, line);
            end
            line = remComment(fgetl(utpRead));
          end
          fprintf(outFile, '%s', strrep(algoDescription, '%%%CONTENT%%%', utpAlgoDescription));
        end

        if strfind(line, '<SyntaxCode>')
          line = escComment(fgetl(utpRead));
          % Get number of spaces before string
          
          utpSyntaxCode = '';
          while isempty(strfind(line, '</SyntaxCode>'))
            utpSyntaxCode = sprintf('%s%s\n', utpSyntaxCode, line);
            line = escComment(fgetl(utpRead));
          end
          fprintf(outFile, '%s', strrep(syntaxCode, '%%%CONTENT%%%', utpSyntaxCode));
        end

        if strfind(line, '<AlgoCode>')
          line = escComment(fgetl(utpRead));
          utpAlgoCode = '';
          while isempty(strfind(line, '</AlgoCode>'))
            utpAlgoCode = sprintf('%s%s\n', utpAlgoCode, line);
            line = escComment(fgetl(utpRead));
          end
          fprintf(outFile, '%s', strrep(algoCode, '%%%CONTENT%%%', utpAlgoCode));
          headerNumber = headerNumber + 1;
        end

      end
      fclose(utpRead);
    end

  end

  % Print the outro
  fprintf(outFile, '%s', postTests);

  fclose(outFile);

  report = '';

end


function newString = remComment(string)
  pos = strfind(string, '%');
  newString = strtrim(string(pos+1:length(string)));
end

function newString = escComment(string)
  string = strrep(string, '%', '\%');
  % string = strtrim(string);
  newString = sprintf('%s<br />', string);
end

function newString = escTex(string)
  string = strrep(string, '\', '\\');
  string = strrep(string, '%', '\%');
  string = strrep(string, '_', '\_');
  string = strrep(string, '^', '\^');
  newString = string;
end
