/*
 * Copyright (C) 2017 ~ 2017 Deepin Technology Co., Ltd.
 *
 * Author:     Iceyer <me@iceyer.net>
 *
 * Maintainer: Iceyer <me@iceyer.net>
 *
 * 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
 * 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 <http://www.gnu.org/licenses/>.
 */

#include <QIcon>
#include <QDBusConnection>
#include <QDBusInterface>

#include <DLog>
#include <DApplication>

#include "activatorcontrol.h"

using namespace Dtk::Widget;
using namespace Dtk::Core;

int main(int argc, char *argv[])
{
    if (QFile("/usr/lib/dde-desktop/plugins/platform/libdxcb.so").exists()) {
        qDebug() << "load dxcb from local path: /usr/lib/dde-desktop/plugins/platform/libdxcb.so";
        qputenv("QT_QPA_PLATFORM_PLUGIN_PATH", "/usr/lib/dde-desktop/plugins/platform");
    }

    DApplication::loadDXcbPlugin();

    DApplication app(argc, argv);

    app.setTheme("light");
    app.setOrganizationName("deepin");
    app.setApplicationName("deepin-activator");
    app.setQuitOnLastWindowClosed(false);
//    app.setWindowIcon(QIcon(":/view/resources/round_logo_64.svg"));
    app.setQuitOnLastWindowClosed(true);

    DLogManager::registerConsoleAppender();
    DLogManager::registerFileAppender();

    app.loadTranslator();

    QCommandLineParser parser;
    parser.setApplicationDescription("deepin activator");
    parser.addHelpOption();
    parser.addVersionOption();

    QCommandLineOption hideOption(QStringList() << "m" << "mini");
    parser.addOption(hideOption);

    QCommandLineOption trayOption(QStringList() << "t" << "tray");
    parser.addOption(trayOption);

    parser.process(app);

    ActivatorControl control;

    if (!QDBusConnection::sessionBus().registerService(ACTIVATOR_SERVICE_NAME)
            || !QDBusConnection::sessionBus().registerObject(ACTIVATOR_SERVICE_PATH, &control,
                    QDBusConnection::ExportScriptableSlots)) {
        qDebug() << "registerService Failed, Another activator maybe be running!"
                 << QDBusConnection::sessionBus().lastError();
        if (parser.isSet(hideOption)) {
            exit(0x0000);
        }

        QDBusInterface activator(ACTIVATOR_SERVICE_NAME,
                                 ACTIVATOR_SERVICE_PATH,
                                 ACTIVATOR_SERVICE_IFC,
                                 QDBusConnection::sessionBus());
        activator.call(QDBus::AutoDetect, "Show");
        exit(0x0000);
    }

//    QSystemTrayIcon *systemTray = nullptr;

//    auto autoshow = !parser.isSet(hideOption);
//    auto showTray = parser.isSet(trayOption);
//    if (showTray) {
//        systemTray = new QSystemTrayIcon(QPixmap(":/view/resources/red_deepin.png"), &app);
//        systemTray->connect(systemTray, &QSystemTrayIcon::activated,
//        &app, [ = ](QSystemTrayIcon::ActivationReason /*reason*/) {
//            Q_EMIT control->show();
//        });
//    }

//    control->connect(control, &ActivatorControl::initialized,
//    control, [ = ](const LicenseModel & model) {
//        if (!model.license.actived) {
//            control->notifyActivator();
//        } else {
//            if (systemTray) {
//                systemTray->setIcon(QPixmap(":/view/resources/green_deepin.png"));
//            }
//        }
//    });
//    control->connect(control, &ActivatorControl::show,
//    &app, [ = ]() {
//        // no need to call show
//        if (!m_activeWindow) {
//            m_activeWindow = new ActivatorWindow(control, systemTray, autoshow);

//        }
//        m_activeWindow->trayActivated(QSystemTrayIcon::Trigger);
//    });

//    if (systemTray) {
//        systemTray->show();
//    }

//    control.installTray();
//    control.installIndicator();

    return app.exec();
}
