function dlgS = utilGetDialogSettings(blk)
% UTILGETDIALOGSETTINGS utility function that returns a structure
% containing relevant logical checks and 
%
%   blk: the masked subsystem of the PID 1dof or 2dof block

%   Author: A. Zimmerman
%   Copyright 2017-2023 The MathWorks, Inc.

blkObj = get_param(blk,'Object');

%% Dialog Logic Check
UseFilter = blkObj.UseFilter;
dlgS.is2DOF = strcmpi(blkObj.MaskType,'PID 2dof');
dlgS.Controller = blkObj.Controller;
dlgS.Form = pidpack.PIDConfig.utilGetNumericDialogSettings(blk,'Form');
dlgS.TimeDomain = pidpack.PIDConfig.utilGetNumericDialogSettings(blk,'TimeDomain');
dlgS.FilterMethod = pidpack.PIDConfig.utilGetNumericDialogSettings(blk,'FilterMethod');
dlgS.IntegratorMethod = pidpack.PIDConfig.utilGetNumericDialogSettings(blk,'IntegratorMethod');
dlgS.AWMethod = pidpack.PIDConfig.utilGetNumericDialogSettings(blk,'AntiWindupMode');
dlgS.ExternalReset = pidpack.PIDConfig.utilGetNumericDialogSettings(blk,'ExternalReset');
dlgS.ControllerParametersSource = pidpack.PIDConfig.utilGetNumericDialogSettings(blk,'ControllerParametersSource');
dlgS.InitialConditionSource = pidpack.PIDConfig.utilGetNumericDialogSettings(blk,'InitialConditionSource');
dlgS.isDialogUseExternalDerivative = strcmp(blkObj.UseExternalDerivativeSource,'on');
dlgS.isDialogDiscrete = dlgS.TimeDomain ~= 1; % Discrete-time
dlgS.isDialogProportional = contains(dlgS.Controller,'P');
dlgS.isDialogIntegral = contains(dlgS.Controller,'I');
dlgS.isDialogDerivative = contains(dlgS.Controller,'D');
dlgS.isDialogExternal = dlgS.ControllerParametersSource == 2; % External
dlgS.isDialogDiscreteFilter = dlgS.isDialogDerivative && strcmp(UseFilter,'on') && dlgS.isDialogDiscrete;
dlgS.isDialogFilter = dlgS.isDialogDerivative && (strcmp(UseFilter,'on') || dlgS.TimeDomain == 1);
dlgS.isDialogDifferentiator = dlgS.isDialogDerivative && strcmp(UseFilter,'off') && dlgS.isDialogDiscrete && (~dlgS.isDialogUseExternalDerivative);
dlgS.isDialogLimitOutput = strcmp(blkObj.LimitOutput,'on');
dlgS.isDialogUseExternalSatLimits = dlgS.isDialogLimitOutput && (pidpack.PIDConfig.utilGetNumericDialogSettings(blk,'SatLimitsSource')==2);
dlgS.isDialogTracking = dlgS.isDialogIntegral && strcmp(blkObj.TrackingMode,'on');
dlgS.isDialogClamping = dlgS.isDialogIntegral && dlgS.isDialogLimitOutput && dlgS.AWMethod == 3;
dlgS.isDialogBackCalc = dlgS.isDialogIntegral && dlgS.isDialogLimitOutput && dlgS.AWMethod == 2;
dlgS.isDialogExtReset = (dlgS.isDialogIntegral || dlgS.isDialogDerivative) && dlgS.ExternalReset ~= 1;
dlgS.isDialogExtICs = (dlgS.isDialogIntegral || dlgS.isDialogDerivative) && dlgS.InitialConditionSource == 2;
dlgS.isDialogFilterCheckBox = dlgS.isDialogDerivative && dlgS.TimeDomain == 2;
dlgS.isDialogExternalTs = dlgS.isDialogDiscrete && strcmp(blkObj.MaskValues{strcmp(blkObj.MaskNames,'UseExternalTs')},'on');
dlgS.isDialogKiTs = dlgS.isDialogIntegral && strcmp(blkObj.MaskValues{strcmp(blkObj.MaskNames,'UseKiTs')},'on');
dlgS.isAddExternalTsPort = dlgS.isDialogExternalTs && ((dlgS.isDialogIntegral && ~dlgS.isDialogKiTs) || dlgS.isDialogDerivative);
dlgS.isDialogLimitIntegrator = strcmp(blkObj.LimitIntegrator,'on');
