#include "d578uv_test.hh"
#include "config.hh"
#include "d578uv.hh"
#include "d578uv_codeplug.hh"
#include "errorstack.hh"
#include <iostream>
#include <QTest>
#include "commercial_extension.hh"

D578UVTest::D578UVTest(QObject *parent)
  : UnitTestBase(parent)
{
  // pass...
}

void
D578UVTest::testBasicConfigEncoding() {
  ErrorStack err;
  Codeplug::Flags flags; flags.setUpdateCodeplug(false);
  D578UVCodeplug codeplug;
  if (! codeplug.encode(&_basicConfig, flags, err)) {
    QFAIL(QString("Cannot encode codeplug for AnyTone AT-D578UV: %1")
          .arg(err.format()).toStdString().c_str());
  }
}

void
D578UVTest::testBasicConfigDecoding() {
  ErrorStack err;
  Codeplug::Flags flags; flags.setUpdateCodeplug(false);
  D578UVCodeplug codeplug;
  if (! codeplug.encode(&_basicConfig, flags, err)) {
    QFAIL(QString("Cannot encode codeplug for AnyTone AT-D578UV: %1")
          .arg(err.format()).toStdString().c_str());
  }

  Config config;
  if (! codeplug.decode(&config, err)) {
    QFAIL(QString("Cannot decode codeplug for AnyTone AT-D578UV: %1")
          .arg(err.format()).toStdString().c_str());
  }
}

void
D578UVTest::testChannelFrequency() {
  ErrorStack err;
  Codeplug::Flags flags; flags.setUpdateCodeplug(false);
  D578UVCodeplug codeplug;
  codeplug.clear();
  if (! codeplug.encode(&_channelFrequencyConfig, flags, err)) {
    QFAIL(QString("Cannot encode codeplug for AnyTone D578UV: {}")
          .arg(err.format()).toStdString().c_str());
  }

  Config config;
  if (! codeplug.decode(&config, err)) {
    QFAIL(QString("Cannot decode codeplug for AnyTone D578UV: %1")
          .arg(err.format()).toStdString().c_str());
  }

  QCOMPARE(config.channelList()->channel(0)->rxFrequency(),
           Frequency::fromHz(123456780ULL));
  QCOMPARE(config.channelList()->channel(0)->txFrequency(),
           Frequency::fromHz(999999990ULL));
}


void
D578UVTest::testMicGain() {
  ErrorStack err;

  Codeplug::Flags flags; flags.setUpdateCodeplug(false);
  D578UVCodeplug codeplug;
  codeplug.clear();

  Config config; config.copy(_basicConfig);
  config.settings()->setMicLevel(10);
  if (! codeplug.encode(&config, flags, err)) {
    QFAIL(QString("Cannot encode codeplug for AnyTone D578UV: {}")
            .arg(err.format()).toStdString().c_str());
  }

  Config copy;
  if (! codeplug.decode(&copy, err)) {
    QFAIL(QString("Cannot decode codeplug for AnyTone D578UV: %1")
            .arg(err.format()).toStdString().c_str());
  }

  QCOMPARE(copy.settings()->micLevel(), 10);
  QVERIFY(copy.settings()->anytoneExtension());
  // FM mic gain enabled only if it differs from DMR gain
  QVERIFY(! copy.settings()->anytoneExtension()->audioSettings()->fmMicGainEnabled());
  QCOMPARE(copy.settings()->anytoneExtension()->audioSettings()->fmMicGain(), 10);
}

QTEST_GUILESS_MAIN(D578UVTest)

