#include <QApplication>
#include <QObject>
#include <QDebug>
#include <QThread>
#include <QTranslator>
#include <QLocale>
#include <QStandardPaths>
#include <QLibraryInfo>
#include <QMutex>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QObject>

#include <sys/inotify.h>
#include <fcntl.h>
#include <syslog.h>

#include <ukui-log4qt.h>

#include "device_monitor.h"
#include "launch_printer.h"
#include "common.h"
#include "tray_win.h"
#include "event_notify_monitor.h"
#include "device_map.h"

int main(int argc, char **argv) {

    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    QApplication app(argc, argv);

    if (!QSystemTrayIcon::isSystemTrayAvailable()) {
        qDebug()<<"I couldn't detect any system tray on this system now";
        return 1;
    }
    QApplication::setQuitOnLastWindowClosed(false);
#ifndef TEST_DEBUG
    initUkuiLog4qt("kylin-printer-applet");
#else
    qInstallMessageHandler(myMessageOutput);
#endif
    //检测单例，调用dbus接口
    QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
    int fd = open(QString(homePath.at(0) + "/.config/kylin-printer-applet%1.lock").arg(getenv("DISPLAY")).toUtf8().data(),
                  O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (fd < 0)
    {
        exit(1);
    }
    if (lockf(fd, F_TLOCK, 0))
    {
        syslog(LOG_ERR, "Can't lock single file, kylin-printer-applet is already running!");
        exit(0);
    }
    QString qtTranslationsPath;
    qtTranslationsPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);// /usr/share/qt5/translations
    QString kylinPrinterTranslationsPath;
#ifndef TEST_DEBUG
    if (QDir("/usr/share/kylin-printer/translations").exists()) {
        kylinPrinterTranslationsPath = "/usr/share/kylin-printer/translations";
    }
    else {
        kylinPrinterTranslationsPath = app.applicationDirPath() + "/.qm";
    }

#else
    kylinPrinterTranslationsPath = QString(PROJECT_SOURCE_DIR) + "/translations";
#endif
    qDebug () << kylinPrinterTranslationsPath;
    QString locale = QLocale::system().name();
    QTranslator trans_global, trans_menu, trans_dict;

    if(!trans_global.load("kylin-printer_" + locale + ".qm", kylinPrinterTranslationsPath))
        qDebug() << "Load translations file" <<QLocale() << "failed!";
    else
        app.installTranslator(&trans_global);

    if(!trans_dict.load("kylin-printer_dict_" + locale + ".qm", kylinPrinterTranslationsPath))
        qDebug() << "Load translations file" <<QLocale() << "failed!";
    else
        app.installTranslator(&trans_dict);

    if(!trans_menu.load("qt_" + locale + ".qm", qtTranslationsPath))
        qDebug() << "Load translations file" <<QLocale() << "failed!";
    else
        app.installTranslator(&trans_menu);

    qDebug () << "LaunchPrinter run!";
    LaunchPrinter *lp = new LaunchPrinter();
    
    DeviceMonitorBackend *p = DeviceMonitorBackend::getInstance();
    // p->setFindPrinterOnly = false;
    QObject::connect (p,  &DeviceMonitorBackend::usbDeviceAdd,
                      lp, &LaunchPrinter::add);

    QObject::connect (p,  &DeviceMonitorBackend::usbDeviceRemove,
                      lp, &LaunchPrinter::remove);
    emit DeviceMonitorBackend::getInstance()->start();
#if 0
    TrayWin tray;
#endif
    EventNotifyMonitor *notify = EventNotifyMonitor::getInstance();
#if 0
    QObject::connect (notify,  &EventNotifyMonitor::getNotify,
                      &tray, &TrayWin::showNotify);
#endif
    emit notify->start();
    
    lp->findPrePrinter();
    DeviceMap::getInstance();
    return app.exec();
}
