#include "yozooffice.h"

#include <QDebug>
#include <QFileInfo>
#

void YozoOffice::updateStatus(Software &s)
{
    // C:\Program Files (x86)\Yozosoft\Yozo_Office
    qDebug() << s.InstallLocation;
#ifdef Q_OS_WIN
    QString datefilePath = s.InstallLocation;
    QFileInfo installLocationInfo(datefilePath);
    if (installLocationInfo.exists()) {
        s.InstallDate =  installLocationInfo.created();
        qWarning() << "get install date" << s.InstallDate;
    } else {
        qWarning() << "get install date failed";
    }
#endif

    auto installPath = s.InstallLocation;
#ifdef Q_OS_LINUX
    // fix intall path
    installPath = "/opt/Yozosoft/Yozo_Office/";
#endif

    auto keyPath = QString("%1/Config/Key.store").arg(installPath);

    /* key data:

    The install cdkey is : 1IDSRV-4IT4HR-JIBXBG-KK1U2W-UHZQN
    The preReg cdkey is : 3517272578174273894977015991140201533797597833409820400942
    The product ID is: 168B1-011-00100-04368-57

    */
    QFile keyFile(keyPath);
    if (!keyFile.open(QIODevice::ReadOnly)) {
        return;
    }


    QString cdkey;
    while (true) {
        auto lineData = keyFile.readLine();
        auto line = QString::fromLocal8Bit(lineData);
        auto kv  = line.split(":");
        auto key = kv.value(0).trimmed();
        auto value = kv.value(1).trimmed();
        qDebug() << line << key << value;
        if (key.contains("install cdkey is")) {
            cdkey = value;
            break;
        }

        if (line.isEmpty()) {
            break;
        }
    }
    qDebug() << cdkey;
    if (!cdkey.isEmpty()) {
        s.ProductKeys.push_back(cdkey);
    }
}
