function vppblock_utilParameterCheck(blk)
% Check mask dialog parameters for Vehicle Path Planner (VPP) Block.

% Copyright 2022 The MathWorks, Inc.

%% get parameter values from mask workspace
maskWSVars = get_param(blk,'MaskWSVariables');
blkWSVarNames = {maskWSVars.Name};

egoLength = maskWSVars(strcmp(blkWSVarNames,'egoLength')).Value;
egoWidth = maskWSVars(strcmp(blkWSVarNames,'egoWidth')).Value;
wheelbase = maskWSVars(strcmp(blkWSVarNames,'wheelbase')).Value;
dist2Center = maskWSVars(strcmp(blkWSVarNames,'dist2Center')).Value;
Ts = maskWSVars(strcmp(blkWSVarNames,'Ts')).Value;
pPlanning = maskWSVars(strcmp(blkWSVarNames,'pPlanning')).Value;
steer_range = maskWSVars(strcmp(blkWSVarNames,'steer_range')).Value;
v_range = maskWSVars(strcmp(blkWSVarNames,'v_range')).Value;
numObs = maskWSVars(strcmp(blkWSVarNames,'numObs')).Value;
obsMat = maskWSVars(strcmp(blkWSVarNames,'obsMat')).Value;
maxiter = maskWSVars(strcmp(blkWSVarNames,'maxiter')).Value;
minDist = maskWSVars(strcmp(blkWSVarNames,'minDist')).Value;
%% get default status of checkboxes
v_ext = strcmp(get_param(blk,'v_ext'),'on');
steer_ext = strcmp(get_param(blk,'steer_ext'),'on');
obs_ext = strcmp(get_param(blk,'obs_ext'),'on');
suboptimal = strcmp(get_param(blk,'suboptimal'),'off');



%% check parameters
% integer
if ~suboptimal && ~isempty(maxiter)
    checkPositiveInteger(maxiter,'MPC:adasblocks:errCPPMaxIter')
end
checkPositiveInteger(pPlanning,'MPC:adasblocks:errCPPSteps')
checkNonnegativeInteger(numObs,'MPC:adasblocks:errCPPNumObs')

% positive
checkPositiveScalar(Ts,'MPC:adasblocks:errCPPSampleTime')
checkPositiveScalar(egoLength,'MPC:adasblocks:errCPPLength')
checkPositiveScalar(egoWidth,'MPC:adasblocks:errCPPWidth')
checkPositiveScalar(wheelbase,'MPC:adasblocks:errCPPWheelbase')
checkPositiveScalar(dist2Center,'MPC:adasblocks:errCPPCenterToRear')

if numObs>0
    checkPositiveScalar(minDist,'MPC:adasblocks:errCPPMinDist')
end

% vector
if ~v_ext
    checkRangeVector(v_range,2,'MPC:adasblocks:errCPPVelocityRange')
end
if ~steer_ext
    checkRangeVector(steer_range,2,'MPC:adasblocks:errCPPSteerRange')
end

% matrix
if numObs>0 && ~obs_ext
    checkObsMat(obsMat,numObs,5,'MPC:adasblocks:errCPPObsList')
end

% check less than:
if wheelbase>egoLength
    error(message('MPC:adasblocks:errCPPwheelbaseLength'))
end
if dist2Center>wheelbase
    error(message('MPC:adasblocks:errCPPwheelbaseCenter'))
end





%% local functions for error checking
function checkPositiveInteger(var,msgid)
isvalid = checkValidScalar(var); 
if ~(isvalid && (var>0) && (var==round(var)))
    error((message(msgid)))
end

function checkNonnegativeInteger(var,msgid)
isvalid = checkValidScalar(var); 
if ~(isvalid && (var>=0) && (var==round(var)))
    error((message(msgid)))
end

function checkPositiveScalar(var,msgid)
isvalid = checkValidScalar(var);
if ~(isvalid && (var>0))
    error((message(msgid)))
end

function checkRangeVector(var,nc,msgid)
isValid = checkValidVector(var,nc);
if ~(isValid && var(1)<var(2))
    error((message(msgid)))
end 

function checkObsMat(var,nrow,ncol,msgid)
isValid = checkValidMatrix(var,nrow,ncol);
if ~isValid 
    error((message(msgid)))
end 


% double, real, finite, scalar
function isValid = checkValidScalar(var)
isValid = isa(var,'double') && isscalar(var) && isreal(var) && isfinite(var);
% double, real, finite, column, vector
function isValid = checkValidVector(var,nc)
isValid = isa(var,'double') && isreal(var) && all(isfinite(var)) && isrow(var) && size(var,2)==nc;
% double, real, finite, matrix
function isValid = checkValidMatrix(var,nrow,ncol)
isValid = isa(var,'double') && isreal(var) && all(isfinite(var(:))) && ismatrix(var)...
    && size(var,1)==nrow && size(var,2)==ncol;









    
