#include "mainwindow.h"

#include <QDesktopWidget>
#include <QApplication>
#include <QScreen>
#include <QIcon>

#include "mainwindow.h"
#include "global/translator.h"
#include "global/global.h"
#include "global/functions.h"
#include "global/globalconfig.h"

#include <kabase/log.hpp>

int main(int argc, char *argv[])
{
    qInstallMessageHandler(kdk::kabase::Log::logOutput);
    Global::initGlobal();

    if(QString(qgetenv("XDG_SESSION_TYPE")) == "wayland")
    {
#if 0
        qputenv("QT_QPA_PLATFORM", "wayland");
#endif
    }
    else {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
        QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
    }

    if (!Functions::isVirtualSuppotGpu() && GlobalConfig::getInstance()->hardwareType() != GlobalConfig::Nvidia_VDPAU_COPY)
        qputenv("QT_XCB_GL_INTEGRATION", "xcb_egl");

    QApplication a(argc, argv);
    a.setWindowIcon(QIcon::fromTheme("kylin-video"));

    // Translator
    Translator *translator = new Translator();
    translator->load("");

    // 加载 sdk 翻译
    QTranslator *tran = new QTranslator;
    if (QLocale::system().name() == "zh_CN")
        tran->load(":/translations/gui_zh_CN.qm");
    else if (QLocale::system().name() == "bo_CN")
        tran->load(":/translations/gui_bo_CN.qm");
    else if (QLocale::system().name() == "zh_HK")
        tran->load(":/translations/gui_zh_HK.qm");
    else if (QLocale::system().name() == "mn")
        tran->load(":/translations/gui_mn.qm");
    a.installTranslator(tran);

    QStringList arg_list;
    for (int i=1; i<argc; i++)
    {
        if (QString(argv[i]).startsWith("http"))
        {
            arg_list << QString(argv[i]);
            continue;
        }
        QFileInfo f(QString(argv[i]));
        if(f.isFile())
            arg_list << f.absoluteFilePath();
    }

    QString systemLang = QLocale::system().name();
    if(systemLang == "ug_CN" || systemLang == "ky_KG" || systemLang == "kk_KZ" ){
        Global::isHaWeiKe= true;
    }else{
        Global::isHaWeiKe= false;
//        Global::isHaWeiKe= true;
    }

    MainWindow w(arg_list);
    w.show();
    int screen_num = qApp->desktop()->screenNumber(&w);
    kdk::WindowManager::setGeometry(w.windowHandle(),
                                    QRect(qApp->desktop()->screenGeometry(screen_num).center() - w.geometry().center(),
                                    QSize(w.size())));

    return a.exec();
}
