#ifndef RADIOSETTINGS_HH
#define RADIOSETTINGS_HH

#include "configobject.hh"
#include "channel.hh"

#include "gnsssettings.hh"
#include "dmrsettings.hh"
#include "radioddity_extensions.hh"
#include "anytone_settingsextension.hh"
#include "tyt_extensions.hh"


/** Represents the common radio-global settings.
 * @ingroup conf */
class RadioSettings : public ConfigItem
{
  Q_OBJECT
  /** The first intro line. */
  Q_PROPERTY(QString introLine1 READ introLine1 WRITE setIntroLine1)
  /** The second intro line. */
  Q_PROPERTY(QString introLine2 READ introLine2 WRITE setIntroLine2)
  /** The mic amplification level. */
  Q_PROPERTY(Level micLevel READ micLevel WRITE setMicLevel)
  /** Speech synthesis flag. */
  Q_PROPERTY(bool speech READ speech WRITE enableSpeech)
  /** The default channel power */
  Q_PROPERTY(Channel::Power power READ power WRITE setPower)
  /** The squelch level. */
  Q_PROPERTY(Level squelch READ squelch WRITE setSquelch)
  /** The default vox sensitivity */
  Q_PROPERTY(Level vox READ vox WRITE setVOX)
  /** The default transmit timeout */
  Q_PROPERTY(Interval tot READ tot WRITE setTOT SCRIPTABLE false)
  /** The default DMR radio ID. */
  Q_PROPERTY(DMRRadioIDReference *defaultID READ defaultIdRef)
  /** The GNSS settings. */
  Q_PROPERTY(GNSSSettings *gnss READ gnss);
  /** The common DMR settings. */
  Q_PROPERTY(DMRSettings *dmr READ dmr);
  /** The settings extension for TyT devices. */
  Q_PROPERTY(TyTSettingsExtension* tyt READ tytExtension WRITE setTyTExtension)
  /** The settings extension for Radioddity devices. */
  Q_PROPERTY(RadiodditySettingsExtension * radioddity READ radioddityExtension WRITE setRadioddityExtension)
  /** Settings for AnyTone devices. */
  Q_PROPERTY(AnytoneSettingsExtension *anytone READ anytoneExtension WRITE setAnytoneExtension)

public:
  /** Default constructor. */
  explicit RadioSettings(QObject *parent=nullptr);

  bool copy(const ConfigItem &other);
  ConfigItem *clone() const;

  /** Resets the settings. */
  void clear();

  /** Returns the first intro line. */
  const QString &introLine1() const;
  /** (Re-)Sets the first intro line. */
  void setIntroLine1(const QString &line);

  /** Returns the second intro line. */
  const QString &introLine2() const;
  /** (Re-)Sets the second intro line. */
  void setIntroLine2(const QString &line);

  /** Returns the MIC amplification level [1,10]. */
  Level micLevel() const;
  /** (Re-)Sets the MIC amplification level [1,10]. */
  void setMicLevel(Level value);

  /** Returns @c true if the speech synthesis is enabled. */
  bool speech() const;
  /** Enables/disables the speech synthesis. */
  void enableSpeech(bool enabled);

  /** Returns the default squelch level [0-10]. */
  Level squelch() const;
  /** Sets the default squelch level. */
  void setSquelch(Level squelch);

  /** Returns the default channel power. */
  Channel::Power power() const;
  /** Sets the default channel power. */
  void setPower(Channel::Power power);

  /** Returns @c true if VOX is disabled by default. */
  bool voxDisabled() const;
  /** Returns the default VOX level [0-10], 0=disabled. */
  Level vox() const;
  /** Sets the default VOX level [0-10], 0=disabled. */
  void setVOX(Level level);
  /** Disables VOX by default. */
  void disableVOX();

  /** Returns @c true if the transmit timeout (TOT) is disabled. */
  bool totDisabled() const;
  /** Returns the default transmit timeout (TOT) in seconds, 0=disabled. */
  Interval tot() const;
  /** Sets the default transmit timeout (TOT) in seconds, 0=disabled. */
  void setTOT(const Interval &sec);
  /** Disables the transmit timeout (TOT). */
  void disableTOT();

  /** Returns a reference to the default DMR radio Id. */
  DMRRadioIDReference *defaultIdRef() const;
  /** Returns the default DMR ID or nullptr, if non is set. */
  DMRRadioID *defaultId() const;
  /** Sets the default DMR ID. */
  void setDefaultId(DMRRadioID *id);

  /** Returns the GNSS settings. */
  GNSSSettings *gnss() const;
  /** Returns the DMR settings. */
  DMRSettings *dmr() const;

  /** Returns the TyT device specific radio settings. */
  TyTSettingsExtension *tytExtension() const;
  /** Sets the TyT device specific radio settings. */
  void setTyTExtension(TyTSettingsExtension *ext);

  /** Returns the Radioddity device specific radio settings. */
  RadiodditySettingsExtension *radioddityExtension() const;
  /** Sets the Radioddity device specific radio settings. */
  void setRadioddityExtension(RadiodditySettingsExtension *ext);

  /** Returns the AnyTone device specific radio settings. */
  AnytoneSettingsExtension *anytoneExtension() const;
  /** Sets the AnyTone device specific radio settings. */
  void setAnytoneExtension(AnytoneSettingsExtension *ext);

  bool parse(const YAML::Node &node, Context &ctx, const ErrorStack &err=ErrorStack());

protected:
  bool populate(YAML::Node &node, const Context &context, const ErrorStack &err=ErrorStack());

protected slots:
  /** Internal used callback whenever an extension is modified. */
  void onExtensionModified();

protected:
  /** Holds the first intro line. */
  QString _introLine1;
  /** Holds the second intro line. */
  QString _introLine2;
  /** Holds the mic amplification level. */
  Level _micLevel;
  /** Holds the speech synthesis flag. */
  bool _speech;
  /** Holds the global squelch setting. */
  Level _squelch;
  /** Holds the global power setting. */
  Channel::Power _power;
  /** Holds the global VOX level. */
  Level _vox;
  /** Holds the global transmit timeout. */
  Interval _transmitTimeOut;
  /** Reference to the default DMR radio ID. */
  DMRRadioIDReference *_defaultId;
  /** The GNSS settings. */
  GNSSSettings *_gnss;
  /** The DMR settings. */
  DMRSettings *_dmr;
  /** Device specific settings extension for TyT devices. */
  TyTSettingsExtension *_tytExtension;
  /** Device specific settings extension for Radioddity devices. */
  RadiodditySettingsExtension *_radioddityExtension;
  /** Device specific settings extension for AnyTone devices. */
  AnytoneSettingsExtension *_anytoneExtension;
};

#endif // RADIOCONFIG_HH
