function addedNewConstraints = refreshModelParams(this,varargin) 
% Search for parameters in model. Merge the list of obtained parameters
% into the current parameter list in Manager.
% addedNewConstraints  is true when constraint is generated for at least one
% parameter.

%   Copyright 2013-2021 The MathWorks, Inc.
    if nargin >= 2
        issueWarnings = varargin{1};
        if nargin == 3
            codeLocation = varargin{2};
        end
    else
        issueWarnings = true;
    end
    this.clearModelParams();
    analyzeCodeForParameters = false;

    % Here third output arg is all the param vars we get with
    % Simulink.findVars
    if nargin == 3
        try
            % This code uses Simulink.findVars to detect all the parameters
            % in the model, filters all the parameters we are not tuning
            % and returns the rest. This process also includes filtering all
            % the parameters  that are not tunable in code when the
            % testgeneration target is code.
            [status, tunableParamList, ~] = Sldv.ParameterTuning.Manager.getParamsFromModel(this.HModel, codeLocation);
            analyzeCodeForParameters = true;
        catch Mex
            % The above call in the try block could fail for various
            % reasons. Let's consider a few to understand what we are doing
            % here.
            % 1. code generation folder is empty: User has either not
            %           generated the code or the code is not stored in the right
            %           directory.
            % 2.Embedded coder license issue: User may not have an embedded
            %           coder license.
            % 
            % If the user is calling this function through the configset UI,
            % the intent here is to not produce a warning on the
            % diagnostics viewer or the command prompt. But, to prompt the
            % user to take the right action through a dialog window. So, we
            % take appropriate action based on the user workflow.
            addedNewConstraints = [];
            if ~issueWarnings
                rethrow(Mex);
            end
            warnMsg = Mex.message;
            sldvshareprivate('local_warning_dlg', warnMsg);
            return;
        end
    else
        [status, tunableParamList, ~] = Sldv.ParameterTuning.Manager.getParamsFromModel(this.HModel);
    end
    pnames = fieldnames(tunableParamList);
    % Inform the user when the model does not have any parameters.
    if isempty(pnames) && status && issueWarnings 
        mname = get_param(this.HModel, 'Name');
        warnmsg = getString(message('Sldv:Parameters:NoParamsInModel', mname));
        sldvshareprivate('local_warning_dlg', warnmsg); % This is really an info to the user.
    elseif isempty(pnames) && status && ~issueWarnings
        addedNewConstraints = [];
        return;
    end
    for idx = 1: length(pnames)
        paramValue = tunableParamList.(pnames{idx});
        pName = pnames{idx};
        this.IsInModel = this.mergeAndAssignParam(paramValue, pName, this.IsInModel);
        if analyzeCodeForParameters
            % All parameters that are tunable in code are marked to be used
            % in analysis.
            this.UseInAnalysis(pName) = true;
        end
    end
    addedNewConstraints = this.generateConstraintsForParams;
end

% LocalWords:  dlg testgeneration