function replaceParametricEQFilter(block, h)
%replaceParametricEQFilter Replaces obsolete DSP Parametric EQ Filter block
%with audiofilters Single-Band Parametric EQ block

%   Copyright 2018-2021 The MathWorks, Inc.

if askToReplace(h, block)    
    
    oldValues = get_param(block,'MaskValues');
    CheckEntries(block, oldValues, 18);
    % Specification
    spec = oldValues{1};
    if strcmpi(spec,'Coefficients')
        % Return early if Specification is set to 'Coefficients'
        funcSet = '';
        ReplaceBlockReasonStr = '';
    else        
        funcSet =  {'muReplaceBlock', block};
        if (doUpdate(h))
            muReplaceBlock(block);
        end
        ReplaceBlockReasonStr = getString(message('audio:parametricEQ:ReplaceParametricEQ'));        
    end
        
     if ~isempty(funcSet)
        appendTransaction(h, block, ReplaceBlockReasonStr, {funcSet});
     end
end
end

% Function: CheckEntries ================================================
%
function CheckEntries(block, entriesList, num)
%CHECKENTRIES Check mask entries list for correct number
%

if length(entriesList) ~= num
    error(message('dsp:slupdate:slupdateWrongNumMaskEntries', block));
end

end

% Function: muReplaceBlock ================================================
%
function muReplaceBlock(oldBlock)
%MUREPLACEBLOCK Replace old block with new block

% Load libraries
load_system('audiofilters');
libBlockName = 'audiofilters/Single-Band Parametric EQ';

% The old block's name and parent are needed for the new block
name  = get_param(oldBlock,'Name');
name = strrep(name,'/','//');
parent = get_param(oldBlock,'Parent');

% create a temp block - if everything works fine replace the old block with
% this one.
tempBlock = [parent '/temp'];

%The decorations must be preserved
decorations = getDecorationParams(oldBlock);

% Add the new block
add_block(libBlockName, tempBlock);

% Mask Parameters
mask = getMaskParams(oldBlock);

% Delete old block and replace with new block
delete_block(oldBlock);
add_block(tempBlock, [parent '/' name],decorations{:}, mask{:});
delete_block(tempBlock);
end

% Function: getDecorationParams ================================================
%
function decorations = getDecorationParams(block)
%GETDECORATIONPARAMS Return a cell array with block decorations
%
% Decorations include FontSize, FontWeight, Orientation, etc.
%

decorations = {
    'Position',        [];
    'Orientation',     [];
    'ForegroundColor', [];
    'BackgroundColor', [];
    'DropShadow',      [];
    'NamePlacement',   [];
    'FontName',        [];
    'FontSize',        [];
    'FontWeight',      [];
    'FontAngle',       [];
    'ShowName',        []
    };

for i=1:size(decorations,1)
    decorations{i,2}=get_param(block,decorations{i,1});
end

decorations=reshape(decorations',1,length(decorations(:)));

end

% Function: getMaskParams ================================================
%
function mask = getMaskParams(block)
%GETMASKPARAMS Return a cell array with old block mask parameters
%

oldValues = get_param(block,'MaskValues');

spec      = oldValues{1};
bwPort    = oldValues{2};
bwVal     = oldValues{3};
cfPort    = oldValues{4};
cfVal     = oldValues{5};
gPort     = oldValues{8};
gVal      = oldValues{9};
qPort     = oldValues{12};
qVal      = oldValues{13};
fsInherit = oldValues{16};
fsVal     = oldValues{17};
simUsing  = oldValues{18};

mask = {'Specification', spec, ...
        'CenterFrequencyPort', cfPort, ...
        'CenterFrequency', cfVal, ...
        'BandwidthPort', bwPort, ...
        'Bandwidth', bwVal, ...
        'QualityFactorPort', qPort, ...
        'QualityFactor', qVal, ...
        'PeakGaindBPort', gPort, ...
        'PeakGaindB',gVal, ...
        'InheritSampleRate', fsInherit, ...
        'SampleRate', fsVal, ...
        'SimulateUsing', simUsing};
    
 mask=reshape(mask',1,length(mask(:)));   
end