function utilStateAttributes(blk,dlgSett, varargin)
% UTILSTATEATTRIBUTES Returns the PID mask enable and visible settings.
% Function updates the enable and visible settings for the PID Block State
% Attributes tab.

%   Author(s): Aaron Zimmerman
%   Copyright 2017-2020 The MathWorks, Inc.

if nargin > 2.5
    Prefix = varargin{1};
else
    Prefix = [];
end

% Get entries of comboboxes/radioButtons
blkObj = get_param(blk, 'Object');
maskObj = get_param(blk,'MaskObject');
RTWEntries_Integrator = blkObj.getPropAllowedValues('IntegratorRTWStateStorageClass');
RTWEntries_Filter = blkObj.getPropAllowedValues('FilterRTWStateStorageClass');

if isempty(Prefix) || strcmp(Prefix,'Integrator')
    % Integrator
    localSetParams(blkObj, maskObj, dlgSett, RTWEntries_Integrator,'Integrator');
end

if isempty(Prefix) || strcmp(Prefix,'Filter')
    % Filter
    localSetParams(blkObj, maskObj, dlgSett, RTWEntries_Filter,'Filter');
end

end

function localSetParams(blkObj, maskObj, dlgSett, RTWEntries, Prefix)

    if strcmp(Prefix,'Integrator')
        isIncluded = dlgSett.isDialogIntegral;
    else
        isIncluded = dlgSett.isDialogFilter;
    end

    % Update values so that the behavior is consistent with the
    % discrete-integrator block
    f1 = ~isempty(blkObj.([Prefix 'StateIdentifier']));
    if ~f1
        blkObj.([Prefix 'StateMustResolveToSignalObject']) = false;
    end
    
    % State Storage is deprecated, make sure values are empty
    blkObj.([Prefix 'RTWStateStorageClass']) = RTWEntries{1};
    blkObj.([Prefix 'RTWStateStorageTypeQualifier']) = '';

    isEnabled = ~blkObj.isHierarchySimulating;
    statename.Enabled       = isIncluded && ~dlgSett.isDialogDiscrete;
    stateidentifier.Enabled = isIncluded && dlgSett.isDialogDiscrete;
    stateresolve.Enabled = isIncluded && dlgSett.isDialogDiscrete && isEnabled && f1;   % Consider stateresolve_I.Enabled = stateresolve_I.Enabled && f1   because of creatWidget

    statename.Visible       = isIncluded && ~dlgSett.isDialogDiscrete;
    stateidentifier.Visible = isIncluded && dlgSett.isDialogDiscrete;
    stateresolve.Visible    = isIncluded && dlgSett.isDialogDiscrete;

    % Continuous State
    param = [Prefix 'ContinuousStateAttributes'];
    enable = statename.Enabled;
    visible = statename.Visible;
    localSetParamEnableVisible(maskObj,param,enable,visible);

    % Discrete State
    param = [Prefix 'StateIdentifier'];
    enable = stateidentifier.Enabled;
    visible = stateidentifier.Visible;
    localSetParamEnableVisible(maskObj,param,enable,visible);

    % Discrete State Resolve
    param = [Prefix 'StateMustResolveToSignalObject'];
    enable = stateresolve.Enabled;
    visible = stateresolve.Visible;
    localSetParamEnableVisible(maskObj,param,enable,visible);

    % Discrete RTW Class
    param = [Prefix 'RTWStateStorageClass'];
    enable = false;
    visible = false;
    localSetParamEnableVisible(maskObj,param,enable,visible);

    % Discrete RTW Qualifier
    param = [Prefix 'RTWStateStorageTypeQualifier'];
    enable = false;
    visible = false;
    localSetParamEnableVisible(maskObj,param,enable,visible);


end

function localSetParamEnableVisible(maskObj,param,enable,visible)

    if enable
        se = 'on';
    else
        se = 'off';
    end
    if visible
        sv = 'on';
    else
        sv = 'off';
    end
    
    % Only update when necessary
    aMaskParameter = maskObj.getParameter(param);
    if ~strcmp(aMaskParameter.Enabled,se)
        aMaskParameter.Enabled = se;
    end
    if ~strcmp(aMaskParameter.Visible,sv)
        aMaskParameter.Visible = sv;
    end
end
