function edcaUpdateStats(nodeID, statType, value) 
%edcaUpdateStats Updates the statistics for all nodes in the model
%
%   This is an example helper function.
%
%   edcaUpdateStats(NODEID, STATSTYPE, VALUE) updates the respective
%   statistic for the given node. NODEID represents a node whose statistic
%   will be updated. STATSTYPE represents the statistic type. VALUE
%   represents the value to be updated for the given STATSTYPE.

%   Copyright 2019 The MathWorks, Inc.

% Store the statistics of each node in the simulation
persistent stats;
if isempty(stats)
    % Define and initialize array of stats structure to zero
    [~, statStrings] = enumeration('edcaStats');
    statsStruct = struct();
    for i = 1:numel(statStrings)
        statsStruct.(statStrings{i}) = 0;
    end
    stats = repmat(statsStruct, 1000, 1);
end

% Update the statistics of the node with given node ID
if ~isempty(nodeID)
    statString = string(statType);
    validateattributes(statType, {'edcaStats'}, {'scalar'}, mfilename, '', 2);
    
    % Max queue length statistic
    if (statType == edcaStats.MACAC0MaxQueueLength) || ...
            (statType == edcaStats.MACAC1MaxQueueLength) || ...
            (statType == edcaStats.MACAC2MaxQueueLength) || ...
            (statType == edcaStats.MACAC3MaxQueueLength) || ... 
            (statType == edcaStats.MACRecentFrameStatusTimestamp)
        if stats(nodeID).(statString) < value
            stats(nodeID).(statString) = value;
        end
    else
        % Access and update the statistic of a node corresponding to the
        % given statType
        stats(nodeID).(statString) = stats(nodeID).(statString) + value;
    end
else
    % Write the whole stats into base workspace
    assignin('base', 'stats', stats);
end
end
