function edcaPlotStats(nodeID, stateIndex, time, duration, varargin)
%edcaPlotStats Plots the simulation time-line statistics
%
%   This is an example helper function.
%
%   edcaPlotStats(NODEID, STATEINDEX, TIME, DURATION) plot the
%   simulation time-line statistics for the given inputs.
%
%   NODEID is the ID of the node.
%
%   STATEINDEX is the index of state. 1 indicates Contend, 2 indicates Tx,
%   3 indicates Rx
%
%   TIME is the state entry time-stamp corresponding to the given
%   STATEINDEX.
%
%   DURATION is the time spent in the state corresponds to the given
%   STATEINDEX.

%   Copyright 2019 The MathWorks, Inc.


persistent statsFig figAxis modelName plotsEnabled
% State colors
contendColor = [0.9, 0.7, 0.4];
txColor = [0.7, 0.9, 0.6];
rxColor = [0.4, 0.6, 0.9];
idleEIFSColor = [1, 1, 1];

% State indexes
contendIndex = 1;
txIndex = 2;
rxIndex = 3;
barHeight = 1;

if ~isempty(varargin) 
    plotsEnabled = false;
    if ~isempty(statsFig) && isvalid(statsFig)
        close(statsFig);
    end
end

if ~isempty(plotsEnabled) && (plotsEnabled == false)
    return;
end

if isempty(statsFig)
    plotsEnabled = true;
    % Get the model name
    modelName = evalin('base', 'ModelName');
    % Get screen resolution
    resolution = get(0, 'screensize');
    screenWidth = resolution(3);
    screenHeight = resolution(4);
    figureWidth = screenWidth*0.7;
    figureHeight = screenHeight*0.7;
    % Create figure
    statsFig = figure('Name', 'MAC State Transitions Over Time', 'Tag', 'EDCANetwork', ...
        'Position', [screenWidth*0.1, screenHeight*0.05, figureWidth, figureHeight]);
    hold on;
    ylables = dcfGetNodeNamesList(modelName);
    for i = 1:numel(ylables)
        ylables{i} = strrep(ylables{i}, [modelName '/'], '');
    end
    ymax = numel(ylables)*(barHeight+1);
    ylim([0 ymax]);
    yticks(0.5:barHeight+1:ymax);
    yticklabels(ylables);
    hold on;
    
    % Create empty plots for annotation
    p(1) = bar(NaN, 'FaceColor', contendColor);
    p(2) = bar(NaN, 'FaceColor', txColor);
    p(3) = bar(NaN, 'FaceColor', rxColor);
    p(4) = bar(NaN, 'FaceColor', idleEIFSColor);
    
    legend(p, {'Contention', 'Transmission', 'Reception', 'Idle/EIFS/SIFS'}, 'Location', 'northeastoutside');
    
    pan xon;
    xlabel('Node Timeline (Microseconds)', 'FontSize', 12);
    ylabel('Node Names', 'FontSize', 12);
    figAxis = gca;
    
    % Button to view MAC queue lengths plot
    uicontrol('Style','pushbutton', 'String','Observe MAC queue lengths', ...
        'Position', [20 20 155 27], 'Callback', @plotQueueLengthsCallback);
end

% Return if the figure is closed in between simulation
if ~isvalid(statsFig)
    return;
end

% Select color based on state index
if stateIndex == contendIndex
    fillColor = contendColor;
elseif stateIndex == txIndex
    fillColor = txColor;
elseif stateIndex == rxIndex
    fillColor = rxColor;
end
rectangle(figAxis, 'Position', [time, ((nodeID-1)*(barHeight+1)), duration, barHeight], 'FaceColor', fillColor);
end

function plotQueueLengthsCallback(~, ~)
persistent modelName
if isempty(modelName)
    % Get the model name
    modelName = evalin('base', 'ModelName');
end
nodeNames = dcfGetNodeNamesList(modelName);
for i = 1:numel(nodeNames)
    nodeNames{i} = strrep(nodeNames{i}, [modelName '/'], '');
end
edcaPlotQueueLengths(true, nodeNames, 0, 0, 0);
end
