/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2024 KylinSoft  Co., Ltd.
 *
 * 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 2 of the License, or
 * (at your option) 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include <QLocale>
#include <QDebug>
#include <gsettingmonitor.h>
#include <usermanual.h>
#include <QPainterPath>
#include "popupfontinfo.h"
#include "../interface/ukcccommon.h"

using namespace ukcc;

PopupFontInfo::PopupFontInfo(QString fontPath , QWidget *parent) : KDialog(parent)
{
    qRegisterMetaType<FontInformation>("FontInformation");
    qDBusRegisterMetaType<FontInformation>();
    m_fontInterface = new QDBusInterface("org.ukui.ukcc.session",
                                         "/Font",
                                         "org.ukui.ukcc.session.Font",
                                         QDBusConnection::sessionBus(), this);
    if (!m_fontInterface->isValid()) {
        qCritical() << "org.ukui.ukcc.session.Font DBus error:" << m_fontInterface->lastError();
    }
    QDBusReply<FontInformation> ret =  m_fontInterface->call("getFontDetails", fontPath);
    if (ret.isValid()) {
        m_font = ret.value();
    }
    setWidegtUi();
    setWidLayout();
    changeFontSize();
}

PopupFontInfo::~PopupFontInfo()
{
}

void PopupFontInfo::setWidegtUi()
{
    int valueWidth = 300;
    this->setFixedSize(420, 451);
    setWindowFlag(Qt::Tool);
    setWindowModality(Qt::WindowModal);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_DeleteOnClose,true);
    setWindowIcon("ukui-control-center");
    setWindowTitle(m_font.name);

    m_tipText = new QLabel(this);
    m_fontName = new QLabel(this);
    m_nameValue = new QLabel(this);
    m_fontSeries = new QLabel(this);
    m_seriesValue = new QLabel(this);
    m_fontStyle = new QLabel(this);
    m_styleValue = new QLabel(this);
    m_fontType = new QLabel(this);
    m_typeValue = new QLabel(this);
    m_fontVersion = new QLabel(this);
    m_versionValue = new QLabel(this);
    m_fontPath = new QLabel(this);
    m_pathValue = new QLabel(this);
    m_fontCopyright = new QLabel(this);
    m_copyrightValue = new QLabel(this);
    m_fontTrademark = new QLabel(this);
    m_trademarkValue = new QLabel(this);

    m_tipText->setFixedHeight(35);
    m_tipText->setText(m_font.family);
    m_tipText->adjustSize();
    m_tipText->setWordWrap(true);
    QString systemLang = QLocale::system().name();
    if(systemLang == "ug_CN" || systemLang == "ky_KG" || systemLang == "kk_KZ"){
        m_tipText->setAlignment(Qt::AlignTop|Qt::AlignRight);
    } else{
        m_tipText->setAlignment(Qt::AlignTop|Qt::AlignLeft);

    }
    m_fontName->setFixedHeight(30);
    m_fontName->setText(tr("FontName:"));
    m_fontName->setAlignment(Qt::AlignTop|Qt::AlignLeft);

    m_nameValue->setFixedHeight(30);
    m_nameValue->setFixedWidth(valueWidth);
    m_nameValue->setTextInteractionFlags(Qt::TextSelectableByMouse);
    m_nameValue->setText(m_font.name);
    m_nameValue->adjustSize();
    m_nameValue->setWordWrap(true);
    m_nameValue->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_nameValue->setCursor(Qt::IBeamCursor);

    if (m_font.name == "") {
        m_fontName->hide();
        m_nameValue->hide();
    }

    m_fontSeries->setFixedHeight(30);
    m_fontSeries->setText(tr("FontSeries:"));
    m_fontSeries->setAlignment(Qt::AlignTop|Qt::AlignLeft);

    m_seriesValue->setFixedHeight(30);
    m_seriesValue->setFixedWidth(valueWidth);
    m_seriesValue->setTextInteractionFlags(Qt::TextSelectableByMouse);
    m_seriesValue->setText(m_font.family);
    m_seriesValue->adjustSize();
    m_seriesValue->setWordWrap(true);
    m_seriesValue->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_seriesValue->setCursor(Qt::IBeamCursor);

    if (m_font.family == "") {
        m_fontSeries->hide();
        m_seriesValue->hide();
    }

    m_fontStyle->setFixedHeight(30);
    m_fontStyle->setText(tr("FontStyle:"));
    m_fontStyle->setAlignment(Qt::AlignTop|Qt::AlignLeft);

    m_styleValue->setFixedHeight(30);
    m_styleValue->setFixedWidth(valueWidth);
    m_styleValue->setTextInteractionFlags(Qt::TextSelectableByMouse);
    m_styleValue->setText(m_font.style);
    m_styleValue->adjustSize();
    m_styleValue->setWordWrap(true);
    m_styleValue->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_styleValue->setCursor(Qt::IBeamCursor);

    if (m_font.style == "") {
        m_fontStyle->hide();
        m_styleValue->hide();
    }

    m_fontType->setFixedHeight(30);
    m_fontType->setText(tr("FontType:"));
    m_fontType->setAlignment(Qt::AlignTop|Qt::AlignLeft);

    m_typeValue->setFixedHeight(30);
    m_typeValue->setFixedWidth(valueWidth);
    m_typeValue->setTextInteractionFlags(Qt::TextSelectableByMouse);
    m_typeValue->setText(m_font.type);
    m_typeValue->adjustSize();
    m_typeValue->setWordWrap(true);
    m_typeValue->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_typeValue->setCursor(Qt::IBeamCursor);

    if (m_font.type == "") {
        m_fontType->hide();
        m_typeValue->hide();
    }

    m_fontVersion->setFixedHeight(30);
    m_fontVersion->setText(tr("FontVersion:"));
    m_fontVersion->setAlignment(Qt::AlignTop|Qt::AlignLeft);

    m_versionValue->setText(m_font.version);
    m_versionValue->setFixedWidth(valueWidth);
    m_versionValue->setTextInteractionFlags(Qt::TextSelectableByMouse);
    m_versionValue->adjustSize();
    m_versionValue->setWordWrap(true);
    m_versionValue->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_versionValue->setCursor(Qt::IBeamCursor);

    if (m_font.version == "") {
        m_fontVersion->hide();
        m_versionValue->hide();
    }

    m_fontPath->setFixedHeight(30);
    m_fontPath->setText(tr("FontPath:"));
    m_fontPath->setAlignment(Qt::AlignTop|Qt::AlignLeft);

    m_pathValue->setText(m_font.path);
    m_pathValue->setFixedWidth(valueWidth);
    m_pathValue->setTextInteractionFlags(Qt::TextSelectableByMouse);
    m_pathValue->adjustSize();
    m_pathValue->setWordWrap(true);
    m_pathValue->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_pathValue->setCursor(Qt::IBeamCursor);

    if (m_font.path == "") {
        m_pathValue->hide();
        m_pathValue->hide();
    }

    m_fontCopyright->setFixedHeight(30);
    m_fontCopyright->setText(tr("FontCopyright:"));
    m_fontCopyright->setAlignment(Qt::AlignTop|Qt::AlignLeft);

    m_copyrightValue->setText(m_font.copyright);
    m_copyrightValue->setFixedWidth(valueWidth);
    m_copyrightValue->setTextInteractionFlags(Qt::TextSelectableByMouse);
    m_copyrightValue->adjustSize();
    m_copyrightValue->setWordWrap(true);
    m_copyrightValue->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_copyrightValue->setCursor(Qt::IBeamCursor);

    if (m_font.copyright == "") {
        m_fontCopyright->hide();
        m_copyrightValue->hide();
    }

    m_fontTrademark->setFixedHeight(30);
    m_fontTrademark->setText(tr("FontTrademark:"));
    m_fontTrademark->setAlignment(Qt::AlignTop|Qt::AlignLeft);

    m_trademarkValue->setText(m_font.manufacturer);
    m_trademarkValue->setFixedWidth(valueWidth);
    m_trademarkValue->setTextInteractionFlags(Qt::TextSelectableByMouse);
    m_trademarkValue->adjustSize();
    m_trademarkValue->setWordWrap(true);
    m_trademarkValue->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_trademarkValue->setCursor(Qt::IBeamCursor);

    if (m_font.manufacturer == "") {
        m_fontTrademark->hide();
        m_trademarkValue->hide();
    }

    m_continueBtn = new QPushButton();
    m_continueBtn->setFixedHeight(36);
    m_continueBtn->setText(tr("Confirm"));
    m_continueBtn->setProperty("isImportant", true);

    connect(m_continueBtn, &QPushButton::clicked, this, &PopupFontInfo::slotContinueClick);

    return;
}

void PopupFontInfo::setWidLayout()
{
    /* 字体信息网格布局 */
    m_gridLayout = new QGridLayout();

    m_gridLayout->setHorizontalSpacing(1);         // 设置水平间距
    m_gridLayout->setVerticalSpacing(4);            // 设置垂直间距
    m_gridLayout->setContentsMargins(5, 0, 0, 0);   // 设置外间距

    /* 字体名称 */
    m_gridLayout->addWidget(m_fontName, 0, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    m_gridLayout->addWidget(m_nameValue, 0, 1, 1, 1, Qt::AlignLeft);
    /* 样式 */
    m_gridLayout->addWidget(m_fontStyle, 1, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    m_gridLayout->addWidget(m_styleValue, 1, 1, 1, 1, Qt::AlignLeft);
    /* 系列 */
    m_gridLayout->addWidget(m_fontSeries, 2, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    m_gridLayout->addWidget(m_seriesValue, 2, 1, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    /* 种类 */
    m_gridLayout->addWidget(m_fontType, 3, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    m_gridLayout->addWidget(m_typeValue, 3, 1, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    /* 版本 */
    m_gridLayout->addWidget(m_fontVersion, 4, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    m_gridLayout->addWidget(m_versionValue, 4, 1, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    /* 位置 */
    m_gridLayout->addWidget(m_fontPath, 5, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    m_gridLayout->addWidget(m_pathValue, 5, 1, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    /* 版权 */
    m_gridLayout->addWidget(m_fontCopyright, 6, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    m_gridLayout->addWidget(m_copyrightValue, 6, 1, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    /* 商标 */
    m_gridLayout->addWidget(m_fontTrademark, 7, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    m_gridLayout->addWidget(m_trademarkValue, 7, 1, 1, 1, Qt::AlignLeft | Qt::AlignTop);

    m_vGridLayout = new QVBoxLayout();
    m_vGridLayout->addLayout(m_gridLayout);
    m_vGridLayout->addStretch();
    m_vGridLayout->setContentsMargins(0, 0, 0, 0);

    m_widget = new QWidget(this);
    m_widget->setFixedWidth(370);
    m_widget->setLayout(m_vGridLayout);

    m_scrollArea = new QScrollArea(this);
    m_scrollArea->setFixedHeight(340);
    m_scrollArea->setWidget(m_widget);
    m_scrollArea->setWidgetResizable(true);
    m_scrollArea->setFrameShape(QFrame::NoFrame);
    m_scrollArea->setStyleSheet("QScrollArea {background-color:transparent;}");
    m_scrollArea->viewport()->setStyleSheet("background-color:transparent;");
    m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_scrollArea->verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);

    /* 按钮横向布局 */
    m_hBtnLayout = new QHBoxLayout();
    m_hBtnLayout->addStretch();
    m_hBtnLayout->addSpacing(10);
    m_hBtnLayout->addWidget(m_continueBtn);
    m_hBtnLayout->setContentsMargins(0, 0, 0, 0);
    m_hBtnLayout->setContentsMargins(0, 0, 24, 0);

    /* 按钮和组件纵向布局 */
    m_vLayout = new QVBoxLayout();
    m_vLayout->addWidget(m_tipText);
    m_vLayout->addSpacing(10);
    m_vLayout->addWidget(m_scrollArea);
    m_vLayout->addSpacing(10);
    m_vLayout->addLayout(m_hBtnLayout);
    m_vLayout->addStretch();
    m_vLayout->setContentsMargins(0, 0, 0, 0);

    /* 组件居中 */
    m_hMainLayout = new QHBoxLayout();
    m_hMainLayout->addSpacing(24);
    m_hMainLayout->addLayout(m_vLayout);

    /* 整个窗体布局 */
    m_vMainLayout = new QVBoxLayout();
    m_vMainLayout->setSpacing(0);
    m_vMainLayout->addSpacing(10);
    m_vMainLayout->addLayout(m_hMainLayout);
    m_vMainLayout->addStretch();
    m_vMainLayout->setContentsMargins(0, 0, 0, 0);
    m_vMainLayout->setContentsMargins(0, 0, 0, 20);   // 设置外间距

    this->mainWidget()->setLayout(m_vMainLayout);
    UkccCommon::centerToScreen(this);
}

void PopupFontInfo::changeFontSize()
{
    double g_fontSize = kdk::GsettingMonitor::getSystemFontSize().toDouble();
    QFont font16;
    double g_font16pxToPt = (g_fontSize * (16.00 / 11)) - 3;
    font16.setPointSizeF(g_font16pxToPt);
    font16.setBold(true);
    font16.setWeight(QFont::DemiBold);
    QFont font14;
    double g_font14pxToPt = (g_fontSize * (14.00 / 11)) - 3;
    font14.setPointSizeF(g_font14pxToPt);
    m_tipText->setFont(font16);

    m_fontName->setFont(font14);
    m_nameValue->setFont(font14);
    m_fontSeries->setFont(font14);
    m_seriesValue->setFont(font14);
    m_fontStyle->setFont(font14);
    m_styleValue->setFont(font14);
    m_fontType->setFont(font14);
    m_typeValue->setFont(font14);
    m_fontVersion->setFont(font14);
    m_versionValue->setFont(font14);
    m_fontPath->setFont(font14);
    m_pathValue->setFont(font14);
    m_fontCopyright->setFont(font14);
    m_copyrightValue->setFont(font14);
    m_fontTrademark->setFont(font14);
    m_trademarkValue->setFont(font14);
    m_continueBtn->setFont(font14);
}

void PopupFontInfo::slotContinueClick()
{
    this->window()->close();
}
