#include "src/view/mainwindow.h"
#include "src/frame/configuration.h"
#include "src/frame/logger.h"
#include "src/model/logicmain.h"
#include "src/model/monitorcdrom.h"
#include "src/model/menufactory.h"
#include "src/model/logiccopyimage.h"
#include "src/view/modaldialog.h"
#include "src/view/execdialog.h"
#include "src/view/filefilterrules.h"
#include "src/view/viewfilterfiles.h"
#include "src/view/kybaboutdialog.h"
#include "src/view/tip_burnresult.h"
#include "src/po/translation.h"

#include <QScreen>
#include <QApplication>
#include <QDesktopWidget>
#include <QCommandLineParser>
#include <ukui-log4qt.h>
#include <QTranslator>
#include <QLibraryInfo>
#include <kwidget.h>
#include <ukuistylehelper/ukuistylehelper.h>
#include <windowmanager/windowmanager.h>
#include <kysdk/diagnosetest/libkydatacollect.h>
using namespace kdk;

int main(int argc, char *argv[])
{
    //在程序启动时调用，不需要传入参数
    kdk_dia_upload_program_startup();

    QCommandLineParser *parser;

    QTranslator trans;
    // locale
    _KYBi18nLoad("kylin-burner", "/usr/share/locale/");
    _KYBi18nDomain("kylin-burner");

    // 4K
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    QApplication a(argc, argv);

    // QT自身标准的翻译
#ifndef QT_NO_TRANSLATION
    QString locale = QLocale::system().name();
    QString translatorFileName = QLatin1String("qt_");
    translatorFileName += locale;
    QTranslator *selfTransOfQt = new QTranslator();
    if (selfTransOfQt->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
        a.installTranslator(selfTransOfQt);
    else
        qDebug() << "load qt translator file failed!";
#endif
    //initUkuiLog4qt("burner");

    CFG->init();
    //EverTime Burner Start, init [DataSetting]--Duplicate Data/Add Data/Delete Data/clean Data
    //it can reduce some error when user some operation
    CFG->set("DataSetting", "Duplicate Data", 0);
    CFG->set("DataSetting", "Add Data", false);
    CFG->set("DataSetting", "Delete Data", true);
    CFG->set("DataSetting", "Clean Data", true);

    CFG->set("FilterFile","hidden",false);
    CFG->set("FilterFile","broken",false);
    CFG->set("FilterFile","Repleace",false);

    LOGGER->init();
    MENU->init();
    MODAL->init();
    TIP_BURNRESULT->init();
    FILTERRULES->init();
    VIEWFILTERFILE->init();
    MAIN->init();
    COPY_IMGAE->init();
    EXEC->init();

    parser = new QCommandLineParser;
    parser->addPositionalArgument( "urls", _("file(s) to open"), "[urls...]" );
    parser->addOption(QCommandLineOption("d", _("Running as debug model.")));
    parser->process(a);
    if (parser->isSet("d"))
        CFG->set("logger","level", 4);
    else
        CFG->set("logger","level", 1);

    //图标库主题的刻录图片名换为burner
    //a.setWindowIcon(QIcon::fromTheme("disk-burner"));
    a.setWindowIcon(QIcon::fromTheme("burner"));
    QCoreApplication::setApplicationName(_("Burner"));

    //判断是否是wayland环境
    bool isWayland = false;
    if(QString(qgetenv("XDG_SESSION_TYPE")) == "wayland") {
        qputenv("QT_QPA_PLATFORM", "wayland");
        isWayland = true;
    }

//    MainWindow w(parser->positionalArguments(), nullptr);
//    if(isWayland) {
        // set window position
//        int sw = QGuiApplication::primaryScreen()->availableGeometry().width();
//        int sh = QGuiApplication::primaryScreen()->availableGeometry().height();
//        kdk::WindowManager::setGeometry(w.windowHandle(), QRect((sw-w.width())/2, (sh-w.height())/2, w.width(), w.height()));
//    }

//    w.show();
        MainWindow::Instance(parser->positionalArguments(), nullptr);
        kdk::UkuiStyleHelper::self()->removeHeader(MainWindow::Instance());
    MainWindow::Instance()->show();
    MODAL->setAttribute(Qt::WA_NativeWindow);
    MODAL->windowHandle()->setTransientParent(MainWindow::Instance()->windowHandle());
    FILTERRULES->setAttribute(Qt::WA_NativeWindow);
    FILTERRULES->windowHandle()->setTransientParent(MainWindow::Instance()->windowHandle());
    EXEC->setAttribute(Qt::WA_NativeWindow);
    EXEC->windowHandle()->setTransientParent(MainWindow::Instance()->windowHandle());
    TIP_BURNRESULT->setAttribute(Qt::WA_NativeWindow);
    TIP_BURNRESULT->windowHandle()->setTransientParent(MainWindow::Instance()->windowHandle());
    VIEWFILTERFILE->setAttribute(Qt::WA_NativeWindow);
    VIEWFILTERFILE->windowHandle()->setTransientParent(MainWindow::Instance()->windowHandle());
    kdk::WindowManager::setGeometry(MainWindow::Instance()->windowHandle(),
                                    QRect(qApp->desktop()->geometry().center() - MainWindow::Instance()->geometry().center(),
                                    QSize(MainWindow::Instance()->size())));
    KYBAboutDialog::instance(MainWindow::Instance());
    ABOUT->init();
    MAIN->initISO();
    MONITOR->start();

    return a.exec();
}
