function hDisplayNetworkStats(modelName)
%hDisplayNetworkStats Display network level statistics
%
%   This is an example helper function.
%
%   hDisplayNetworkStats(MODELNAME) displays the network level statistics
%   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 2019 The MathWorks, Inc.

%#codegen

% Read statistics from base workspace
stats = evalin('base','stats');
% 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

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);

% Initialize interested statistics
macDataTx = zeros(numNodes, 1);
macDataRx = zeros(numNodes, 1);
macTotalRetries = zeros(numNodes, 1);
macTxSuccess = zeros(numNodes, 1);
macRxDrop = zeros(numNodes, 1);
interferencePkts = zeros(numNodes, 1);

% Collect the interested statistics from all nodes
for idx = 1:numNodes
    macDataTx(idx) = stats(idx).MACDataTx;
    macDataRx(idx) = stats(idx).MACDataRx;
    macTxSuccess(idx) = stats(idx).MACTxSuccess;
    macRxDrop(idx) = stats(idx).MACRxDrop;
    macTotalRetries(idx) = stats(idx).MACRetries;
    interferencePkts(idx) = stats(idx).AppRx;
end

% Create a plot matrix with all the interested statistics
PlotMatrix = [macDataTx macTxSuccess macTotalRetries macDataRx macRxDrop];

nextBarOffset = 1;

% Set colors for the bars
colors = [...
    0.2784    0.5176    0.6784; ... % MAC Data Tx
    0.4667    0.6745    0.1882; ... % MAC Tx Success
    0.9608    0.7569    0.6706; ... % MAC Retries
    0.5176    0.7922    0.9098; ... % MAC Data Rx
    0.8510    0.3255    0.0980; ... % MAC Rx Drop
    ];

% 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 Network Statistics', ...
    'Position',[screenWidth*0.2, screenHeight*0.1, figureWidth, figureHeight]);

% Set figure axis
hold on;

% Plot the bar chart
for idx = 1:size(PlotMatrix, 2)
    bar(nextBarOffset, sum(PlotMatrix(:,idx)),'FaceColor',colors(idx, :));
    nextBarOffset = nextBarOffset+1;
end

% Add legend
legend({'Transmitted data frames',...
    'Successful data frame transmissions',...
    'Retransmitted data frames', ...
    'Received data frames',...
    'Dropped data frames',...
    },...
    'Location','northeast');

% Set title to the plot
title('Network-Level Statistics');
% Label Y-axis of the plot
ylabel('Number of Frames');
% Add tick labels to axis
xticks((1:5));
xticklabels({'Total Tx', 'Tx Success', 'Tx Retries', 'Rx Success', 'Rx Drop'});

end