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

#include <QDebug>
#include <QCloseEvent>
#include <QLabel>
#include <QDateTime>
#include <QPainter>
#include <QIcon>
#include <QImage>

#include <DHiDPIHelper>
#include <DTitlebar>

using namespace Dtk::Widget;

LicenseWindow::LicenseWindow(const QString &local, const QString &product, QWidget *parent) : DMainWindow(parent)
{
    QString shortLocal = "en_US";
    if ("zh_CN" == local.split(".").first()) {
        shortLocal = "zh_CN";
    }
    auto flags = windowFlags() & ~Qt::WindowMinMaxButtonsHint;
    flags = flags & ~Qt::WindowSystemMenuHint;
    flags = flags & ~Qt::WindowCloseButtonHint;
    titlebar()->setTitle("");
    titlebar()->setBackgroundTransparent(true);
    overrideWindowFlags(flags);

    auto imagePath = QString(":/view/resources/license-agreement-bg-%1.svg").arg(shortLocal);
    backgroundPixmap = DHiDPIHelper::loadNxPixmap(imagePath);
    setFixedSize(backgroundPixmap.size() / backgroundPixmap.devicePixelRatioF());
    setFocusPolicy(Qt::StrongFocus);

    int osHeight = 276 - 20 - 10 - 2;
    int dateBegintX = 207 - 40;
    if (shortLocal == "en_US") {
        osHeight = 257 - 20 - 10 - 2;
        dateBegintX = 230 - 40;
    }

    QString styleSheet = "font-size: 13px; color: #004EE5;";
    QLabel *os = new QLabel(product, this);
    os->setStyleSheet(styleSheet);
    os->move(dateBegintX, osHeight);
    os->setFixedHeight(os->height());
    os->adjustSize();

    hide();
}

void LicenseWindow::closeEvent(QCloseEvent *e)
{
    e->accept();
    DMainWindow::closeEvent(e);
}

void LicenseWindow::focusOutEvent(QFocusEvent *e)
{
    DMainWindow::focusOutEvent(e);
    hide();
}

void LicenseWindow::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    p.drawPixmap(rect(), backgroundPixmap);
    DMainWindow::paintEvent(e);
}
