classdef bluetoothSourceDialog < wirelessAppContainer.sources.BitSourcePNLengthDialog
 %

  % Copyright 2022-2023 The MathWorks, Inc.
  properties (Dependent)
    FixedPayloadLength
  end

  properties (Hidden)
    FixedPayloadLengthType = 'text'
    FixedPayloadLengthGUI
    FixedPayloadLengthLabel
  end

  methods
    function obj = bluetoothSourceDialog(parent)
      obj@wirelessAppContainer.sources.BitSourcePNLengthDialog(parent); % call base constructor
      obj.NumBitsGUI.(obj.Callback) = @(a, b) PayloadLengthChanged(obj, []);
      setupDialog(obj);
    end
    function props = displayOrder(~)
        props = {'InputSource'; 'NumBits';'FixedPayloadLength'; 'InputValue'};
    end
    function restoreDefaults(obj)
      obj.InputSource = 'User-defined';
      obj.FixedPayloadLength = '18';
    end
    function setupDialog(obj)
        obj.NumBitsLabel.(obj.TextValue) = getString(message('bluetooth:waveformGeneratorApp:PayloadLength'));
        obj.NumBits = 18;
        PayloadLengthChanged(obj);
        setupDialog@wirelessAppContainer.sources.BitSourcePNLengthDialog(obj); % layout panel and uicontrols
    end

    % Payload length
    function PayloadLengthChanged(obj, ~)
      try
        val = obj.NumBits;
        validateattributes(val,{'double'},{'nonnan','real','integer',...
        'nonnegative','finite','scalar'},mfilename,'PayloadLength');
        PLValueStr = num2str(val);
        obj.InputValue = ['randi([0 1],' PLValueStr '*8,1)'];
      catch e
        obj.errorFromException(e);
      end
    end

    function InputSourceChanged(obj, ~)
      valNB = obj.FixedPayloadLengthGUI.Visible;
      set([obj.NumBitsLabel obj.NumBitsGUI], 'Visible', ~valNB)
      valIS = strcmp(obj.InputSource, 'User-defined');
      set([obj.InputValueLabel obj.InputValueGUI], 'Visible', valIS)
    end

    % Fixed payload length
    function n = get.FixedPayloadLength(obj)
      n = getTextNumVal(obj, 'FixedPayloadLength');
    end
    function set.FixedPayloadLength(obj, val)
      setTextVal(obj, 'FixedPayloadLength', val);
      PLValueStr = num2str(val);
      obj.InputValue = ['randi([0 1],' PLValueStr '*8,1)'];
    end

    function addInputCode(obj, sw)
      numBitsStr = num2str(obj.NumBits*8);
      addInputCode@wirelessAppContainer.sources.BitSourceDialog(obj, sw, numBitsStr);
    end

    function str = getCatalogPrefix(~)
      str = 'bluetooth:waveformGeneratorApp:';
    end
   end
end
