function res  = dumpAggregatedTestSummary(this, cvd, options )
%

%   Copyright 2019 The MathWorks, Inc.
    
    res = false;
    ati = cvd.aggregatedTestInfo;
    if isempty(ati) 
        return;
    end

    msgId = 'Slvnv:simcoverage:cvhtml:AggregatedTests';
    htmlTag = cvi.ReportScript.convertNameToHtmlTag(msgId);
    printIt(this,'<a name="%s"></a><h3>%s</h3>\n', htmlTag, getString(message(msgId)));
   
    [tableData, colSettings] = buildReportStruct(cvd.aggInfoMap, options);
    [tableInfo, tableTemplate] = getTemplate(options, colSettings);
    tableStr = cvprivate( 'html_table', tableData, tableTemplate, tableInfo);
    printIt(this,'%s',tableStr);
    printIt(this,'<br/>\n');
    
    res = true;
end
%====================
function [tableData, colSettings] = buildReportStruct(aggInfoMap, options)

    colSettings.hasModelNameTitle = false;
    colSettings.hasTestCaseName = false;
    colSettings.hasDescription = false;

    tableData = [];
    allKeys = aggInfoMap.keys();
    for idxA = 1 : numel(allKeys)
        modelName = allKeys{idxA};
        aggInfo = aggInfoMap(modelName);
        if contains(modelName, '/') 
            modelName = sprintf('<a name="%s"></a><b>%s: "%s"</b>', 'TBD', getString(message('Slvnv:simcoverage:cvhtml:Subsystem')), modelName);
        else
            modelName = sprintf('<a name="%s"></a><b>%s: "%s"</b>', 'TBD', getString(message('Slvnv:simcoverage:cvhtml:Model')), modelName);
        end
        tmpTableData.modelName = modelName;

        traceData = [];
        for idx = 1 : numel(aggInfo)
            cagi = aggInfo(idx);

            traceLabel = cagi.traceLabel;
            td.runId = sprintf('<a name="ref_trace_%s">%s</a>', traceLabel , traceLabel); 
            if ~isempty(cagi.testRunInfo.runName)
                if isstruct(cagi.testRunInfo.testId)
                    testId = cagi.testRunInfo.testId.uuid;
                    runName = cagi.testRunInfo.runName;
                    contextType = cagi.testRunInfo.testId.contextType;
                else
                    testId = num2str(cagi.testRunInfo.runId);
                    runName = cagi.testRunInfo.runName;
                    contextType = 'ST';
                end
                if strcmpi(contextType, 'RE')
                    contextName = getString(message('Slvnv:simcoverage:cvhtml:OpenTestInContextRE'));
                elseif strcmpi(contextType, 'ST')
                    contextName = getString(message('Slvnv:simcoverage:cvhtml:OpenTestInContextST'));
                end

                td.testCaseName = sprintf('<a title="%s" href="matlab: cvi.ReportUtils.reportContextCallBack(''run'', ''%s'', ''%s'', ''%s'');">%s</a>', ...
                                contextName, testId, contextType, options.topModelName, runName);

                colSettings.hasTestCaseName = true;
            else
                td.testCaseName = 'N/A';
            end
            if ~isempty(cagi.description) 
                colSettings.hasDescription = true;
            end
            td.description = cagi.description;
            %can be mixed empty/non empty
            if isempty(td.description)
                td.description = ' ';
            end 
            td.date = cagi.date;
            if isempty(traceData)
                traceData = td;
            else
                traceData = [traceData td]; %#ok<AGROW>
            end
        end
        tmpTableData.traceData = traceData;
        if isempty(tableData)
            tableData = tmpTableData;
        else
            tableData = [tableData tmpTableData]; %#ok<AGROW>
        end
    end
end
%====================
function [tableInfo, tableTemplate] = getTemplate(options, colSettings)

    tableInfo.table = 'border="1" cellpadding="5" ';
    tableInfo.cols = struct('align','"left"');
    tableInfo.imageDir = options.imageSubDirectory;

    numOfCol = 2;
    tableTitle = {['$<b> ',  getString(message('Slvnv:simcoverage:cvhtml:Run')), ' </b>']};
    tableRows = {{'Cat','#runId'}};
    if colSettings.hasTestCaseName 
        tableTitle =[tableTitle {['$<b> ' ,getString(message('Slvnv:simcoverage:cvhtml:TestName')),  ' </b>']}];
        tableRows = [tableRows {{'Cat','#testCaseName'}}];
        numOfCol = numOfCol + 1;
    end
    if colSettings.hasDescription
        tableTitle =[tableTitle {['$<b> ', getString(message('Slvnv:simcoverage:cvhtml:Description')),  ' </b>']}];
        tableRows = [tableRows {{'Cat','#description'}}];
        numOfCol = numOfCol + 1;
    end

    tableTitle =[tableTitle {['$<b> ' ,getString(message('Slvnv:simcoverage:cvhtml:Date')),  ' </b>']}];
    tableRows = [tableRows {{'Cat','#date'}}];

    tableTemplate = ...
    { ...
       tableTitle{:},...
       '\n' ...
      {'ForEach', '#.',...
            {'CellFormat',  {'Cat','$<b>','#modelName','$</b>'}, numOfCol}, ...
            '\n' ...
            {'ForEach', '#traceData',...
                    tableRows{:}, ....
                  '\n',...
            }...
       }...
    }; %#ok<CCAT>
end