classdef (Hidden = true) ExecHelper
    %EXECHELPER contains shared code to generate HTML report for time and
    %stack profiling
    
    %   Copyright 2021-2022 The MathWorks, Inc.
    
    properties (Constant=true, Access=public)
        HistogramIcon = 'histogram_16.png';
        SdiIcon = 'SimulationDataInspector_16.png';
        CodeIcon = 'coder_profile_code_icon.png';
        ResourcesFolder = fullfile(matlabroot, 'toolbox',  'coder', ...
                              'profile', 'resources');
        PartialResourcesFolder = fullfile(filesep, 'toolbox',  'coder', ...
                              'profile', 'resources');
    end
    
    properties (Constant=true, Access=private)
        SdiIconCheckedExistence = false;
    end
    
    methods(Static)
        function htmlStr = formattedHtml(text, matlabCmd, tooltip, noteHtml, additionalName)
            if nargin < 4
                noteHtml = '';
            end
            if nargin < 5
                additionalName = '';
            end

            if ~isempty(tooltip)
                titleStr = sprintf(' title = "%s"', tooltip);
            else
                titleStr = '';
            end
            if ~isempty(matlabCmd)
                hrefStr = sprintf(' href = "matlab:%s"', matlabCmd);
            else
                hrefStr = '';
            end
            if ~isempty(additionalName)
                htmlStr = sprintf('<a%s%s>%s</a> (%s)', titleStr, hrefStr, text, additionalName);
            else
                htmlStr = sprintf('<a%s%s>%s</a>', titleStr, hrefStr, text);
            end
            if ~isempty(noteHtml)
                htmlStr = sprintf('%s %s', htmlStr, noteHtml);
            end 
        end
        
        function lSdiIcon = getSdiIcon
            if ~coder.profile.ExecHelper.SdiIconCheckedExistence
                sdiIconLocaltion = fullfile(...
                    coder.profile.ExecHelper.ResourcesFolder, ...
                    coder.profile.ExecTimeHelper.SdiIcon);
                assert(isfile(sdiIconLocaltion), 'SDI icon must exist');
            end
            lSdiIcon = coder.profile.ExecTimeHelper.SdiIcon;
        end

        function [url, hDlg] = codeHighlight(buildDir, modelName, checksumStr, varargin)

            url = '';
            hDlg = [];
            input = [];
            
            evaluatedChecksumStr = coder.profile.ProbeComponentRegistry ...
                .getTraceabilityChecksumForBuildDir(buildDir);
            
            errorMsg = '';
            if strcmp(evaluatedChecksumStr,  checksumStr)
                cr = simulinkcoder.internal.Report.getInstance;
                if cr.features.profiling
                    src = simulinkcoder.internal.util.getSource(modelName);
                    if ~isempty(src.studio)
                        panel = src.studio.getComponent('GLUE2:DDG Component','CodeView');
                        if ~isempty(panel)
                            src.studio.show;
                            src.studio.showComponent(panel);
                            panel.restore;
                            src.studio.focusComponent(panel);
                            input.title = 'Code Profiling';
                            data = cell([length(varargin{2}) 1]);
                            for i =1:length(varargin{2})
                                data(i) = {struct('file',varargin{1},'line',varargin{2}(i))};
                            end
                            input.data = data;
                            simulinkcoder.internal.util.highlightInCode(modelName, input);
                        else
                            url = coder.internal.slcoderReport('hiliteCode', buildDir, varargin{:});
                        end
                        
                    else
                       url = coder.internal.slcoderReport('hiliteCode', buildDir, varargin{:});
                    end
                    
                else
                    url = coder.internal.slcoderReport('hiliteCode', buildDir, varargin{:});
                end
                
            else
                errorMsg = DAStudio.message...
                    ('CoderProfile:ExecutionTime:ProfilingStaleBuildDir', buildDir);
            end
            
            if ~isempty(errorMsg) 
                errTitle = DAStudio.message...
                    ('CoderProfile:ExecutionTime:CodeReportLinkFailedTitle'); 
                hDlg = errordlg(errorMsg, errTitle, 'modal');
            end
        end
    end

end
