classdef Informer < coder.profile.AnnotateFeature
    % Manage the execution profiling informer window
    
    %   Copyright 2015-2022 The MathWorks, Inc.
    
    properties(Access = private)
        InformerWindow;
    end
    
    methods (Access=public)
        function this = Informer(executionProfile, models, varargin)
            this = this@coder.profile.AnnotateFeature(executionProfile, models);
            
            % If no block diagram is open, do not create an informer window
            if ~this.isAnyModelOpenInEditor()
                this.InformerWindow = [];
                return;
            end
            
            % Create the informer window
            lInformerWindow = DAStudio.Informer;
            lInformerWindow.mode = 'ClickMode';
            defaultInformerPosition = [10 600 600 400];
            lInformerWindow.position = defaultInformerPosition;
            if this.ExecutionTimeObj.getModuleNumber() > 1
                lSimulationComponent = this.ExecutionTimeObj.ModelForProfilingTopSettings;
            else
                lSimulationComponent = this.ExecutionTimeObj.SimulationComponent;
            end
            lMsgTitle = DAStudio.message('CoderProfile:ExecutionTimeInformer:etiInformerTitle');
            lInformerWindow.title = sprintf('%s %s', lMsgTitle, lSimulationComponent);
            lInformerWindow.preCloseFcn = ...
                ['coder.profile.AnnotateManager.tearDownInformer' ...
                '(''' this.ExecutionTimeObj.SimulationComponent ''');'];
            lInformerWindow.defaultText = this.getDefaultInformerText;
            this.InformerWindow = lInformerWindow;
        end
    end
    
    methods (Access=protected)
        function refreshItemImpl(this, itemHandle, lModelAnnotationData, varargin)
            % Apply styling to the model and populate the informer with corresponding
            % annotations
            if isempty(this.InformerWindow)
                return;
            end
            lHtml = coder.profile.getHTMLForInformer(itemHandle, lModelAnnotationData, this.ExecutionTimeObj);
            this.InformerWindow.mapData(itemHandle, lHtml);
        end

        function destroyImpl(~)
        end
    end
    
    methods (Access=private)
        function lDefaultInformerText = getDefaultInformerText(this)
            lSimulationComponent = this.ExecutionTimeObj.SimulationComponent;
            matlabCmd = this.getReportLinkCmd(lSimulationComponent);
            lReportLinkCmd = sprintf('matlab:%s', matlabCmd);
            % generate html for the default view
            lTooltip = DAStudio.message...
                ('CoderProfile:ExecutionTimeInformer:etiReportTooltip');

            lAppTooltip = DAStudio.message('CoderProfile:App:AppLinkTooltip');
            lAppLinkCmd = sprintf('matlab:%s', this.getAppLinkCmd(lSimulationComponent));
            msgBody = DAStudio.message(...
                'CoderProfile:ExecutionTimeInformer:etiDefaultTextBodyExtended', ...
                lTooltip, lReportLinkCmd, lAppTooltip, lAppLinkCmd);

            lDefaultInformerText = sprintf...
                (['<table align=center width=80%%>' ...
                '<tr><td></td></tr>' ...
                '<tr><td></td></tr>' ...
                '<tr><td align=center><b>%s</b></td></tr>' ...
                '<tr><td></td></tr>' ...
                '<tr><td>%s</td></tr>' ...
                '</table>'], ...
                DAStudio.message('CoderProfile:ExecutionTimeInformer:etiDefaultTextHeading'), ...
                msgBody);
        end
    end
end
