function helperBLEPlotHRPFrames(inp, val)
% Plot the data frame exchange between the Heart rate profile server and
% client

% Copyright 2019 The MathWorks, Inc.

persistent heartRateMeasurement arrows msg transaction

% Get screen resolution
resolution = get(0, 'screensize');
screenWidth = resolution(3);
screenHeight = resolution(4);

if isempty(heartRateMeasurement)
  % Initialize figure
  figHRP = figure('Position', [screenWidth*0.15, screenHeight*0.15, screenWidth*0.7, ...
    screenHeight*0.6], 'Color','white', 'MenuBar', 'none', ...
    'Name', 'BLE Heart Rate Profile', 'Tag', 'HRP');
  % hold on
  ax = axes(figHRP, 'Visible', 'off');
  for i=1:11
     ax(i) = copyobj(ax(1),figHRP);
     ax(i).Visible = 'off';
  end
  
  % Message exchange between Client and Server
  
  % Initialize the arrows
  fromClient = imread('BLERightArrow.png');
  fromServer = imread('BLELeftArrow.png');
  
  % Preallocate the buffers to store sub plots and messages
  msgSubPlot = cell(1,9);
  msg = cell(1,9);
  
  % Create a sub plot for each transmission
  for i = 1:9
    msgSubPlot{i} = subplot(1, 11, i+1, ax(i));
    axis(msgSubPlot{i}, [0 200 0 100]);
    if rem(i, 2)
      ax(i).Tag = ['bleRightArrow' num2str(i)];
      arrows{i} = imshow(fromClient, 'XData', [1 200], 'YData', [1 35], 'Parent', ax(i));
    else
      arrows{i} = imshow(fromServer, 'XData', [1 200], 'YData', [1 35], 'Parent', ax(i));
    end
    arrows{i}.Visible = 'off';
    msg{i} = text(ax(i), 10, 5 , '' ,  'FontSize', 7, 'FontUnits', 'normalized');
    
    % Notification sub plot
    if i == 9
      % Notifications are initiated from the server
      arrows{i}.CData = fromServer;
    elseif i == 1
      % Add title to the plot
      text(ax(i), 0, -50, 'BLE Heart Rate Profile Frames', 'FontSize', 20, ...
        'FontWeight', 'bold', 'Color', [0, 0.45, 0.74], ...
        'FontUnits', 'normalized', 'Tag', 'bleHRPFramesText');
      
      % Initialize the current transaction message
      transaction = text(ax(i), 0, -20, '', 'FontSize', 18, ...
        'FontWeight', 'bold', 'FontUnits', 'normalized');
    end
  end
  
  % Set the position to each subplot
  for i = 1:9
    msgSubPlot{10-i}.Position = [0.32 0.15+(0.07*i) 0.4 0.07];
  end
  
  % Client sub plot
  clientSP = subplot(1, 11, 1, ax(10));
  clientSP.Position = [0.1 0.1 0.22 0.9];
  clientSP.Tag = 'clientSPHere';
  axis(ax(10), [0 500 0 700]);
  hrpClient = imread('BLEHRPClient.png');
  imshow(hrpClient, 'Parent', clientSP);
  
  text(clientSP, 120, 440, {'Client', '(Smartphone)'}, 'FontSize', 14, ...
    'FontWeight', 'bold', 'FontUnits', 'normalized', 'Tag', 'smartphoneText', ...
    'HorizontalAlignment', 'center');
  
  heartRateMeasurement = text(clientSP, 75, 210, {'Fetching...'}, 'FontSize', 15, ...
    'FontWeight', 'bold', 'FontUnits', 'normalized', 'Tag', 'fetchingText');
  
  text(clientSP, 120, 310, {'Heart rate', 'measurement in', 'beats per minute'}, ...
    'FontSize', 12, 'FontWeight', 'bold', 'Color', [1 0.5 0], ...
    'FontUnits', 'normalized', 'Tag', 'inBeatsPerMinuteText', ...
    'HorizontalAlignment', 'center');
  
  % Server sub plot
  serverSP = subplot(1, 11, 11, ax(11));
  serverSP.Position = [0.75 0.1 0.22 0.8];
  axis(ax(11), [0 500 0 700]);
  hrpServer = imread('BLEHRPServer.png');
  imshow(hrpServer, 'Parent', serverSP);
  text(serverSP, 140, 420, ...
    {'Server', '(Heart rate sensor', 'wrist band)'},...
    'FontSize', 14, 'FontWeight', 'bold', 'FontUnits', 'normalized', ...
    'Tag', 'wristBandText', 'HorizontalAlignment', 'center')
end

if isempty(findobj('Tag', 'HRP'))
  return;
end

switch inp
  case 0
    % Update procedure
    transaction.String = val;
    pause(0.1);
    
  case 10
    % Update Heart rate measurement value at client
    heartRateMeasurement.String = val;
    heartRateMeasurement.Position = [105, 210];
    transaction.Visible = 'off';
    pause(1);
    
  otherwise
    % Message exchange between client and server
    if inp < 10 && inp > 0
      msg{inp}.String = val;
      msg{inp}.Tag = val;
      arrows{inp}.Visible = 'on';
      % Add slide effect
      for i=1:10
        if isempty(findobj('Tag', 'HRP'))
          break;
        end
        if rem(inp, 2) && inp ~= 9
          % Slide from left to right
          arrows{inp}.XData = [-200, 0] + (i*20);
        else
          % Slide from right to left
          arrows{inp}.XData = [200, 400] - (i*20);
        end
        pause(0.05);
      end
    end
end
