function [plist, use] = getParamsFromConfig(this)
%

%   Copyright 2013-2022 The MathWorks, Inc.

plist = [];

% variable 'use' is an array of structures if the feature flag 'BusParameterTuning' is turned on
% Each struct will consist of the following fields
% name : Name of the parameter
% value: 0/1 depending on whether the parameter is used in analysis or not

pnames = this.Config.get_param([this.ProductTag 'ParameterNames']);
pconstraints = this.Config.get_param([this.ProductTag 'ParameterConstraints']);
checks = this.Config.get_param([this.ProductTag 'ParameterUseInAnalysis']);

if slavteng('feature','BusParameterTuning')
    % Initialize the use array of structs
    basicStruct = struct();
    use(1:length(pnames)) = basicStruct;
else
    use = [];
end
% These properties can be empty, otherwise they should be cell arrays.
if ~(isempty(pnames) || iscell(pnames))
    return;
end
if ~(isempty(pconstraints) || iscell(pconstraints))
    return;
end
if ~(isempty(checks) || iscell(checks))
    return;
end

% If all was ok when creating configuration the pnames and pconstraints
% should be of the same size and all the constraints should be well-formed
% so the eval should pass. If for some reason that is not the case, I will
% take those constraints as empty i.e. free parameter. and throw a
% warning.
warn = false;
for idx = 1: length(pnames)
    if ~isempty(pnames{idx})
        % g1820928 : Test for parameter name being empty - special case
        % where DVParameterNames has the value {[]}
        
        if ~(length(pconstraints) < idx)
            p = Sldv.ParameterTuning.Param(pnames{idx},...
                'Constraint', pconstraints{idx});
        else
            p = Sldv.ParameterTuning.Param(pnames{idx},'Constraint', '[]');
            warn = true;
        end
        if slavteng('feature','BusParameterTuning')
            use(idx).name = pnames{idx};
            % plist is configured using eval directly
            eval(['plist.' pnames{idx} ' = p;']); %#ok<EVLDOT> 
            % % If the checkbox property is on the select the parameter for use in analysis.
            % If the length of checkbox property does not match the length of
            % parameter names, we assume that the parameter is turned off and warn
            % the user.
            if ~(length(checks)< idx) && isCheckTurnedOn(checks{idx})
                use(idx).value = 1;
            elseif  ~(length(checks)< idx) && ~isCheckTurnedOn(checks{idx})
                use(idx).value = 0;
            else
                warn = true;
            end
        else
            plist.(pnames{idx}) = p;
            % If the checkbox property is on the select the parameter for use in analysis.
            % If the length of checkbox property does not match the length of
            % parameter names, we assume that the parameter is turned off and warn
            % the user.
            if ~(length(checks)< idx) 
                if isCheckTurnedOn(checks{idx})
                    use.(pnames{idx}) = 1;
                else
                    use.(pnames{idx}) = 0;
                end
            else
                warn = true;
            end
        end
        
    end
end
if warn
    sldvshareprivate('local_warning_dlg', getString(message('Sldv:Parameters:NoConstraintSet')));
end
end

function turnedOnBool = isCheckTurnedOn(checkValue)
    turnedOnBool = false;
    if islogical(checkValue)
        turnedOnBool = checkValue;
    elseif ischar(checkValue)
        turnedOnBool = strcmp(checkValue, 'on');
    end
end

% LocalWords:  pconstraints plist dlg