function adeSet(varargin)
%ADESET Set parameters or variables in a maestro view
%
%   ADESET(paramName="name",paramValue=value) sets the
%   specified parameter. Parameter names must contain at least five
%   non-empty names each separated by a / (slash), typically representing
%   the library/cell/view/instance/property to be modified.
%
%   ADESET(varName="name",varValue=value) sets the specified global 
%   variable. If the variable is not present in the maestro view, a new 
%   variable is created.
%
%   ADESET(varName="name",varValue=value,testName="test") sets the specified 
%   local variable. If the variable is not present in the maestro view, a new 
%   variable is created.
%
%   ADESET(varName={"var1", "var2"}, varValue={varVal1, varVal2}) sets 
%   the values of var1 and var2. Similar syntax can be used for variables 
%   local to a test by providing testName.
%   
%   ADESET(paramName={"library/cell/view/instance/prop1",
%   "library/cell/view/instance/prop2"}, paramValue={paramVal1, paramVal2}) 
%   sets multiple parameter values.

% Copyright 2023 The Mathworks, Inc.
%---------------------------------------------------------------------------

p = inputParser;
defaultStrArg = '';
defaultNumArg = [];
try
    adeInfoLocal = evalin('base','adeInfo');
catch
    error(message('msblks:mixedsignalanalyzer:invalidAdeInfo'));
end
p.addParameter('paramName', defaultStrArg);
p.addParameter('paramValue', defaultNumArg);
p.addParameter('varName', defaultStrArg);
p.addParameter('varValue', defaultNumArg);
p.addParameter('testName', defaultStrArg);
p.addParameter('adeInfo', adeInfoLocal);
parse(p, varargin{:});

adeInfoP = p.Results.adeInfo;
adeSession = adeInfoP.adeSession;

paramNames = p.Results.paramName;
varNames = p.Results.varName;
if ~isempty(paramNames)
    if isempty(p.Results.paramValue)
        error(message('msblks:mixedsignalanalyzer:paramValueEmpty'));
    end
    %%%%% SET PARAMETERS / VARIABLES %%%%%%%%%%%%%%
    ivar = 1;
    for par = string(paramNames)
        if ischar(p.Results.paramValue) 
            % if pval is char it is considered an array and hency it will
            % error out in the next if statement
            pval = string(p.Results.paramValue);
        else
            pval = p.Results.paramValue;
        end
        if numel(pval) == 1
            paramVal = p.Results.paramValue;
        else
            paramVal = p.Results.paramValue{ivar};
        end
        ivar = ivar+1;
        if isnumeric(paramVal)
            paramVal = num2str(paramVal);
        end
        outputArg = msblks.internal.cadence2matlab.setMaestroParameter(par, ...
            paramVal, adeSession);
        if isempty(outputArg) || (outputArg==false)
            error(message('msblks:mixedsignalanalyzer:failureMaestroParameterSet', ...
                par));
        end
    end
    if ~isempty(varNames)
        warning(message('msblks:mixedsignalanalyzer:ignoreVarNameSet'));
    end
elseif ~isempty(varNames)
    varList = msblks.internal.cadence2matlab.listMaestroVariables(adeSession, p.Results.testName);
    if isempty(p.Results.varValue)
        error(message('msblks:mixedsignalanalyzer:varValueEmpty'));
    end
    ivar = 1;
    for var = string(varNames)
        if ischar(p.Results.varValue) 
            % if pval is char it is considered an array and hency it will
            % error out in the next if statement
            vval = string(p.Results.varValue);
        else
            vval = p.Results.varValue;
        end
        if numel(vval) == 1
            varVal = p.Results.varValue;
        else
            varVal = p.Results.varValue{ivar};
        end
        ivar = ivar+1;
        if isnumeric(varVal)
            varVal = num2str(varVal);
        end
        % Warn the user if a new variable is being created
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
        if ~any(contains(varList, var))
            warning(message('msblks:mixedsignalanalyzer:newVariableCreated',var));
        end
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        outputArg = msblks.internal.cadence2matlab.setMaestroVariable(var, ...
            varVal, adeSession, p.Results.testName);
        if isempty(outputArg) || (outputArg==false)
            error(message('msblks:mixedsignalanalyzer:failureMaestroVariableSet', ...
                var));
        end
    end
end

end

