function dcfPlotStats(nodeID, stateIndex, time, duration)
%dcfPlotStats Plots the simulation time-line statistics
%
%   dcfPlotStats(NODEID, STATEINDEX, TIME, DURATION) plot the
%   simulation time-line statistics for the given inputs.
%
%   NODEID is the ID of the node specified by the user.
%
%   STATEINDEX is the enumeration of type <matlab:help('dcfStats')
%   dcfStats>.
%
%   TIME is the entry time-stamp corresponding to the given STATEINDEX.
%
%   DURATION is the time spent for the given STATEINDEX.

%   Copyright 2019 The MathWorks, Inc.

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

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

if isempty(statsFig)
    % 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', 'DCFNetwork', ...
        '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);
    % Color annotation coordinates
    annoteX = 0.04;
    annoteY = 0.92;
    textWidth = 0.054;
    textHeight = 0.048;
    
    % Create textbox for labeling color of contention
    annotation(statsFig,'textbox',...
        [annoteX annoteY textWidth textHeight], 'String', {'Contention'},...
        'LineStyle', 'none', 'FitBoxToText', 'off');
    annoteY = annoteY-0.04;
    
    % Create textbox for labeling color of Transmission
    annotation(statsFig,'textbox', [annoteX annoteY textWidth textHeight],...
        'String', {'Transmission'}, 'LineStyle','none', 'FitBoxToText','off');
    annoteY = annoteY-0.04;
    
    % Create textbox for labeling color of Reception
    annotation(statsFig,'textbox',[annoteX annoteY textWidth textHeight],...
        'String', {'Reception'}, 'LineStyle','none', 'FitBoxToText','off');
    
    annoteY = 0.94;
    annoteX = annoteX-0.03;
    boxWidth = 0.029;
    boxHeight = 0.027;
    
    % Create rectangle for labeling color of contention
    annotation(statsFig,'rectangle', [annoteX annoteY boxWidth boxHeight],...
        'FaceColor', contendColor, 'LineStyle', 'none');
    annoteY = annoteY-0.04;
    
    % Create rectangle for labeling color of Transmission
    annotation(statsFig,'rectangle', [annoteX annoteY boxWidth boxHeight], ...
        'FaceColor', txColor, 'LineStyle', 'none');
    annoteY = annoteY-0.04;
    
    % Create rectangle  for labeling color of Reception
    annotation(statsFig,'rectangle', [annoteX annoteY boxWidth boxHeight],...
        'FaceColor', rxColor, 'LineStyle', 'none');
    
    hold on;
    pan xon;
    xlabel('Node Timeline (Microseconds)', 'FontSize', 12);
    ylabel('Node Names', 'FontSize', 12);
    figAxis = gca;
    
    % Button to view MAC queue lengths plot
    pbutton = uicontrol('Style','pushbutton', 'String', {'Observe MAC Tx Queue Lengths'}, ...
        'Position', [20 20 155 27], 'Callback', @plotQueueLengthsCallback);
    % Wrap text for the push button ui control. Here 40 is length of text
    % that is going to appear on the push button.
    [wrappedText, positions] = textwrap(pbutton, pbutton.String, 40);
    % Set the text on push button
    pbutton.String = wrappedText;
    % Set the push button's position
    pbutton.Position = positions;
end

if isempty(findobj('Name', 'MAC State Transitions over Time'))
    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
dcfPlotQueueLengths(true, nodeNames, 0, 0, 0);
end
