function simResult = helperBLEMeshFloodingSimulationResults(varargin)
%helperBLEMeshFloodingSimulationResults Stores the observed simulation
%results into a table
%   SIMRESULTS = helperBLEMeshFloodingSimulationResults(MESHNODES) returns
%   a table containing the cumulative network layer statistics at each
%   node, at the end of the simulation.
%
%   SIMRESULTS = helperBLEMeshFloodingSimulationResults(SRCDSTPAIRS, PATH)
%   returns a table containing the results i.e. source, destination, path
%   and number of hops for all given source and destination pairs for a
%   given scenario.
%
%   MESHNODES is a vector of objects of type helperBLEMeshNetworkNode.
%
%   SRCDSTPAIRS is a n-by-2 array contains source and destination pairs.
%
%   PATH is a cell array contains path for given SRCDSTPAIRS.

%   Copyright 2019 The MathWorks, Inc.

% Check number of arguments
narginchk(1, 2);

% Network statistics
if nargin == 1
    meshNodes = varargin{1};
    % Create a table to store cumulative network statistics at each node
    networkStatistics = table('Size', [numel(meshNodes), 6], ...
        'VariableTypes', {'double', 'double', 'double', 'double', 'double', 'double'}, ...
        'VariableNames', {'NodeID', 'TotalTxMsgs', 'TotalRxMsgs', 'TotalAppRxMsgs', ...
        'TotalRelayedMsgs', 'TotalDroppedMsgs'});
    
    % Store network statistics in a table
    for nodeIdx = 1:numel(meshNodes)
        networkStatistics{nodeIdx, 1} = meshNodes(nodeIdx).Identifier;
        networkStatistics{nodeIdx, 2} = meshNodes(nodeIdx).NetworkLayer.MsgsTransmitted;
        networkStatistics{nodeIdx, 3} = meshNodes(nodeIdx).NetworkLayer.MsgsReceived;
        networkStatistics{nodeIdx, 4} = meshNodes(nodeIdx).NetworkLayer.MsgsForAppRx;
        networkStatistics{nodeIdx, 5} = meshNodes(nodeIdx).NetworkLayer.MsgsRelayed;
        networkStatistics{nodeIdx, 6} = meshNodes(nodeIdx).NetworkLayer.MsgsDropped;
    end
    simResult = networkStatistics;
    
% Scenario results
else
    srcDstPairs = varargin{1};
    path = varargin{2};
    % Fetch unique source and destination pairs
    uniqueSrcDstNodes = zeros(numel(srcDstPairs)/2, 2);
    uniqueIdx = 1;
    for pairIdx = 1:(numel(srcDstPairs)/2)
        isUniquePair = true;
        pair = srcDstPairs(pairIdx, :);
        for uniquePairIdx = 1:(numel(uniqueSrcDstNodes)/2)
            if unique(uniqueSrcDstNodes(uniquePairIdx, :) == pair) == 1
                isUniquePair = false;
                break;
            end
        end
        if isUniquePair
            uniqueSrcDstNodes(uniqueIdx, :) = pair;
            uniqueIdx = uniqueIdx + 1;
        end
    end
    srcDstPairs = uniqueSrcDstNodes(1:uniqueIdx-1, :);
    
    % Create a table to store results of the scenario
    scenarioResults = table('Size', [numel(srcDstPairs)/2, 4], ...
        'VariableTypes', {'double','double','cell', 'double'}, ...
        'VariableNames', {'Source', 'Destination', 'Path', 'NumberOfHops'});
    
    % Store data into table
    for nodeIdx = 1:numel(srcDstPairs)/2
        scenarioResults{nodeIdx, 1} = srcDstPairs(nodeIdx, 1);
        scenarioResults{nodeIdx, 2} = srcDstPairs(nodeIdx, 2);
        scenarioResults{nodeIdx, 3} = path(nodeIdx);
        scenarioResults{nodeIdx, 4} = numel(path{nodeIdx}) - 1;
    end
    simResult = scenarioResults;
end
end
