function hDisplayPerBSSThroughput(modelName)
%hDisplayNetworkStats Display per BSS aggregated throughput
%
%   This is an example helper function.
%
%   hDisplayNetworkStats(MODELNAME) displays the per BSS aggregated
%   throughput at the end of the simulation as a bar graph. These
%   statistics are collected while running the model with the name,
%   MODELNAME.
%
%   MODELNAME is a Simulink model name.

%   Copyright 2020 The MathWorks, Inc.

% Read statistics from base workspace
stats = evalin('base','stats');
%stats = evalin('base','statisticsTable');
% Get the node names in the model
nodeNamesList = dcfGetNodeNamesList(modelName);
% Get the number of nodes in the simulation scenario
numNodes = numel(nodeNamesList);
% First element of numNodes is the number of nodes in simulation
numNodes = numNodes(1, 1);

% Calculate derived statistics
for idx = 1:numNodes
    if (stats(idx).MACAverageTimePerFrame ~= 0)
        stats(idx).MACAverageTimePerFrame = (stats(idx).MACAverageTimePerFrame)/(stats(idx).MACDataTx);
    end
    stats(idx).Throughput = ((stats(idx).MACTxBytes*8)/get_param(modelName,'SimulationTime'))/1e6; % in Mbps
end

% Get BSSs information
[apNodeIDMap, nodeMap, isAP, ~]  = edcaGetBSSInfo(nodeNamesList);
% Number of BSSs
numBSS = numel(unique(apNodeIDMap));
% Initialize an arry to indicate unprocessed nodes in a BSS
processBSSNodes = ones(1, numel(apNodeIDMap));

% Initialize
count = 1;
throughput = zeros(numBSS, 1);
xticksList = cell(numBSS, 1);
legendList = cell(numBSS, 1);

% Calculate per BSS throughput
for i = 1:numNodes
    if processBSSNodes(i)
        % Get all the node IDs per BSS
        bss = (apNodeIDMap == apNodeIDMap(i));
        bssNodeIDs = find(bss);
        
        % Get the AP node name
        [~, apName] = fileparts(nodeMap(bssNodeIDs(isAP(bssNodeIDs))));
        for j = 1:numel(bssNodeIDs)
            throughput(count) = throughput(count) + stats(bssNodeIDs(j)).Throughput;
            processBSSNodes(bssNodeIDs(j)) = 0;
        end
        % Set xticks and legends for each BSS
        xticksList{count} = ['BSS-', char(apName)];
        legendList{count} = ['BSS with AP name ', char(apName)];
        count = count + 1;
    end
end

NodeNames = cell(numNodes, 1);
for idx = 1:numNodes
    NodeNames{idx} = strrep(nodeNamesList{idx}, [modelName '/'], '');
end
% Create a stats table and write into statistics.mat file
statisticsTable = struct2table(stats(1:numNodes),'RowNames',NodeNames);
save('statistics.mat','statisticsTable')

% Display the statistics of each node in the simulation
results = statisticsTable(:, {'AppRx','MACDataTx','MACDataRx','MACRxDrop',...
    'MACAckTx','MACAckRx','MACRTSTx','MACRTSRx','MACCTSTx','MACCTSRx',...
    'MACTxFails','MACRetries'});
disp(results);

nextBarOffset = 1;

% Set colors for the bars
colors = [...
    0    0.4471    0.7412;
    0.8510    0.3255    0.0980;
    0.9294    0.6941    0.1255;
    0.4941    0.1843    0.5569;
    0.4667    0.6745    0.1882;
    0.3020    0.7451    0.9333;
    0.6353    0.0784    0.1843;
    0.5020    0.5020    0.5020;
    ];

% Get screen resolution
resolution = get(0, 'screensize');
screenWidth = resolution(3);
screenHeight = resolution(4);
figureWidth = screenWidth*0.35;
figureHeight = screenHeight*0.55;
% Create figure
figure('Name','Multi Node 802.11 Per BSS Aggregated Throughput', ...
    'Position',[screenWidth*0.2, screenHeight*0.1, figureWidth, figureHeight]);

% Set figure axis
hold on;

% Plot the bar chart
for idx = 1:size(throughput)
    bar(nextBarOffset, throughput(idx), 'FaceColor', colors(mod(idx, numel(colors(:, 1))), :));
    nextBarOffset = nextBarOffset+1;
end

% Add legend
legend(legendList,...
    'Location','northeast');

% Set title to the plot
title('Aggregated Per BSS Throughput');
% Label Y-axis of the plot
ylabel('Throughput (Mbps)');
% Add tick labels to axis
xticks(1:numBSS);
% Rotate X-axis tick lables
xtickangle(45);
% Add X-axis tick lables
xticklabels(xticksList);
end
