#include "sysdbusregister.h"

extern "C" {
#include <string>
#include <glib.h>
#include <glib/gprintf.h>
}

SysDbusRegister::SysDbusRegister()
{
    qInfo() << Q_FUNC_INFO ;

    if (InterfaceAlreadyExists()) {
        QDBusConnection systemBus = QDBusConnection::systemBus();
        if (!systemBus.registerService("com.bluetooth.systemdbus")) {
            qCritical() << "QDbus register service failed reason:" << systemBus.lastError();
        }

        if (!systemBus.registerObject("/", "com.bluetooth.interface", this, QDBusConnection::ExportAllSlots)){
            qCritical() << "QDbus register object failed reason:" << systemBus.lastError();
        }
    }
}

SysDbusRegister::~SysDbusRegister()
{

}

int SysDbusRegister::exitService()
{
    qApp->exit(0);
    return 0;
}

QString SysDbusRegister::writeKeyFile(QString devAddress, QString devName, qint32 type)
{
    qDebug() << Q_FUNC_INFO ;
    if(devAddress.isNull())
        return QString("Address can not be empty ! ! !");
    if(devName.isNull())
        return QString("Name can not be empty ! ! !");
    if(devAddress.at(2) != ":" ||
       devAddress.at(5) != ":" ||
       devAddress.at(8) != ":" ||
       devAddress.at(11) != ":" ||
       devAddress.at(14) != ":")
        return QString("arg0 is not an address ! ! !");

    qDebug() << Q_FUNC_INFO << QFile::exists(LIST_PATH);
    if(!QFile::exists(LIST_PATH)){
        QFile file(LIST_PATH);
        file.open(QIODevice::WriteOnly|QIODevice::Text);
        file.close();
    }

    GKeyFile *key_file = nullptr;
    key_file = g_key_file_new();
    char *data;
    gsize length = 0;
    g_key_file_load_from_file(key_file,QString(LIST_PATH).toStdString().c_str(),G_KEY_FILE_NONE,NULL);
    g_key_file_set_string(key_file,devAddress.toStdString().c_str(),"Name",devName.toStdString().c_str());
    g_key_file_set_string(key_file,devAddress.toStdString().c_str(),"Type",QString("%1").arg(type).toStdString().c_str());
    g_key_file_set_string(key_file,devAddress.toStdString().c_str(),"ConnectTime",QString::number(QDateTime::currentMSecsSinceEpoch() / 1000).toStdString().c_str());


    data = g_key_file_to_data(key_file,&length,NULL);
    g_file_set_contents(QString(LIST_PATH).toStdString().c_str(),data,length,NULL);
    g_free(data);

    g_key_file_free(key_file);

    return QString("Key write ok!!!");
}

QString SysDbusRegister::removeKeyFile(QString devAddress)
{
    qDebug() << Q_FUNC_INFO ;
    if(devAddress.isNull())
        return QString("Address can not be empty ! ! !");
    if(devAddress.at(2) != ":" ||
       devAddress.at(5) != ":" ||
       devAddress.at(8) != ":" ||
       devAddress.at(11) != ":" ||
       devAddress.at(14) != ":")
        return QString("arg0 is not an address ! ! !");

    if(!QFile::exists(LIST_PATH)){
        QFile file(LIST_PATH);
        file.open(QIODevice::WriteOnly|QIODevice::Text);
        file.close();
    }

    GKeyFile *key_file = nullptr;
    char *data;
    gsize length = 0;
    GError *error;
    key_file = g_key_file_new();
    g_key_file_load_from_file(key_file,QString(LIST_PATH).toStdString().c_str(),G_KEY_FILE_NONE,NULL);
    if(g_key_file_has_group(key_file,devAddress.toStdString().c_str())){
        g_key_file_remove_group(key_file,devAddress.toStdString().c_str(),&error);
        data = g_key_file_to_data(key_file,&length,NULL);
        g_file_set_contents(QString(LIST_PATH).toStdString().c_str(),data,length,NULL);
        g_free(data);
    }
    g_key_file_free(key_file);

    return QString("Key remove ok!!!");
}

QString SysDbusRegister::getKeyFilePath()
{
    return QString(LIST_PATH);
}

bool SysDbusRegister::InterfaceAlreadyExists()
{
    QDBusConnection conn = QDBusConnection::systemBus();
    if (!conn.isConnected())
        return 0;

    QDBusReply<QString> reply = conn.interface()->call("GetNameOwner", "com.ukui.bluetooth");
    return reply.value() == "";
}
