function commdigbbndpm2ReplaceBlocks(block, muObj)
    % commdigbbndpm2ReplaceBlocks Replace blocks in commdigbbndpm2 with
    % corresponding blocks in commdigbbndpm3 library.
    %
    %  Input arguments:
    %    block - handle to the block being updated
    %    muObj - model updater object
    
    % Copyright 2018 The MathWorks, Inc.

    if askToReplace(muObj, block)
        instanceData = get_param(block, 'InstanceData');
        isMod = false;
        switch get_param(block, 'SourceType')
            case 'BPSK Modulator Baseband'
                isMod = true;
                newBlock = 'commdigbbndpm3/BPSK Modulator Baseband';
                oldParamNames = {'Ph'};
                newParamNames = {'Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'BPSK Demodulator Baseband'
                newBlock = 'commdigbbndpm3/BPSK Demodulator Baseband';
                oldParamNames = {'Ph'};
                newParamNames = {'Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'QPSK Modulator Baseband'
                isMod = true;
                newBlock = 'commdigbbndpm3/QPSK Modulator Baseband';
                oldParamNames = {'InType','Enc','Ph'};
                newParamNames = {'InType','Enc','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'QPSK Demodulator Baseband'
                newBlock = 'commdigbbndpm3/QPSK Demodulator Baseband';
                oldParamNames = {'OutType','Dec','Ph'};
                newParamNames = {'OutType','Dec','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'M-PSK Modulator Baseband'
                isMod = true;
                newBlock = 'commdigbbndpm3/M-PSK Modulator Baseband';
                oldParamNames = {'M','InType','Enc','Ph'};
                newParamNames = {'M','InType','Enc','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'M-PSK Demodulator Baseband'
                newBlock = 'commdigbbndpm3/M-PSK Demodulator Baseband';
                oldParamNames = {'M','OutType','Dec','Ph'};
                newParamNames = {'M','OutType','Dec','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'OQPSK Modulator Baseband'
                isMod = true;
                newBlock = 'commdigbbndpm3/OQPSK Modulator Baseband';
                oldParamNames = {'InType','Ph'};
                newParamNames = {'InType','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'OQPSK Demodulator Baseband'
                newBlock = 'commdigbbndpm3/OQPSK Demodulator Baseband';
                oldParamNames = {'OutType','Ph'};
                newParamNames = {'OutType','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'DBPSK Modulator Baseband'
                isMod = true;
                newBlock = 'commdigbbndpm3/DBPSK Modulator Baseband';
                oldParamNames = {'M','InType','Enc','Ph'};
                newParamNames = {'M','InType','Enc','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'DBPSK Demodulator Baseband'
                newBlock = 'commdigbbndpm3/DBPSK Demodulator Baseband';
                oldParamNames = {'M','OutType','Dec','Ph'};
                newParamNames = {'M','OutType','Dec','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'DQPSK Modulator Baseband'
                isMod = true;
                newBlock = 'commdigbbndpm3/DQPSK Modulator Baseband';
                oldParamNames = {'InType','Enc','Ph'};
                newParamNames = {'InType','Enc','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'DQPSK Demodulator Baseband'
                newBlock = 'commdigbbndpm3/DQPSK Demodulator Baseband';
                oldParamNames = {'OutType','Dec','Ph'};
                newParamNames = {'OutType','Dec','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'M-DPSK Modulator Baseband'
                isMod = true;
                newBlock = 'commdigbbndpm3/M-DPSK Modulator Baseband';
                oldParamNames = {'M','InType','Enc','Ph'};
                newParamNames = {'M','InType','Enc','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
            case 'M-DPSK Demodulator Baseband'
                newBlock = 'commdigbbndpm3/M-DPSK Demodulator Baseband';
                oldParamNames = {'M','OutType','Dec','Ph'};
                newParamNames = {'M','OutType','Dec','Ph'};
                [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames);
        end
        
        funcSet = uReplaceBlock(muObj, block, newBlock, ...
            newParamNameValue{:});
        if isNumSampNotOne
            reasonStr = 'Replace old block with new block';
        else
            reasonStr = ['Replace old block with new block. Old block ' ...
                'uses ''Samples per symbol'' parameter which is not ' ...
                'supported in new block.'];
            if isMod
                reasonStr = [reasonStr ' Consider using Repeat block' ...
                    ' after this block to achieve same functionality'];                
            else
                reasonStr = [reasonStr ' Consider using Derepeat block' ...
                    ' before this block to achieve same functionality'];
            end
        end       

       appendTransaction(muObj, block, reasonStr, {funcSet}); 
    end
end

function [newParamNameValue, isNumSampNotOne] = paramMapper(instanceData, oldParamNames, newParamNames)
    % Map parameters in old block to that in new block. Also, detect if the
    % parameter 'numSamp' in the old block is other than 1. New block does
    % not support/have 'numSamp' parameter.
    
    newParamNameValue = cell(0);
    isNumSampNotOne = false;
    for idx=1:length(instanceData)        
        paramIdx = find(strcmpi(instanceData(idx).Name, oldParamNames),1);
        if ~isempty(paramIdx)
            newParamNameValue(end+(1:2)) = {newParamNames{paramIdx}, instanceData(idx).Value};
        elseif strcmpi(instanceData(idx).Name, 'numSamp')
            isNumSampNotOne = strcmp(instanceData(idx).Value, '1');
        end
    end    
end