function [cando, msg] = setobject(this, object, hdlfilt)
%SETOBJECT   Set function for possible object update.

%   Author(s): J. Sun
%   Copyright 2009-2023 The MathWorks, Inc.
%     


cando = true;
msg = '';

this.hdlfilt = hdlfilt;
this.filterObj = object;
this.ArchitectureEntries = getArchitectureEntries(this, object);

% nothing to do for single coefficient for FIR
try
    if length(hdlfilt.Coefficients) == 1
        return;
    end
catch ME %#ok<NASGU> % no need to do anything
end

if getEnableState(this, 'Architecture')    
    try % for now we use try/catch to determine if it's supported, backend will provide an API to handle non-supported cases
        % for fully parallel
        % get fully serial partition default values:
        if  fdhdlcoderui.issupportedsos(object)
            [ff,mult] =  hdlfilterserialinfo(object, 'foldingfactor', 1);
            sp = -1;
        else
            [sp, ff, mult] = hdlfilterserialinfo(object, 'serialpart', -1);
        end
        this.FPSerialPartition = hdlfilt.convSerialPart2String(sp);
        this.FPFoldingFactor = num2str(ff);
        this.FPMultipliers = num2str(mult);
                
        serial = hdlfilt.isSerialSupported;         
        if serial.fullyserial || serial.partlyserial || serial.cascadeserial
            [splist, fflist, mullist] = getlistforcomboboxs(hdlfilt);
            this.SerialPartitionEntry = splist';
            this.FoldingFactorEntry = fflist';
            this.MultipliersEntry = mullist';

             if serial.partlyserial
                % get serial partition default values:
                [sp, ff, mult] = getSpecificSerialInfo(hdlfilt, 'Multipliers', 2);
                this.SerialPartition = hdlfilt.convSerialPart2String(sp);%['[', num2str(sp), ']'];
                this.FoldingFactor = num2str(ff);
                this.Multipliers = num2str(mult);
             end

             if serial.fullyserial
                % get fully serial partition default values:
                [sp, ff, mult] = getSpecificSerialInfo(hdlfilt, 'Multipliers', 1);
                this.FSSerialPartition = hdlfilt.convSerialPart2String(sp);%['[', num2str(sp), ']'];
                this.FSFoldingFactor = num2str(ff);
                this.FSMultipliers = num2str(mult);
             end        
        end
    catch 
        this.SerialPartitionEntry = {' '};
        this.FoldingFactorEntry = {' '};
        this.MultipliersEntry = {' '};
    end    
    
    if ~isempty(findSupportedArchit(this, 'DA')) 
            % Get the DA related entries
            [ff, daradix, lutinput, lutpatition] = hdlfilterdainfo(object);
            
            % Array of strings for the entries
            this.LUTPartitionEntries = lutpatition;
            this.LUTInputsEntries = lutinput;            
            this.DARadixEntries = daradix;
            this.FoldingFactorEntries = ff;
            
            % get default values
            LUTInputs = min(6,max(str2double(this.LUTInputsEntries)));
            foldingfactor = max(str2double(this.FoldingFactorEntries));
            [lutpartition, daadix, this.DALUTSize, foldingfactor] = hdlfilterdainfo(object, 'LUTInputs', LUTInputs, 'foldingfactor', foldingfactor);
            % DARadix syntax has problem!!
            % Assign the defaults - must convert to strings
            this.LUTInputsValue = num2str(LUTInputs);
            this.DAFoldingFactorValue = num2str(foldingfactor);
            daadix = ['2^', num2str(log2(daadix))];
            this.DARadixValue = daadix;
            this.LUTPartitionValue = mat2str(lutpartition);                
    end
    
    this.CascadeSerialPartition = getCascadeSerialPartition(object);
    
end
% [EOF]

%--------------------------------------------------------------------------
function val = getCascadeSerialPartition(filtobj)
   
coeffs = coefficients(filtobj);
coeffs = coeffs{1};

if isa(filtobj, 'dfilt.dfsymfir') || isa(filtobj, 'dfilt.dfasymfir')
    %   For symmetric FIR filter - the actual length should be (the length of
    %   non-zero coeffs)/2.
    FL = ceil(length(coeffs(coeffs ~= 0))/2);
else
    FL = length(coeffs(coeffs ~= 0));
end
val = num2str([ceil(FL/2) floor(FL/2)]);

%--------------------------------------------------------------------------
function val = getArchitectureEntries(this, filterObj)

firfilter = fdhdlcoderui.isfirfilter(filterObj);
multiratefir = fdhdlcoderui.ismultiratefir(filterObj);
supportedsos = fdhdlcoderui.issupportedsos(filterObj);

if ~firfilter && ~multiratefir && ~supportedsos
    val = {'Fully parallel'};
elseif supportedsos
    val = {'Fully parallel',...
        findSupportedArchit(this, 'FullySerial'), ...
        findSupportedArchit(this, 'PartlySerial')};
else
    val = {'Fully parallel',...
        findSupportedArchit(this, 'FullySerial'), ...
        findSupportedArchit(this, 'PartlySerial'), ...
        findSupportedArchit(this, 'CascadeSerial'), ...
        findSupportedArchit(this, 'DA')};        
end

% set the Architecture to first element if current selection does
% not apply. This is the case when filter changed.
for i=1:length(val)
    if strcmpi(val{i}, this.Architecture)
        return;
    end
end

this.Architecture = val{1};
%-------------------------------------------------------------
function  [splist, fflist, mullist] = getlistforcomboboxs(hdlfilt)

if isa(hdlfilt, 'hdlfilter.df1sos') || isa(hdlfilt, 'hdlfilter.df2sos')
    [fflist, mullist] = getSerialPartition(hdlfilt);
    splist = {};
else
    [splist, fflist, mullist] = getSerialPartition(hdlfilt);
end
%-------------------------------------------------------------
function  [sp, ff, mul] = getSpecificSerialInfo(hdlfilt, prop, val)

if isa(hdlfilt, 'hdlfilter.df1sos') || isa(hdlfilt, 'hdlfilter.df2sos')
    [ff, mul] = getSerialPartition(hdlfilt, prop, val);
    sp = 0; % place holder
else
    [sp, ff, mul] = getSerialPartition(hdlfilt, prop, val);
end

% function 
% NO OP

% LocalWords:  serialpart sp df
