#include "mainprogram.h"

#include <QApplication>
#include <QtSingleApplication>
#include <QTranslator>
#include <QLibraryInfo>
#include <QStyleFactory>
#include <QTextCodec>
#include <QDebug>
#include <QTranslator>
#include <QLibraryInfo>
#include <ukui-log4qt.h>
#include "kysdk/kysdk-system/libkysysinfo.h"

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

void setEnvPCValue()
{
    qDebug() << Q_FUNC_INFO <<__LINE__;
    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[])
{
    initUkuiLog4qt("ukui-bluetooth");

    setEnvPCValue();
    qDebug() << "Ishuawei: "<< (envPC == Environment::HUAWEI);

    //    QApplication::setStyle(QStyleFactory::create("ukui-default"));
        QApplication::setQuitOnLastWindowClosed(false);
        QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
        QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
    #endif

    QtSingleApplication app(argc, argv);

    if (app.isRunning()) {
        qDebug() << "ukui-bluetooth is Already running ! ! !";
        return EXIT_SUCCESS;
    }
    else
    {
        QTranslator *t = new QTranslator();
        t->load("/usr/share/libpeony-qt/libpeony-qt_"+QLocale::system().name() + ".qm");
        QApplication::installTranslator(t);

        //安装ukui-bluetooth的中文翻译
        QTranslator * translator = new QTranslator();;
        translator->load("/usr/share/ukui-bluetooth/translations/ukui-bluetooth_" + QLocale::system().name() + ".qm");
        app.installTranslator(translator);

        //安装Qt中文翻译
        QTranslator * qt_translator = new QTranslator();
        QString qtTransPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
        if (qt_translator->load(QLocale(), "qt", "_", qtTransPath))
            app.installTranslator(qt_translator);

        MainProgram *w;
        bool intel;
        if (envPC == Environment::LAIKA || envPC == Environment::MAVIS)
            intel = true;
        else
            intel = false;
        w = new MainProgram(intel);
        if( w->exit_flag ){
            return 0;
        }
        return app.exec();
    }
}
