/*
 * 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 "licensehelper.h"

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>      /* open */
#include <unistd.h>     /* exit */
#include <sys/ioctl.h>  /* ioctl */

#include <QDebug>
#include <QFile>
#include <QTemporaryFile>
#include <QtDBus>

LicenseHelper::LicenseHelper(QObject *parent) : QObject(parent)
{
    licenseAgent = new QDBusInterface("com.deepin.license",
                                      "/com/deepin/license/Agent",
                                      "com.deepin.license.Agent",
                                      QDBusConnection::systemBus());

    licenseInfo = new QDBusInterface("com.deepin.license",
                                     "/com/deepin/license/Info",
                                     "com.deepin.license.Info",
                                     QDBusConnection::systemBus());

    connect(licenseInfo, SIGNAL(LicenseStateChange(quint32)),
            this, SIGNAL(activeStateChanged(quint32)));
}

quint32 LicenseHelper::licenseState()
{
    return licenseInfo->property("LicenseState").toUInt();
}

QString LicenseHelper::organization()
{
    return licenseInfo->property("Organization").toString();
}

QString LicenseHelper::product()
{
    return licenseInfo->property("Product").toString();
}

QString LicenseHelper::copyright()
{
    return licenseInfo->property("Copyright").toString();
}

QString LicenseHelper::edition()
{
    return licenseInfo->property("Edition").toString();
}

QString LicenseHelper::machineCode()
{
    return licenseInfo->property("MachineCode").toString();
}

QDateTime LicenseHelper::validityBegin()
{
    QString timeString = licenseInfo->property("ValidityBegin").toString();
    return QDateTime::fromString(timeString, Qt::ISODate);
}

QDateTime LicenseHelper::validityEnd()
{
    QString timeString = licenseInfo->property("ValidityEnd").toString();
    return QDateTime::fromString(timeString, Qt::ISODate);
}

int LicenseHelper::trialRemind()
{
    return licenseAgent->property("TrialRemind").toInt();
}

QString LicenseHelper::systemCopyright()
{
    return licenseAgent->property("Copyright").toString();
}

QString LicenseHelper::systemEdition()
{
    return licenseAgent->property("Edition").toString();
}

QString LicenseHelper::systemMachinecode()
{
    return licenseAgent->property("MachineCode").toString();
}

QString LicenseHelper::systemVersion()
{
    return licenseAgent->property("Version").toString();
}

bool LicenseHelper::exportInformation(const QString &infoFilename)
{
    QVariantMap info;
    info["machine_code"] = systemMachinecode();
    QJsonDocument josnDoc = QJsonDocument::fromVariant(info);

    QFile infoFile(infoFilename);
    infoFile.open(QIODevice::Truncate | QIODevice::ReadWrite);
    infoFile.write(josnDoc.toJson());
    infoFile.close();
    return true;
}

quint32 LicenseHelper::importLicenseFile(const QString &licenseFile)
{
    QList<QVariant> argumentList;
    argumentList << licenseFile;

    QDBusMessage reply = licenseAgent->callWithArgumentList(QDBus::AutoDetect,
                         "ImportLicenseFile", argumentList);
    if (reply.type() == QDBusMessage::ErrorMessage) {
        qDebug() << "D-Bus Error:" << reply.errorMessage();
        return 0xFFFFFFFF;
    }
    return reply.arguments().first().toUInt();
}

quint32 LicenseHelper::importSerialKey(const QString &key)
{
    QList<QVariant> argumentList;
    argumentList << key;

    QDBusMessage reply = licenseAgent->callWithArgumentList(QDBus::AutoDetect,
                         "ImportSerialKey", argumentList);
    if (reply.type() == QDBusMessage::ErrorMessage) {
        qDebug() << "D-Bus Error:" << reply.errorMessage();
        return 0xFFFFFFFF;
    }
    return reply.arguments().first().toUInt();
}

void LicenseHelper::deactive()
{
    QList<QVariant> argumentList;
    QDBusMessage reply = licenseAgent->callWithArgumentList(QDBus::AutoDetect,
                         "Deactive", argumentList);
    if (reply.type() == QDBusMessage::ErrorMessage) {
        qDebug() << "D-Bus Error:" << reply.errorMessage();
    }
}

void LicenseHelper::quit()
{
    QList<QVariant> argumentList;
    QDBusMessage reply = licenseAgent->callWithArgumentList(QDBus::AutoDetect,
                         "Quit", argumentList);
    if (reply.type() == QDBusMessage::ErrorMessage) {
        qDebug() << "D-Bus Error:" << reply.errorMessage();
    }
}


