#include "privacydialog.h"

#include <QDebug>
#include <QVBoxLayout>
#include <QHBoxLayout>

PrivacyDialog::PrivacyDialog(QWidget *parent) :
    QDialog(parent)
{
    this->setWindowFlags(Qt::Dialog);
    setWindowTitle(tr("Set"));
    initUi();
}
PrivacyDialog::~PrivacyDialog()
{

}

void PrivacyDialog::initUi()
{
    this->resize(560, 560);
    QVBoxLayout *mverticalLayout = new QVBoxLayout(this);
    mverticalLayout->setSpacing(8);
    mverticalLayout->setContentsMargins(24, 0, 24, 24);

    QLabel *mTitleLabel = new QLabel(getProtocolTitle(), this);
    mTitleLabel->setAlignment(Qt::AlignCenter);
    mverticalLayout->addWidget(mTitleLabel);
    QTextBrowser *mContentLabel = new QTextBrowser(this);
    // 协议内容
    QString mContent = getEnduserAgreement();
    mContentLabel->setText(mContent);
    mverticalLayout->addWidget(mContentLabel);
    QLabel *label_1 = new QLabel(QString(tr("Kylinsoft Co., Ltd.")) , this);
    label_1->setAlignment(Qt::AlignRight);
    mverticalLayout->addWidget(label_1);
}

QString PrivacyDialog::getEnduserAgreement()
{
    QString agreementPath = endUserAgreementSP1;
    if (UkccCommon::isOpenkylin()) {
        agreementPath = endUserAgreementOK;
    }
    const QString protocol = getLicenseText(agreementPath);
    return protocol;
}

QString PrivacyDialog::getLicenseText(const QString &filePath)
{
    QFile license(getLicensePath(filePath));
    if (!license.open(QIODevice::ReadOnly)) return QString();
    const QByteArray buf = license.readAll();
    license.close();
    return buf;
}

QString PrivacyDialog::getLicensePath(const QString &filePath)
{
    const QString &local {QLocale::system().name()};
    QString path = QString(filePath).arg(local);

    if (QFile::exists(path)) {
        return path;
    } else {
        return QString(filePath).arg("en_US");;
    }
}

QString PrivacyDialog::getProtocolTitle()
{
    if (UkccCommon::isOpenkylin()) {
        return QString(tr("End User License Agreement and Privacy Policy Statement of openKylin"));
    }
    return QString(tr("End User License Agreement and Privacy Policy Statement of Kylin"));
}
