// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "dfontinfodialog.h"
#include "dfontpreviewitemdef.h"
#include "utils.h"
#include "fonticontext.h"
#include "dfontinfoscrollarea.h"

#include <DApplication>
#if QT_VERSION_MAJOR <= 5
#include <DApplicationHelper>
#endif
#include <DLog>
#include <DFontSizeManager>

#include <QScrollArea>
#include <QKeyEvent>
#include <QBitmap>
#include <QFileInfo>

#define NAME_TITLE_WIDTH 300

/*************************************************************************
 <Function>      DFontInfoDialog
 <Description>   信息页面构造函数
 <Author>
 <Input>
    <param1>     fontInfo        Description:当前字体信息结构体
    <param2>     parent          Description:父控件
 <Return>        null            Description:null
 <Note>          null
*************************************************************************/
DFontInfoDialog::DFontInfoDialog(DFontPreviewItemData *fontInfo, QWidget *parent)
    : DFontBaseDialog(parent)
    , m_fontInfo(fontInfo)
    , m_fontinfoArea(nullptr)
    , m_scrollArea(nullptr)
{
    qDebug() << "Font info dialog opened for:" << fontInfo->fontInfo.familyName
             << "Style:" << fontInfo->fontInfo.styleName;
    connect(m_signalManager, &SignalManager::sizeChange, this, &DFontInfoDialog::autoHeight);
    initUI();
    initConnections();
    this->move(parent->geometry().center() - this->rect().center());
    qDebug() << "Exiting function: DFontInfoDialog::DFontInfoDialog";
}

/*************************************************************************
 <Function>      AutoFeed
 <Description>   用于信息页面nametitle的宽度和字符串长度判断，
 <Author>        UT000539
 <Input>
    <param1>     text            Description:待处理文本
 <Return>        QString         Description:返回处理过的字符串
 <Note>          null
*************************************************************************/
QString DFontInfoDialog::AutoFeed(QString text)
{
    qDebug() << "Entering function: DFontInfoDialog::AutoFeed";
    QFont m_CurrentFont = this->font();
    QFontMetrics fm(m_CurrentFont);
#if QT_VERSION_MAJOR > 5
    int n_TextSize = fm.boundingRect(text).width();
#else
    int n_TextSize = fm.width(text);
#endif
    int count  = 0;
    if (n_TextSize > NAME_TITLE_WIDTH) {
        qDebug() << "Text size is greater than NAME_TITLE_WIDTH";
        int n_position = 0;
        long n_curSumWidth = 0;
        for (int i = 0; i < text.size(); i++) {
#if QT_VERSION_MAJOR > 5
            n_curSumWidth += fm.boundingRect(text.at(i)).width();
#else
            n_curSumWidth += fm.width(text.at(i));
#endif
            if (n_curSumWidth >= NAME_TITLE_WIDTH * (count + 1)) {
                qDebug() << "Insert newline";
                n_position = i;
                text.insert(n_position, "\n");
                count++;
            }
            if (count == 2) {
                qDebug() << "Insert third line";
                QString thirdLine = text.right(text.size() - n_position);
                text.remove(thirdLine);
                thirdLine = adaptiveLengthForNameTitle(fm, thirdLine, NAME_TITLE_WIDTH);
                text.append(thirdLine);
                break;
            }
        }
    }
    qDebug() << "Exiting function: DFontInfoDialog::AutoFeed";
    return text;
}

/*************************************************************************
 <Function>      adaptiveLengthForNameTitle
 <Description>   用于nametitle的第三行判断，由AutoFeed调用
 <Author>        UT000539
 <Input>
    <param1>     fm            Description:传入当前字体属性信息
    <param2>     thirdLineText Description:第三行待处理的文本
    <param3>     lineWidth     Description:用于判断的行宽参数
 <Return>        QString       Description:返回处理过的第三行字符串
 <Note>          null
*************************************************************************/
QString DFontInfoDialog::adaptiveLengthForNameTitle(QFontMetrics fm, QString thirdLineText, int lineWidth)
{
    qDebug() << "Entering function: DFontInfoDialog::adaptiveLengthForNameTitle";
#if QT_VERSION_MAJOR > 5
    if (fm.boundingRect(thirdLineText).width() > lineWidth) {
#else
    if (fm.width(thirdLineText) > lineWidth) {
#endif
        QString s = thirdLineText.right(6);
        int width = 0;
        for (int i = 0; i < thirdLineText.size(); i++) {
#if QT_VERSION_MAJOR > 5
            width += fm.boundingRect(thirdLineText.at(i)).width();
            if (width > (lineWidth - (fm.boundingRect(s).width() + fm.boundingRect("...").width()))) {
#else
            width += fm.width(thirdLineText.at(i));
            if (width > (lineWidth - (fm.width(s) + fm.width("...")))) {
#endif
                thirdLineText.remove(i, thirdLineText.size());
                thirdLineText.append("...").append(s);
                break;
            }
        }
    }
    qDebug() << "Exiting function: DFontInfoDialog::adaptiveLengthForNameTitle";
    return thirdLineText;
}

/*************************************************************************
 <Function>      initUI
 <Description>   初始化信息页面
 <Author>
 <Input>         null
 <Return>        null            Description:null
 <Note>          null
*************************************************************************/
void DFontInfoDialog::initUI()
{
    qDebug() << "Entering function: DFontInfoDialog::initUI";
    setFixedSize(QSize(DEFAULT_WINDOW_W, DEFAULT_WINDOW_H));
    setLogoVisable(false);

    //setWindowOpacity(0.5); //Debug

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
    mainLayout->setContentsMargins(10, 0, 0, 10);
    mainLayout->setSpacing(0);

    m_mainFrame = new QWidget(this);
    //m_mainFrame->setFrameShape(DFrame::Shape::NoFrame);
    m_mainFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // Font logo
    m_fontLogo = new FontIconText("://font-info-logo.svg", this);
    QFileInfo fileInfo(m_fontInfo->fontInfo.filePath);
    QString suffix = fileInfo.suffix().toLower();
    if (!suffix.compare("ttf")) {
        m_fontLogo->setContent(true);
    } else {
        m_fontLogo->setContent(false);
    }
    DFontSizeManager::instance()->bind(m_fontLogo, DFontSizeManager::T4);
    m_fontLogo->setFontName(m_fontInfo->fontInfo.familyName, m_fontInfo->fontInfo.styleName);

    m_fontFileName = new DLabel(this);
    m_fontFileName->setFixedWidth(280);
    m_fontFileName->adjustSize();
    m_fontFileName->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    m_fontFileName->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    m_fontFileName->setMinimumHeight(m_fontFileName->fontMetrics().height());
//    m_fontFileName->setWordWrap(true);

    DFontSizeManager::instance()->bind(m_fontFileName, DFontSizeManager::T8);
    m_FileName = QFileInfo(m_fontInfo->fontInfo.filePath).fileName();
    QString text = AutoFeed(m_FileName);
    m_fontFileName->setText(text);

    // Set color
#if QT_VERSION_MAJOR > 5
    DPalette pa = m_fontFileName->palette();
#else
    DPalette pa = DApplicationHelper::instance()->palette(m_fontFileName);
#endif
    pa.setBrush(DPalette::WindowText, pa.color(DPalette::ToolTipText));
    m_fontFileName->setPalette(pa);

    m_scrollArea = new QScrollArea(this);
//    scrollArea->setLineWidth(120);
    m_scrollArea->setFixedSize(QSize(290, 375));
//    scrollArea->setContentsMargins(0,0,30,0);

    QPixmap bmp(QSize(280, 375));
    bmp.fill();
    QPainter p(&bmp);
    bmp.setDevicePixelRatio(0);
    p.setBrush(Qt::black);
    p.drawRoundedRect(bmp.rect(), 12, 12);
    p.setRenderHint(QPainter::Antialiasing);

#if QT_VERSION_MAJOR > 5
    m_scrollArea->viewport()->setMask(QBitmap::fromPixmap(bmp));
#else
    m_scrollArea->viewport()->setMask(bmp.mask());
#endif
    m_scrollArea->setFrameShape(QFrame::Shape::NoFrame);

    m_fontinfoArea = new dfontinfoscrollarea(m_fontInfo, this);
    m_fontinfoArea->setFixedWidth(290);
    m_scrollArea->setWidget(m_fontinfoArea);
    m_scrollArea->setWidgetResizable(true);
    m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);

    // Add childs to main layout
    mainLayout->addWidget(m_fontLogo, 0, Qt::AlignHCenter);
//    mainLayout->addSpacing(6);
    mainLayout->addWidget(m_fontFileName);
//    mainLayout->addSpacing(42);
    mainLayout->addStretch(8);
    mainLayout->addWidget(m_scrollArea);
    m_mainFrame->setLayout(mainLayout);
    addContent(m_mainFrame);

#if QT_VERSION_MAJOR > 5
    if (DGuiApplicationHelper::DarkType == DGuiApplicationHelper::instance()->themeType()) {
        DPalette paFrame = m_scrollArea->viewport()->palette();
#else
    if (DApplicationHelper::DarkType == DGuiApplicationHelper::instance()->themeType()) {
        DPalette paFrame = DApplicationHelper::instance()->palette(m_scrollArea->viewport());
#endif
        QColor colorFrame = paFrame.textLively().color();
        colorFrame.setAlphaF(0.05);
        paFrame.setColor(DPalette::Base, colorFrame);
#if QT_VERSION_MAJOR > 5
        m_scrollArea->viewport()->setPalette(paFrame);

    } else if (DGuiApplicationHelper::LightType == DGuiApplicationHelper::instance()->themeType()) {
        DPalette paFrame = m_scrollArea->viewport()->palette();
#else
        DApplicationHelper::instance()->setPalette(m_scrollArea->viewport(), paFrame);

    } else if (DApplicationHelper::LightType == DGuiApplicationHelper::instance()->themeType()) {
        DPalette paFrame = DApplicationHelper::instance()->palette(m_scrollArea->viewport());
#endif
        //使用白色背景，而不是文字颜色作为背景
        QColor colorFrame(255, 255, 255);
        colorFrame.setAlphaF(0.70);
        paFrame.setColor(DPalette::Base, colorFrame);
#if QT_VERSION_MAJOR > 5
        m_scrollArea->viewport()->setPalette(paFrame);
#else
        DApplicationHelper::instance()->setPalette(m_scrollArea->viewport(), paFrame);
#endif
    }
    m_fontinfoArea->autoHeight();
    m_scrollArea->setFocus(Qt::MouseFocusReason);
    qDebug() << "Exiting function: DFontInfoDialog::initUI";
}

/*************************************************************************
 <Function>      initConnections
 <Description>   初始化信号与槽连接处理函数-主题变化信号和槽
 <Author>        UT000539
 <Input>         Null
 <Return>        null            Description:null
 <Note>          null
*************************************************************************/
void DFontInfoDialog::initConnections()
{
    qDebug() << "Entering function: DFontInfoDialog::initConnections";
#if QT_VERSION_MAJOR > 5
    connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [ = ] {
        DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();

        if (DGuiApplicationHelper::DarkType == themeType)
        {
            DPalette paFrame = m_scrollArea->viewport()->palette();
            QColor colorFrame = paFrame.textLively().color();
            colorFrame.setAlphaF(0.05);
            paFrame.setColor(DPalette::Base, colorFrame);
            m_scrollArea->viewport()->setPalette(paFrame);
            //通过m_fontFileName->setPalette(pa)未实现更新字体颜色，在此根据主题变化同步更新字体名称标签的颜色属性
            m_fontFileName->setPalette(paFrame);

        } else if (DGuiApplicationHelper::LightType == themeType)
        {
            DPalette paFrame = m_scrollArea->viewport()->palette();
            //使用构造函数中有关设置，会导致颜色重叠的异常的情况，故在此使用固定颜色，与textLively中#FFFFFF同色号
            QColor colorFrame(255, 255, 255);
            colorFrame.setAlphaF(0.70);
            paFrame.setColor(DPalette::Base, colorFrame);
            m_scrollArea->viewport()->setPalette(paFrame);
            //同时更新字体名称标签的颜色属性,跟随主题，原因同上
            m_fontFileName->setPalette(paFrame);
        }
    });
#else
    connect(DApplicationHelper::instance(), &DApplicationHelper::themeTypeChanged, this, [ = ] {
        DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();

        if (DApplicationHelper::DarkType == themeType)
        {
            DPalette paFrame = DApplicationHelper::instance()->palette(m_scrollArea->viewport());
            QColor colorFrame = paFrame.textLively().color();
            colorFrame.setAlphaF(0.05);
            paFrame.setColor(DPalette::Base, colorFrame);
            DApplicationHelper::instance()->setPalette(m_scrollArea->viewport(), paFrame);
            //通过m_fontFileName->setPalette(pa)未实现更新字体颜色，在此根据主题变化同步更新字体名称标签的颜色属性
            DApplicationHelper::instance()->setPalette(m_fontFileName, paFrame);

        } else if (DApplicationHelper::LightType == themeType)
        {
            DPalette paFrame = DApplicationHelper::instance()->palette(m_scrollArea->viewport());
            //使用构造函数中有关设置，会导致颜色重叠的异常的情况，故在此使用固定颜色，与textLively中#FFFFFF同色号
            QColor colorFrame(255, 255, 255);
            colorFrame.setAlphaF(0.70);
            paFrame.setColor(DPalette::Base, colorFrame);
            DApplicationHelper::instance()->setPalette(m_scrollArea->viewport(), paFrame);
            //同时更新字体名称标签的颜色属性,跟随主题，原因同上
            DApplicationHelper::instance()->setPalette(m_fontFileName, paFrame);
        }
    });
#endif
    qDebug() << "Exiting function: DFontInfoDialog::initConnections";
}

/*************************************************************************
 <Function>      keyPressEvent
 <Description>   重新实现键盘press事件-信息页面打开快捷键关闭本窗口
 <Author>
 <Input>
    <param1>     ev              Description:事件对象
 <Return>        null            Description:null
 <Note>          null
*************************************************************************/
void DFontInfoDialog::keyPressEvent(QKeyEvent *ev)
{
    // qDebug() << "Entering function: DFontInfoDialog::keyPressEvent (high frequency key event - commented)";
    QDialog::keyPressEvent(ev);
    if (QApplication::keyboardModifiers()
            == Qt::ControlModifier && ev->key() == Qt::Key_I) {
        qDebug() << "Font info dialog closed by shortcut Ctrl+I";
        this->close();
        deleteLater();
    }
    // qDebug() << "Exiting function: DFontInfoDialog::keyPressEvent (high frequency key event - commented)";
}

/*************************************************************************
 <Function>      autoHeight
 <Description>   自适应变化信息详情页面高度
 <Author>        UT000442
 <Input>
    <param1>     height          Description:用于判断信息详情页面高度的参数
 <Return>        null            Description:null
 <Note>          null
*************************************************************************/
void DFontInfoDialog::autoHeight(int height)
{
    qDebug() << "Adjusting dialog height to:" << height;
    //repaint m_fontFileName/*UT000539*/
    m_fontFileName->setText(AutoFeed(m_FileName));

    if (height * 1.1 + 280 < DEFAULT_WINDOW_H) {
        qDebug() << "Using dynamic height calculation:" << static_cast<int>(height * 1.1 + 280);
        this->setFixedHeight(static_cast<int>(height * 1.1 + 280));
        QPixmap bmp(QSize(280, (static_cast<int>(height * 1.1 + 10))));
        bmp.fill();
        QPainter p(&bmp);
        bmp.setDevicePixelRatio(0);
        p.setBrush(Qt::black);
        p.drawRoundedRect(bmp.rect(), 12, 12);
        p.setRenderHint(QPainter::Antialiasing);
#if QT_VERSION_MAJOR > 5
        m_scrollArea->viewport()->setMask(QBitmap::fromPixmap(bmp));
#else
        m_scrollArea->viewport()->setMask(bmp.mask());
#endif
        m_scrollArea->viewport()->setFixedHeight(static_cast<int>(height * 1.1 + 10));
        m_scrollArea->setFixedHeight(static_cast<int>(height * 1.1 + 10));
    } else {
        qDebug() << "Using default height:" << DEFAULT_WINDOW_H;
        this->setFixedHeight(DEFAULT_WINDOW_H);
        QPixmap bmp(QSize(280, 375));
        bmp.fill();
        QPainter p(&bmp);
        bmp.setDevicePixelRatio(0);
        p.setBrush(Qt::black);
        p.drawRoundedRect(bmp.rect(), 12, 12);
        p.setRenderHint(QPainter::Antialiasing);
#if QT_VERSION_MAJOR > 5
        m_scrollArea->viewport()->setMask(QBitmap::fromPixmap(bmp));
#else
        m_scrollArea->viewport()->setMask(bmp.mask());
#endif
        m_scrollArea->viewport()->setFixedHeight(375);
        m_scrollArea->setFixedHeight(375);
    }
    qDebug() << "Exiting function: DFontInfoDialog::autoHeight";
}
