/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#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 "device_monitor.h"
#include "launch_printer.h"
#include "common.h"
#include "log.h"
#include "tray_win.h"
#include "event_notify_monitor.h"
#include "device_map.h"
#include "pop_window_manager.h"
#include "find_and_match_ppds.h"
#include "printer_check_helper.h"

void addTrans()
{
//加载中文
    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 = QApplication::applicationDirPath() + "/.qm";
    }

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

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

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

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

    if(!trans_sdk.load(":/translations/gui_" + locale + ".qm"))
        qDebug() << "Load translations file" <<QLocale() << "failed!";
    else
        QApplication::installTranslator(&trans_sdk);
}

int main(int argc, char *argv[])
{
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    QApplication app(argc, argv);

    if (!processCheck("/usr/sbin/cupsd")) {
        qCritical() << "Cannot find cups service!";
        return 0;
    }

    if (!QSystemTrayIcon::isSystemTrayAvailable()) {
        qDebug()<<"I couldn't detect any system tray on this system now";
        return 1;
    }
    QApplication::setQuitOnLastWindowClosed(false);
#ifndef TEST_DEBUG
    qInstallMessageHandler(logOutput);
#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);
    }

    addTrans();

    qRegisterMetaType<QString>("QString&");
    qRegisterMetaType<DeviceInformation>("DeviceInformation");
    qRegisterMetaType<DeviceInformation>("DeviceInformation&");

    g_findAndMatchWorkThread->start();
    g_findAndMatchWorkThread->updatePPDsMap();

    qDebug () << "LaunchPrinter run!";
    LaunchPrinter::getInstance();
    PopWindowManager::getInstance();

    DeviceMonitorBackend *p = DeviceMonitorBackend::getInstance();
    QObject::connect (p,  &DeviceMonitorBackend::usbDeviceAdd,
                      LaunchPrinter::getInstance(), &LaunchPrinter::add);

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

#ifdef UNIT_TEST
    return 0;
#else
    return app.exec();
#endif
}
