classdef AnnotateFeature < handle
% Base class for all annotation features

% Copyright 2020-2022 The MathWorks, Inc.

properties(GetAccess = public, SetAccess=private)
ExecutionTimeObj;
Models;
end

methods (Access=public, Hidden=true)
function matlabCmd = getReportLinkCmd(~, model)
matlabCmd = sprintf('coder.profile.informerDisplayProfilingReport(''%s'');', model);
end

function matlabCmd = getAppLinkCmd(~, model)
matlabCmd = sprintf('coder.profile.informerDisplayProfilingApp(''%s'');', model);
end
end

properties(GetAccess = protected, SetAccess=private)
% chain of command pattern
Successor;
end

methods (Abstract, Access=protected)
refreshItemImpl(this, itemHandle, lModelAnnotationData, varargin);
destroyImpl(this);
end

methods (Access=public)
function this = AnnotateFeature(executionProfile, models, varargin)
this.ExecutionTimeObj = executionProfile;
this.Models = models;
this.Successor = [];
end

function refreshItem(this, itemHandle, lModelAnnotationData, varargin)
this.refreshItemImpl(itemHandle, lModelAnnotationData, varargin);
if ~isempty(this.Successor)
this.Successor.refreshItem(itemHandle, lModelAnnotationData, varargin);
end
end

function destroy(this)
this.destroyImpl();
if ~isempty(this.Successor)
this.Successor.destroyImpl();
end
end

function setSuccessor(this, successor)
this.Successor = successor;
end

function val = isAnyModelOpenInEditor(this)
% Identify all open models. If models are opened in different
% studios, this returns all open models. With the unified
% editor, this command returns all models that have been opened
% since the top model until the current selection
% @todo update the usage of edit-time filter filterOutInactiveVariantSubsystemChoices(),
% instead use the post-compile filter activeVariants(). g2531677
openModels = bdroot(find_system('MatchFilter', @Simulink.match.internal.filterOutInactiveVariantSubsystemChoices, 'Shown', 'on'));
% Models with profiling data
modelsWithProfilingData = this.Models(:);
for i=1:this.ExecutionTimeObj.getModuleNumber()
modelsWithProfilingData = [modelsWithProfilingData(:)' {this.ExecutionTimeObj.ModelForProfilingTopSettings}];
end
val = ~isempty(intersect(modelsWithProfilingData, openModels));
end
end
end