function mpcBlocksSlupdateHelper(h)
% Helper function for SLUPDATE.
% Not intended to be called directly from the command line.

% Author(s): R. Chen
% Copyright 2011-2022 The MathWorks, Inc.

replaceInfo = getReplaceInfo();

context     = getContext(h);
% work our way up from whatever context we get to the model
while strcmpi(get_param(context,'Type'), 'block')
    context = get_param(context,'Parent');
end

for idx=1:size(replaceInfo,1)
    args = replaceInfo(idx).BlockDesc;
    blocks = find_system(context, 'LookUnderMasks', 'all', ...                       
        'MatchFilter', @Simulink.match.allVariants, ... 
        args{:});
    
    % if any are found, call the ReplaceFcn
    for blkIdx=1:numel(blocks)
        feval(replaceInfo(idx).ReplaceFcn, blocks{blkIdx}, h);
    end
    
end

% This function sets up replacement information for the obsolete blocks in
% MPC Toolbox
function ReplaceInfo = getReplaceInfo

ReplaceInfo = struct(...
    'BlockDesc',{{'MaskType','MPC','LinkStatus','none'};...
                 {'MaskType','MPC','LinkStatus','inactive'};...
                 {'MaskType','Adaptive MPC','LinkStatus','none'};...
                 {'MaskType','Adaptive MPC','LinkStatus','inactive'};...
                 {'MaskType','Explicit MPC','LinkStatus','none'};...
                 {'MaskType','Explicit MPC','LinkStatus','inactive'};...
                 {'MaskType','Multiple MPC','LinkStatus','none'};...
                 {'MaskType','Multiple MPC','LinkStatus','inactive'};...
                 {'MaskType','Multiple Explicit MPC','LinkStatus','none'};...
                 {'MaskType','Multiple Explicit MPC','LinkStatus','inactive'}},...
    'ReplaceFcn','ReplaceMPCBlock');


%ReplaceBlock Prompt for update
function ReplaceMPCBlock(block, h)
if askToReplace(h, block)
    load_system('mpclib');
    MaskType = get_param(block,'MaskType');
    switch MaskType
        case 'MPC'
            libblockname = 'mpclib/MPC Controller';
        case 'Adaptive MPC'
            libblockname = 'mpclib/Adaptive MPC Controller';
        case 'Explicit MPC'
            libblockname = 'mpclib/Explicit MPC Controller';
        case 'Multiple MPC'
            libblockname = 'mpclib/Multiple MPC Controllers';
        case 'Multiple Explicit MPC'
            libblockname = 'mpclib/Multiple Explicit MPC Controllers';
    end        
    NewValues = get_param(libblockname,'MaskValues');
    NewNames = get_param(libblockname,'MaskNames');
    OldValues = get_param(block,'MaskValues');
    OldNames = get_param(block,'MaskNames');
%     ObsoleteParameters = {'ref_from_ws','ref_signal_name','ref_preview',...
%         'ref_from_ws_multiple','ref_signal_name_multiple','ref_preview_multiple',...
%         'md_from_ws','md_signal_name','md_preview',...
%         'md_from_ws_multiple','md_signal_name_multiple','md_preview_multiple',...
%         'from_project','project_file','project_name','task_name',...
%         'linswitch','is_designfromscratch'};

    for ct = 1:length(OldNames)
        if strcmp(OldNames{ct},'lims_inport')
            NewValues{strcmp('umin_inport',NewNames)} = OldValues{ct};
            NewValues{strcmp('umax_inport',NewNames)} = OldValues{ct};
            NewValues{strcmp('ymin_inport',NewNames)} = OldValues{ct};
            NewValues{strcmp('ymax_inport',NewNames)} = OldValues{ct};
        elseif strcmp(OldNames{ct},'lims_inport_multiple')
            NewValues{strcmp('umin_inport_multiple',NewNames)} = OldValues{ct};
            NewValues{strcmp('umax_inport_multiple',NewNames)} = OldValues{ct};
            NewValues{strcmp('ymin_inport_multiple',NewNames)} = OldValues{ct};
            NewValues{strcmp('ymax_inport_multiple',NewNames)} = OldValues{ct};
        elseif strcmp(OldNames{ct},'return_sequence')
            NewValues{strcmp('return_mvseq',NewNames)} = OldValues{ct};
        elseif strcmp(OldNames{ct},'cost_outport')
            NewValues{strcmp('return_cost',NewNames)} = OldValues{ct};
        else
            ind = strcmp(OldNames{ct},NewNames);
            if any(ind)
                % ignore obsolete parameters
                NewValues{ind} = OldValues{ct};
            end
        end
    end
    funcSet = uReplaceBlock(h, block,libblockname,'MaskValues',NewValues);
    appendTransaction(h, block, h.ReplaceBlockReasonStr, {funcSet});
end




