function dcfPlotQueueLengths(visibilityFlag, strings, nodeID, queueLen, time)
%dcfPlotQueueLengths Plots the queue lengths in the simulation
%
%   dcfPlotQueueLengths(FLAG, STRINGS, NODEID, QUEUELEN, TIME) plots the
%   queue lengths in the simulation
%
%   VISIBILITYFLAG is specified as either true or false. The value true
%   makes the plot visible. This is configured only when the simulation is
%   in progress.
%
%   STRINGS is a cell array of the node names for which the queue lengths
%   will be plotted.
%
%   NODEID is the ID of the node for which the queue lengths are plotted.
%
%   QUEUELEN is the length of the queue at the current simulation time.
%
%   TIME is the current simulation time.

%   Copyright 2019 The MathWorks, Inc.

persistent userReqFlag queueLengthsFig queueLenPlot nodeSelector prevNode

if isempty(userReqFlag) || (~isempty(queueLengthsFig) && ~isvalid(queueLengthsFig))
    userReqFlag = false;
end

% User requested to plot MAC queue lengths
if ~userReqFlag && visibilityFlag
    userReqFlag = true;
end

% User is not interested in plotting MAC queue lengths
if ~userReqFlag
    return;
end
if (isempty(queueLengthsFig) || isempty(queueLenPlot)) || ~isvalid(queueLengthsFig)
    % Get screen resolution
    resolution = get(0, 'screensize');
    screenWidth = resolution(3);
    screenHeight = resolution(4);
    figureWidth = screenWidth*0.7;
    figureHeight = screenHeight*0.7;
    % Create figure
    queueLengthsFig = figure('Name', 'MAC Tx Queue Lengths', 'Tag', 'DCFNetwork', ...
        'Position', [screenWidth*0.1, screenHeight*0.05, figureWidth, figureHeight]);
    hold on;
    numNodes = numel(strings);
    plotColor = [0 0.45 0.74];
    % Preallocate a cell array to store queueLenPlot handle of each node
    queueLenPlot = cell(numNodes, 1);
    for idx = 1:numNodes
        % Create plots for each node
        queueLenPlot{idx} = plot(0, 0, 'Marker', '*', 'LineStyle', '-', 'LineWidth', 1, 'Visible', 'off', 'Color', plotColor);
    end
    
    % Create a ui control panel to hold both annotation and dropdown
    panel = uipanel(queueLengthsFig, 'Position', [0.13 0.925 0.25 0.04], 'BorderType', 'none');
    % Create textbox
    uicontrol(panel, 'Style','text','String', 'Node Selector:', ...
       'Position', [20 2 155 27]);
    % Create drop down for node selection
    nodeSelector = uicontrol(panel, 'Style','popupmenu','String', strings, ...
        'Position', [150 3 155 27], 'Callback', @plotNodeQueueLengths);
    prevNode = nodeSelector.Value;
    queueLenPlot{prevNode}.Visible = 'on';
    % Create empty plots and add legend
    emptyPlot = plot(NaN, 'Marker', '*', 'LineStyle', '-', 'LineWidth', 1, 'Color', plotColor);
    % Add legend
    legend(emptyPlot, 'MAC Tx Queue Length', 'Location', 'northeastoutside');
    % Add X and Y labels
    xlabel('Node Timeline (Microseconds)', 'FontSize', 12);
    ylabel('Number of Frames in Queue', 'FontSize', 12);
end

% Stop plotting if the figure is closed during simulation
if ~isvalid(queueLengthsFig)
    return;
end

% Plot selected node queue lengths
plotNodeQueueLengths([], []);
    function plotNodeQueueLengths(~, ~)
        % New node is selected
        if (nodeSelector.Value ~= prevNode)
            % Hide the previous node plot
            queueLenPlot{prevNode}.Visible = 'off';
            % Display the current node plot
            queueLenPlot{nodeSelector.Value}.Visible = 'on';
            % Clear the previous node plots
            prevNode = nodeSelector.Value;
        end
        % New node is selected and node ID matches with input node ID
        if (nodeID ~= 0)
            if queueLenPlot{nodeID}.YData(end) ~= queueLen
                % Update the node plot
                queueLenPlot{nodeID}.XData = [queueLenPlot{nodeID}.XData time];
                queueLenPlot{nodeID}.YData = [queueLenPlot{nodeID}.YData queueLen];
            end
        end
    end
end
