function [mpcpfc,structname] = pfcblock_utilExportDataStruct(blk,modelname,AllNames)
% Utility function of the "Path Following Control (PFC) System" block.
% Called when "Creste PFC subsystem" button is clicked.

% Inputs 
%   modelname: model name for the crated LKA subsystem
%   ALLNames: all names already exist in base workspace
% Outputs
%   mpcpfc: data struct containing all data for PFC block
%   structname: name for the struct containing all data for PFC block


%   Author: Meng Xia, Rong Chen
%   Copyright 2017-2018 The MathWorks, Inc.


%% get mask variables
pfcmask = Simulink.Mask.get(blk);
% WS variable struct
maskwsvars = pfcmask.getWorkspaceVariables;
% turn struct to cell
maskwsvarscell = struct2cell(maskwsvars);
% get variable values
DataType = getVarValue(maskwsvarscell,'DataType');
mpcobj = getVarValue(maskwsvarscell,'mpcobj');
Ts = getVarValue(maskwsvarscell,'Ts');
NumEgoStates = getVarValue(maskwsvarscell,'NumEgoStates');
UseExternalModel = getVarValue(maskwsvarscell,'UseExternalModel');

m = getVarValue(maskwsvarscell,'VehicleMass');
Iz = getVarValue(maskwsvarscell,'VehicleYawInertia');
Cr = getVarValue(maskwsvarscell,'RearTireStiffness');
lr = getVarValue(maskwsvarscell,'LengthToRear');
lf = getVarValue(maskwsvarscell,'LengthToFront');
Cf = getVarValue(maskwsvarscell,'FrontTireStiffness');
lag = getVarValue(maskwsvarscell,'TransportLag');
hasLag = getVarValue(maskwsvarscell,'hasLag');
hasSpacingCtrl = getVarValue(maskwsvarscell,'hasSpacingCtrl');
tau = getVarValue(maskwsvarscell,'AccelTimeConstant');
v0 = getVarValue(maskwsvarscell,'InitialLongVel');
L0 = getVarValue(maskwsvarscell,'DefaultSpacing');

%% create mpcpfc struct 
mpcpfc = struct('DataType',DataType,...
                'Controller',mpcobj,...
                'NumEgoStates',NumEgoStates,...
                'UseExternalModel',UseExternalModel,...
                'VehicleMass',m,...
                'VehicleYawInertia',Iz,...
                'RearTireStiffness',Cr,...
                'LengthToRear',lr,...
                'LengthToFront',lf,...
                'FrontTireStiffness',Cf,...
                'AccelTimeConstant',tau,...
                'InitialLongVel',v0,...
                'DefaultSpacing',L0,...
                'Ts',Ts,...
                'TransportLag',lag,...
                'hasLag',hasLag,...
                'hasSpacingCtrl',hasSpacingCtrl...
                );
            
% optinal blocks/data            
if strcmp(get_param(blk,'amin_ext'),'off')
    amin = getVarValue(maskwsvarscell,'MinAcceleration');
    mpcpfc.('MinAcceleration') = amin;
end
if strcmp(get_param(blk,'amax_ext'),'off')
    amax = getVarValue(maskwsvarscell,'MaxAcceleration');
    mpcpfc.('MaxAcceleration') = amax;
end
if strcmp(get_param(blk,'umin_ext'),'off')
    umin = getVarValue(maskwsvarscell,'MinSteering');
    mpcpfc.('MinSteering') = umin;
end
if strcmp(get_param(blk,'umax_ext'),'off')
    umax = getVarValue(maskwsvarscell,'MaxSteering');
    mpcpfc.('MaxSteering') = umax;
end

% export mpcacc to MATLAB workspace
structname = [modelname '_data'];
structname = matlab.lang.makeUniqueStrings(structname, AllNames);
assignin('base',structname,mpcpfc);

%% local function
function value = getVarValue(maskwsvarscell,needvarname)
% locate needvarname
hasvar = ismember(maskwsvarscell(1,:),needvarname);
index = hasvar;
% get value out
value = maskwsvarscell{2,index};
