function qDetails = quantizationDetails(net)
    %QUANTIZATIONDETAILS Returns the quantization details for a network
    %   details = quantizationDetails(net) returns a struct of information
    %   regarding whether the input network is quantized, the intended
    %   target library for emulation, which layers are quantized and the
    %   stored integer values of quantized learnables.
    %
    %   DETAILS = QUANTIZATIONDETAILS(NET) returns the results for
    %   DAGNetwork, SeriesNetwork and dlnetwork in an 1-by-1 structure with
    %   the fields:
    %       IsQuantized           -- True if the network is quantized
    %       TargetLibrary         -- Target library for code generation
    %       QuantizedLayerNames   -- List of quantized layers
    %       QuantizedLearnables   -- Quantized network learnable parameters
    %
    % Example:
    %   net = googlenet;
    %   qDetails = quantizationDetails(net);
    %
    %   See also dlquantizer, DAGNetwork, SeriesNetwork, dlnetwork.

    %   Copyright 2021-2022 The MathWorks, Inc.

    arguments
        net(1,1) {iMustBeOfClass(net)}
    end

    qDetails = struct('IsQuantized', false, 'TargetLibrary', "", 'QuantizedLayerNames', string.empty, 'QuantizedLearnables', iGetEmptyQuantizedLearnablesTable());

    if ~deep.internal.quantization.isQuantizationEnabled(net)
        return;
    end

    qDetails.IsQuantized = true;

    qDetails.TargetLibrary = iGetTargetLibrary(net);

    qDetails.QuantizedLayerNames = iGetQuantizedLayerNames(net);

    qDetails.QuantizedLearnables = iGetQuantizedLearnables(net);

end

function value = iGetTargetLibrary(net)
    value = string(deep.internal.quantization.getTargetLibrary(net));
end

function value = iGetQuantizedLearnables(net)
    learnableValues = deep.internal.quantization.getQuantizedLearnables(net);
    if isempty(learnableValues)
        value = iGetEmptyQuantizedLearnablesTable();
    else
        value = struct2table([learnableValues{:}], 'AsArray', true);
    end
end

function value = iGetQuantizedLayerNames(net)
    value = string(deep.internal.quantization.getQuantizedLayerNames(net))';
end

function value = iGetEmptyQuantizedLearnablesTable()
    value = array2table(zeros(0,3), 'VariableNames', {'Layer', 'Parameter', 'Value'});
end

function iMustBeOfClass(input)
    % Test for specific class name
    acceptedClassNames = {'DAGNetwork', 'SeriesNetwork', 'dlnetwork'};
    inputClassName = class(input);
    if ~ismember(inputClassName,acceptedClassNames)
        msg = message('nnet_cnn:Quantization:InvalidNetworkTypeQuantizationDetails');
        throwAsCaller(MException(msg))
    end
end
