#include "sacnexplorer.h"

#include <QDebug>
#include <QSettings>

SacnExplorer::SacnExplorer(int argc, char *argv[])
  : QApplication(argc, argv)
{
  setOrganizationName("Company235");
  setOrganizationDomain("company235.com");
  setApplicationName(tr("sACN Explorer"));

  loadSettings();

  node_ = new QSacnNode(this, cid_);
  qDebug() << "sACN node started with CID" << node_->cid().string().c_str();
  node_->assignUserName(applicationName().toStdString());
  qDebug() << "Starting sACN discovery.";
  node_->discoveryStart();

  window = new MultiverseView(nullptr, node_);
  window->show();
}


SacnExplorer::~SacnExplorer()
{
  saveSettings();

  delete window;
  delete node_;
}

/**
 * @brief MultiverseView::loadSettings
 */
void SacnExplorer::loadSettings()
{
    QSettings settings;
    qDebug() << "Loading application settings from" << settings.fileName();

    settings.beginGroup("acn");
    cid_ = QUuid(settings.value("cid",
                                QUuid::createUuid().toString()).toString());
    qDebug() << "Persistent CID is" << cid_.toString().toStdString().c_str();
    settings.endGroup();
}


/**
 * @brief MultiverseView::saveSettings
 */
void SacnExplorer::saveSettings()
{
    QSettings settings;
    qDebug() << "Saving application settings to" << settings.fileName();

    settings.beginGroup("acn");
    settings.setValue("cid", cid_.toString());
    settings.endGroup();
}

