classdef HTMLReviewReportDocument < mpcreview.BaseReport
    %HTMLREVIEWREPORTDOCUMENT Creates a report for the @mpc object.
    % HTMLReviewReportDocument inherits BaseReportclass which contains
    % HTML and CSS templates for the report.

    %   Author: Eri Gualter
    %   Copyright 2022 MathWorks, Inc.

    properties
        ReportData
    end

    methods
        function obj = HTMLReviewReportDocument(resultsObj, reportType)
            %HTMLREVIEWREPORTDOCUMENT Constructor
            %   Object inherits mlreportgen.dom.DocumentPart class to
            %   generate an HTML report based on templates in a template
            %   library

            import mlreportgen.dom.*
            import mlreportgen.utils.*

            if nargin == 0
                resultsObj = [];
            elseif nargin == 1
                % g2795752 - "rptview is failing for 'html' in MATLAB Online".
                reportType = 'html-file';
            end

            % Define location of the generated report file
            outputPath = fullfile(tempdir,'index');

            % Call BaseReport constructor
            obj = obj@mpcreview.BaseReport(outputPath, reportType);
            
            % Update Location of HTML Template
            obj.TemplatePath = obj.getDefaultTemplatePath;

            if isempty(resultsObj)
                return;
            else
                obj.ReportData = resultsObj;
            end

            %% Add Summary Table
            summaryTableObj = mpcreview.HTMLAppendSummaryTable(obj);
            summaryTableObj.fill();
            obj.append(summaryTableObj);
            
            %% Append 'Individual Test Result' Title
            mpcreview.HTMLTag.appendHeading(obj, mpcreview.EnumTag.AddHeading1, ...
                obj.ReportData.ReportTitles.IndividualSubTitle);

            %% Append Test Content Details
            for it = 1:9
                % Create a HTMLTestDetailsPart object for each test
                testHeader = mpcreview.HTMLTestDetailsPart(obj, ...
                    obj.ReportData.Test(it), ...
                    mpcreview.EnumTag.AddHeading3, ...
                    summaryTableObj.TestLinkID{it});

                testHeader.fill();
                obj.append(testHeader);

                % Only generate full report if MPC initialization Pass
                if obj.ReportData.Test(1).Status == mpcreview.EnumStatus.Fail
                    break;
                end
            end
        end

        function openReport(obj)
            %OPENREPORT open report 
            obj.close();
            
            % Launch report using web, instead of rptview function
            if license('test','MATLAB_Report_Gen')
                rptview(obj.OutputPath)
            else
                web(obj.OutputPath,'-new','-noaddressbox')
            end

            % % Display where report has been saved if verbosity level is on
            % if mpccheckverbose
            %     reportFile = obj.OutputPath;
            %     openWebCmd = sprintf('web(''%s'',''-new'')', strrep(reportFile,'''',''''''));
            %     displayCmd = sprintf('<a href = "matlab:%s">%s</a>\n', openWebCmd, reportFile);
            %     disp(getString(message('MPC:object:ReviewReportSaved', displayCmd)));
            % end
        end
    end
end
