#include "wpsoffice.h"

#include <QDebug>
#include <QStandardPaths>
#include <QFileInfo>
#include <QDir>

#include "tool.h"

extern QString quoteStrip(QString UninstallString);

void WPSOffice::updateStatus(Software &s)
{
    //C:\ProgramData\Kingsoft\office6\license2.dat
    auto appdatadirs = QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation);
    qDebug() << appdatadirs;
    auto licenseDir = appdatadirs.value(0);
    for (auto dir : appdatadirs) {
        if (dir.contains("ProgramData") || dir.contains("All Users/Application Data")) {
            licenseDir = dir;
        }
    }

    auto licensePath = licenseDir + "\\Kingsoft\\office6\\license2.dat";
    s.ProductKeys = Tool::getWPSProductkeys(licensePath);
    if (s.ProductKeys.size() > 0) {
        s.IsGenuine = true;
    }

#ifdef Q_OS_WIN
    QString datefilePath = s.UninstallString;

    auto execPath = quoteStrip(s.UninstallString);
    QFileInfo execPathInfo(execPath);
    if (execPathInfo.exists()) {
        datefilePath = execPath;
    }

    QFileInfo uninstallInfo(datefilePath);
    QFileInfo dirInfo(uninstallInfo.dir().canonicalPath());
    if (dirInfo.exists()) {
        s.InstallDate =  dirInfo.created();
        qWarning() << "get wps install date" << s.InstallDate;
    } else {
        qWarning() << "get wps install date failed";
    }
#endif
}

