/*
 * Copyright (C) 2020, 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 3 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, see <https://www.gnu.org/licenses/>.
 */
#include "custommessagebox.h"

#include <QResizeEvent>
#include <QHBoxLayout>
#include <QVBoxLayout>

#include "windowhelper.h"

CustomMessageBox::CustomMessageBox(QWidget *parent)
    : QDialog(parent), m_clickedButton(0)
{
    initLayout();
    initConnect();
    checkTheme();
}

void CustomMessageBox::initLayout()
{
    this->setWindowTitle(QString(" "));
    setFixedSize(376, 156);
    setWindowFlags(Qt::Dialog);

#ifdef UKUI_WINDOWHELPER_SUPPORT
    UkuiWindowHelper *ukuiWindowHelper = new UkuiWindowHelper(this);
    ukuiWindowHelper->removeTitleBar();
#else
    kdk::UkuiStyleHelper::self()->removeHeader(this);
#endif

    // 焦点不在窗口上时鼠标悬浮可显示tooltips
    this->setAttribute(Qt::WA_AlwaysShowToolTips);

    closeButton = new QPushButton;
    closeButton->setAccessibleName("kylin-aiassistant_QPushButton_closeButton");
    closeButton->setFlat(true);
    closeButton->setFixedSize(32, 32);
    closeButton->setProperty("isWindowButton",  0x2);
    closeButton->setProperty("useIconHighlightEffect", 0x8);
    closeButton->setIcon(QIcon::fromTheme("window-close-symbolic"));
    closeButton->setToolTip(tr("Close"));

    QWidget *titleWidget = new QWidget();
    titleWidget->setAccessibleName("kylin-aiassistant_QWidget_titleWidget");
    titleWidget->setFixedSize(376, 40);
    QHBoxLayout *titleHLayout = new QHBoxLayout(titleWidget);
    titleHLayout->setContentsMargins(8, 0, 4, 0);
    titleHLayout->addStretch();
    titleHLayout->addWidget(closeButton);

    // 提示
    iconLabel = new QLabel(this);
    iconLabel->setAccessibleName("kylin-aiassistant_QLabel_iconLabel");
    iconLabel->setFixedSize(24, 24);
    QPixmap pixmap(":/icon/icon/tips.svg");
    pixmap.scaled(iconLabel->size(), Qt::KeepAspectRatio,Qt::SmoothTransformation);
    iconLabel->setScaledContents(true);
    iconLabel->setPixmap(pixmap);//设置图标

    messageLabel = new QLabel(this);
    messageLabel->setAccessibleName("kylin-aiassistant_QLabel_messageLabel");
    messageLabel->setFixedWidth(294);
    messageLabel->setWordWrap(true);

    QHBoxLayout *tipsLayout = new QHBoxLayout();
    tipsLayout->setContentsMargins(24, 8, 24, 0);
    tipsLayout->setSpacing(8);
    tipsLayout->addWidget(iconLabel);
    tipsLayout->addWidget(messageLabel);
    tipsLayout->addStretch();

    // 按钮
    cancelButton = new KPushButton(this);
    cancelButton->setText(tr("Cancel"));
    cancelButton->setAccessibleName("kylin-aiassistant_QQPushButton_cancelButton");
    cancelButton->setFixedSize(94, 36);

    okButton = new KPushButton(this);
    okButton->setText(tr("Delete"));
    okButton->setAccessibleName("kylin-aiassistant_QPushButton_okButton");
    okButton->setFixedSize(94, 36);
    okButton->setBackgroundColorHighlight(true);
    okButton->setDefault(true);

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setContentsMargins(24, 0, 24, 24);
    buttonLayout->setSpacing(16);
    buttonLayout->addStretch();
    buttonLayout->addWidget(cancelButton);
    buttonLayout->addWidget(okButton);

    // 主布局
    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);
    mainLayout->addWidget(titleWidget);
    mainLayout->addLayout(tipsLayout);
    mainLayout->addStretch();
    mainLayout->addLayout(buttonLayout);
    this->setLayout(mainLayout);
}

void CustomMessageBox::initConnect()
{
    connect(closeButton, &QPushButton::clicked, this, &CustomMessageBox::onCancelClicked);
    connect(cancelButton, &QPushButton::clicked, this, &CustomMessageBox::onCancelClicked);
    connect(okButton, &QPushButton::clicked, this, &CustomMessageBox::onOkClicked);
}

void CustomMessageBox::setMessage(const QString &message) {
    messageLabel->setText(message);
    setFixedSize(376, 126+getLableLineCount(messageLabel) * messageLabel->height());
}

void CustomMessageBox::setButtons(int buttons) {
    okButton->setVisible(buttons & OkButton);
    cancelButton->setVisible(buttons & CancelButton);
}

int CustomMessageBox::clickedButton() const {
    return m_clickedButton;
}

void CustomMessageBox::onOkClicked() {
    m_clickedButton = OkButton;
    accept();
}

void CustomMessageBox::onCancelClicked() {
    m_clickedButton = CancelButton;
    reject();
}

void CustomMessageBox::resizeEvent(QResizeEvent *event) {
    QDialog::resizeEvent(event);
    // Center the messageLabel within the dialog
    //messageLabel->setGeometry(0, 0, width(), messageLabel->height());
}

void CustomMessageBox::changeTheme()
{
    QPalette p = this->palette();
    QColor color = p.color(QPalette::Base);
    p.setColor(QPalette::Window, color);
    this->setPalette(p);
}

void CustomMessageBox::checkTheme()
{
    changeTheme();

    if(QGSettings::isSchemaInstalled("org.ukui.style")) {
        themeData = new QGSettings("org.ukui.style");
        connect(themeData, &QGSettings::changed, this, [=](const QString &key) {
            if(key == "styleName") {
                changeTheme();
            }
        });
    }
}

int CustomMessageBox::getLableLineCount(QLabel* label)
{
    QRect textRect = label->contentsRect();
    QFontMetrics fm(label->font());

    QString text = label->text();
    QRect neededRect = fm.boundingRect(textRect,
                                      Qt::TextWordWrap | label->alignment(),
                                      text);

    return neededRect.height() / fm.height();
}
