function faultBandsPlot(FB, info, domain, hAx)
% FAULTBANDSPLOT Fault bands plot to be used in conjunction with the
% FAULTBANDS, BEARINGFAULTBANDS, and GEARMESHFAULTBANDS commands

% Copyright 2020-2023 The MathWorks, Inc.

% Plots the fault frequency bands

if (nargin > 3) % used for Fault Bands live task
    ax = hAx;
else % axes is not provided
    ax = newplot;
    set(ax, 'NextPlot', 'replace');
    ax = gca;
end

title(ax, m('predmaint:plot:strFaultbandsTitle'));
if (nargin > 2) && (domain == "order")
  xlabel(ax, m('predmaint:plot:strFaultbandsXLabelOrd', '1/rev'))
else
  xlabel(ax, m('predmaint:plot:strFaultbandsXLabel'));
end

% The info.Labels may be a string array (in releases older than R2022b) or
% a cell array (in R2023a or newer). To avoid any backwards incompatibility
% issues, we need to ensure that from here on out it is a cell array.
info.Labels = cellstr(info.Labels);

% Plot frequency bands for each harmonic or fault group
for k = 1:size(FB,1)
  if isfield(info, 'HarmonicGroups')
    groups = info.HarmonicGroups;
  elseif isfield(info, 'FaultGroups')
    groups = info.FaultGroups;
  end
  
  % Assumption is that we want to use the standard extended color order of
  % 12 colors
  idx = rem(groups(k)-1, 12) + 1;
  str = regexp(info.Labels{k}, "[+-](\d*)", 'match'); % Extract sideband number
  if isempty(str)
    H = 1.5;
  else
    H = 1.5 / (abs(str2double(str)) + 0.5);
  end
  
  p = patch(ax, FB(k,[1 1 2 2]), [0 H H 0], [0 0 0], 'FaceAlpha', 0.8);
  controllib.plot.internal.utils.setColorProperty(p, ["FaceColor" "EdgeColor"], ...
      sprintf("--mw-graphics-colorOrder-%d-primary", idx));
  text(ax, mean(FB(k,:)), H*1.02, info.Labels{k}, ...
    'Rotation', 90, 'VerticalAlignment', 'middle');
end

ax.YLim = [0 2];
ax.YTick = [];
end


function s = m(id, varargin)
% Reads strings from the resource bundle
s = string(message(id, varargin{:}));
end
