function bleWLANCoexistenceStatistics = helperBLELogCoexistenceStats(master, slaves, matFile)
%helperBLELogCoexistenceStats Log the statistics to MATLAB workspace
%   BLEWLANCOEXISTENCESTATISTICS = helperBLELogCoexistenceStats(MASTER,
%   SLAVES, MATFILE) log the statistics to MATLAB workspace with file name
%   mentioned in MATFILE. Statistics are in the form of a table, which are
%   collected from the Master and Slaves objects.
%
%   BLEWLANCOEXISTENCESTATISTICS = helperBLELogCoexistenceStats(MASTER,
%   SLAVES) log the statistics to MATLAB workspace with file name as
%   bleWLANCoexistenceStatistics.mat. Statistics are in the form of a
%   table, which are collected from the Master and Slaves objects.
%
%   MASTER is an object of type <a
%   href="matlab:help('helperBLEDeviceModel')">helperBLEDeviceModel</a>. It
%   acts as a Link Layer Master device.
%
%   SLAVES is a cell array of objects, each is of type <a
%   href="matlab:help('helperBLEDeviceModel')">helperBLEDeviceModel</a>. It
%   acts as a Link Layer Slave device.
%
%   BLEWLANCOEXISTENCESTATISTICS is a cell array of tables. Each table
%   contains the statistics of a Master-Slave connection pair.
%
%   MATFILE is a character vector or string scalar. It is the file name for
%   storing the statistics into MATLAB workspace.

%   Copyright 2018-2019 The MathWorks, Inc.

% Validate number of input arguments
narginchk(2, 3)

% Initialize MAT file name to dump the statistics
matFileName = 'bleWLANCoexistenceStatistics.mat';

if nargin > 2
    matFileName = matFile;
end

% Number of columns for each table for Slave statistics
ncols = 37;

% Number of tables
ntables = numel(slaves);

% Row names for total statistics
statisticsCatagories = {'TotalRxPacketsSlaveToMaster', 'TotalRxPacketsFailedSlaveToMaster'};

% Column data types
varTypes = cell(1, ncols);

% Column names
variableNames = cell(1, ncols);

% Initialize the data type of each cell
for idx = 1:numel(varTypes)
    varTypes{idx} = 'double';
end

% Column names
for idx = 1:numel(variableNames)
    variableNames{idx} = ['Channel_' num2str(idx-1)];
end

bleWLANCoexistenceStatistics = cell(1, ntables);

for slIdx = 1:numel(slaves)
    cnt = 1;
    classificationCatagories = cell(1, master.LLConnectionConfigs(slIdx).ChannelClassificationCount*3);
    for clIdx = 1:master.LLConnectionConfigs(slIdx).ChannelClassificationCount
        classificationCatagories{cnt} = ['RxPacketsSlaveToMasterClassification' num2str(clIdx)];
        cnt = cnt + 1;
        classificationCatagories{cnt} = ['RxPacketsFailedSlaveToMasterClassification' num2str(clIdx)];
        cnt = cnt + 1;
        classificationCatagories{cnt} = ['ChannelStatusForSlaveAfterClassification' num2str(clIdx)];
        cnt = cnt + 1;
    end
    rowNames = [classificationCatagories statisticsCatagories];
    nrows = numel(rowNames);
    
    bleWLANCoexistenceStatistics{slIdx} = table('Size', [nrows, ncols], ...
        'RowNames', rowNames, 'VariableNames', variableNames, 'VariableTypes', varTypes);
end

for idx = 1:master.SlavesCount
    % Update total Rx packets count for each Slave
    rowName = 'TotalRxPacketsSlaveToMaster';
    for slIdx = 1:ncols
        bleWLANCoexistenceStatistics{idx}({rowName}, slIdx) = {master.LLConnectionConfigs(idx).TotalRxCount(slIdx)};
    end
    
    % Update total Rx packets failure count for each Slave
    rowName = 'TotalRxPacketsFailedSlaveToMaster';
    for slIdx = 1:ncols
        bleWLANCoexistenceStatistics{idx}({rowName}, slIdx) = {master.LLConnectionConfigs(idx).TotalRxFailureCount(slIdx)};
    end
    
    for slIdx = 1:master.LLConnectionConfigs(idx).ChannelClassificationCount
        rowName1 = ['RxPacketsSlaveToMasterClassification' num2str(slIdx)];
        rowName2 = ['RxPacketsFailedSlaveToMasterClassification' num2str(slIdx)];
        rowName3 = ['ChannelStatusForSlaveAfterClassification' num2str(slIdx)];
        
        for clIdx = 1:ncols
            bleWLANCoexistenceStatistics{idx}({rowName1}, clIdx) = {nnz(master.LLConnectionConfigs(idx).ClassificationStats(slIdx,clIdx,:) ~= -1)};
            bleWLANCoexistenceStatistics{idx}({rowName2}, clIdx) = {nnz(master.LLConnectionConfigs(idx).ClassificationStats(slIdx,clIdx,:) == 0)};
            bleWLANCoexistenceStatistics{idx}({rowName3}, clIdx) = {master.LLConnectionConfigs(idx).ChannelMapStats(slIdx,clIdx)};
        end
    end
end

% Save statistics into MATLAB base workspace
save(matFileName, 'bleWLANCoexistenceStatistics')
end