function [success,msg] = dialogCallback(this, hDlg, parent, action)  %#ok
%DIALOGCALLBACK - Callback for the FDHDLToolDLG/FDArchitecture panel.

%   Author(s): J. SUN
%   Copyright 2009-2011 The MathWorks, Inc.
%     

success = true;
msg = '';

switch action
%     case 'ComboboxChanged'
%         indx = varargin{1};
%         prop = varargin{2};
%         entries = varargin{3};
%         
%         this.(prop) = entries{indx+1};
        
    case 'FoldingFactor'        
        [sp, ff, mult] = getSpecificSerialInfo(this.hdlfilt,this.CoefficientSource, 'FoldingFactor', str2num(this.FoldingFactor)); %#ok
        this.SerialPartition = this.hdlfilt.convSerialPart2String(sp);
        this.Multipliers = num2str(mult);
    case 'Multipliers'        
        [sp, ff, mult] = getSpecificSerialInfo(this.hdlfilt, this.CoefficientSource,'Multipliers', str2num(this.Multipliers)); %#ok
        this.SerialPartition = this.hdlfilt.convSerialPart2String(sp);
        this.FoldingFactor = num2str(ff);        
    case 'SerialPartition'        
        [sp, ff, mult] = getSpecificSerialInfo(this.hdlfilt,this.CoefficientSource, 'SerialPartition', str2num(this.SerialPartition)); %#ok
        this.FoldingFactor = num2str(ff);
        this.Multipliers = num2str(mult);
    case 'PartitionModeEntry'
        if strcmpi(this.PartitionModeEntry, 'Folding factor')
            [sp, ff, mult] = getSpecificSerialInfo(this.hdlfilt, this.CoefficientSource, 'FoldingFactor', str2num(this.FoldingFactor) ); %#ok<*ST2NM>
        elseif strcmpi(this.PartitionModeEntry, 'Multipliers')
            [sp, ff, mult] = getSpecificSerialInfo(this.hdlfilt,this.CoefficientSource, 'Multipliers', str2num(this.Multipliers));
        elseif strcmpi(this.PartitionModeEntry, 'Serial partition')
            [sp, ff, mult] = getSpecificSerialInfo(this.hdlfilt, this.CoefficientSource, 'SerialPartition', str2num(this.SerialPartition));
        end
        this.SerialPartition = this.hdlfilt.convSerialPart2String(sp);
        this.FoldingFactor = num2str(ff);
        this.Multipliers = num2str(mult);
    case 'SpecDAFoldingFactor'
    case 'SpecifyLUT'
        
    case 'DAFoldingFactor'
        [lutpartition daadix this.DALUTSize foldingfactor] = hdlfilterdainfo(this.filterObj, 'foldingfactor', str2double(this.DAFoldingFactorValue), 'LUTInputs', str2double(this.LUTInputsValue)); %#ok
            daadix = ['2^', num2str(log2(daadix))];
            this.DARadixValue = daadix;
        
    case 'DARadix'
        [lutpartition daadix this.DALUTSize foldingfactor] = hdlfilterdainfo(this.filterObj, 'DARAdix', eval(str2mat(this.DARadixValue)), 'LUTInputs', str2double(this.LUTInputsValue));%#ok
            this.DAFoldingFactorValue = num2str(foldingfactor);
        
    case 'LUTInputs'
        [lutpartition daadix this.DALUTSize foldingfactor] = hdlfilterdainfo(this.filterObj, 'LUTInputs', str2double(this.LUTInputsValue), 'DARAdix', eval(this.DARadixValue));%#ok
        this.LUTPartitionValue = mat2str(lutpartition);    
        
    case 'DALUTPartition'
        [lutpartition daadix this.DALUTSize foldingfactor] = hdlfilterdainfo(this.filterObj, 'DALUTPartition', eval(str2mat(this.LUTPartitionValue)), 'DARAdix', eval(this.DARadixValue));%#ok
            this.LUTInputsValue = num2str(max(max(lutpartition')));            
            
    case 'DAInfoLink'
        % Bring up html window
        [damatrix, lutmatrix]  = getDAPartMatrix(this.hdlfilt);
        htmltext = dispDAPartitionHTML(this.hdlfilt,damatrix,lutmatrix);      
        displaDetailedInfo(this, htmltext, '--- Distributed Arithmetic (DA) ---');        
        
    case 'SerialPartitionInfoLink'    
        if isa(this.hdlfilt, 'hdlfilter.df1sos') || isa(this.hdlfilt, 'hdlfilter.df2sos') 
            spmatrix  = getSerialPartMatrix(this.hdlfilt);
            htmltext = dispSerialPartitionHTML(this.hdlfilt,spmatrix);
            displaDetailedInfo(this, htmltext, '--- Serial Architecture ---');
        else
            fl = this.hdlfilt.getfilterlengths;
            if strcmpi(this.CoefficientSource, 'processorinterface') || ...
                    strcmpi(this.CoefficientSource, 'processor interface')
                fl = fl.coeff_len; 
            else
                fl = fl.czero_len;% discounting the zeros
            end
            spmatrix  = getSerialPartMatrix(this.hdlfilt,fl);
            htmltext = dispSerialPartitionHTML(this.hdlfilt,spmatrix);
            displaDetailedInfo(this, htmltext, '--- Serial Architecture ---');            
        end
    otherwise
end

function displaDetailedInfo (this, htmltext, archname)
if ~isempty(this.webdlgHandle) && ishandle(this.webdlgHandle)
    % dialog exist
    this.reportHandle.setDisplayContent([archname htmltext], sprintf(DAStudio.message('HDLShared:fdhdldialog:architectureDetails')));
    this.webdlgHandle.refresh;
    this.webdlgHandle.show;
else
    % created a new report dialog
    this.reportHandle = reportdlg.htmlreportdlg([archname htmltext], sprintf(DAStudio.message('HDLShared:fdhdldialog:architectureDetails')));
    this.reportHandle.setIcon('\toolbox\shared\dastudio\resources\MatlabIcon.png');
    this.webdlgHandle = DAStudio.Dialog(this.reportHandle);
end

%-------------------------------------------------------------
function  [sp, ff, mul] = getSpecificSerialInfo(hdlfilt, csvalue, 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, 'CoefficientSource', csvalue);
end