#include <QCoreApplication>
#include <QtSingleCoreApplication>
#include <QDBusConnection>
#include <QCommandLineParser>
#include <QDebug>
#include <QFile>

#include <ukui-log4qt.h>

#include "daemon.h"
#include "sessiondbusregister.h"


extern "C" {
    #include <string.h>
    #include <signal.h>
}

 #include "kysdk/kysdk-system/libkysysinfo.h"


void signalHandlerFunc(int signal) {
    fprintf(stderr,"---------------------- %d",signal);
}


static QString executeLinuxCmd(QString strCmd)
{
    QProcess p;
    p.start("bash", QStringList() <<"-c" << strCmd);
    p.waitForFinished();
    QString strResult = p.readAllStandardOutput();
    return strResult.toLower();
}

static void setEnvPCValue()
{
    unsigned int productFeatures = 0;
    char * projectName = "";
    char * projectSubName = "";
    productFeatures = kdk_system_get_productFeatures();
    projectName = kdk_system_get_projectName();
    QString qs_projectName = QString(projectName);
    projectSubName = kdk_system_get_projectSubName();
    QString qs_projectSubName = QString(projectSubName);

    envPC = Environment::NOMAL;

    QString str = executeLinuxCmd("cat /proc/cpuinfo | grep Hardware");
    if(str.length() == 0)
    {
        goto FuncEnd;
    }

    if(str.indexOf("huawei") != -1 || str.indexOf("pangu") != -1 ||
            str.indexOf("kirin") != -1)
    {
        envPC = Environment::HUAWEI;
    }

FuncEnd:
    if (QFile::exists("/etc/apt/ota_version"))
        envPC = Environment::LAIKA;
    else if(qs_projectName.contains("V10SP1-edu",Qt::CaseInsensitive) &&
            qs_projectSubName.contains("mavis",Qt::CaseInsensitive))
    {
        envPC = Environment::MAVIS;
    }

    qInfo () << Q_FUNC_INFO << envPC <<__LINE__;

}

int main(int argc, char *argv[])
{
    fprintf(stdout,"Program running.....\n");
    initUkuiLog4qt(QString("bluetoothserver"));

    setEnvPCValue();
    qDebug() << "envPC: "<< envPC;

    QtSingleCoreApplication a(argc, argv);
    a.setOrganizationName("Kylin Team");
    a.setApplicationName("dbus-bluetooth-service");
    a.setApplicationVersion("1.0.0");

    if (a.isRunning()) {
        qDebug() << "bus-bluetooth-service is Already running ! ! !";
        return EXIT_SUCCESS;
    } else {
//        signal(SIGKILL,signalHandlerFunc);
//        signal(SIGQUIT,signalHandlerFunc);

        QCommandLineParser parser;
        parser.addHelpOption();
        parser.addVersionOption();
        parser.addOptions({{{"o","obex"},QCoreApplication::translate("main","Load the Bluetooth file transfer agent")}});

        parser.setApplicationDescription(QCoreApplication::translate("main","UKUI bluetooth daemon"));
        parser.process(a);

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

    //    if (!systemBus.registerObject("/", "com.bluetooth.interface", new SysDbusRegister(), QDBusConnection::ExportAllSlots)){
    //        qCritical() << "QDbus register object failed reason:" << systemBus.lastError();
    //        //exit(2);
    //    }
    //    qDebug() << "ok";
        SessionDbusRegister *sessionDbus = new SessionDbusRegister();
        Q_UNUSED(sessionDbus);

        Daemon *daemon = new Daemon();
        Q_UNUSED(daemon);

        int quitValue = a.exec();

        delete sessionDbus;
        delete daemon;

        qInfo() << Q_FUNC_INFO << "Daemon program exit!!!!";

        return quitValue;
    }
}
