#include "tipschange.h"
#include "ui_tipschange.h"
#include <QDebug>
#include <QVBoxLayout>
#include "src/po/translation.h"
#include <QSize>
#include <QFontMetrics>
#include <QPushButton>
#include <QLineEdit>
#include <ukuistylehelper.h>
using namespace kdk;

#define ICON_SIZE QSize(30, 30)

TipsChange::TipsChange(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TipsChange)
{
    ui->setupUi(this);

    //判断是否是wayland环境
    if(QString(qgetenv("XDG_SESSION_TYPE")) == "wayland") {
        m_bIsWayland = true;
    } else {
        m_bIsWayland = false;
        MotifWmHints hints;
        hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
        hints.functions = MWM_FUNC_ALL;
        hints.decorations = MWM_DECOR_BORDER;
        XAtomHelper::getInstance()->setWindowMotifHint(winId(), hints);
    }

    QFont f;
    f = ui->labelContent->font();
    f.setPixelSize(16);
    ui->title->onlyShowInDialog();
    connect(ui->title, SIGNAL(close()), this, SLOT(close()));
    connect(this, SIGNAL(windowIconChanged(QIcon)), ui->title, SLOT(onWindowIconChange(QIcon)));
    connect(this, SIGNAL(windowTitleChanged(QString)), ui->title, SLOT(onWindowTitleChange(QString)));
    ui->labelContent->setFont(f);
    ui->labelContent->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    ui->m_oCancelBtn->setText(_("Cancel"));
    ui->m_oEnsureBtn->setText(_("OK"));
    connect(ui->m_oCancelBtn, &QPushButton::clicked, this, &TipsChange::accept);
    connect(ui->m_oEnsureBtn, &QPushButton::clicked, this, &TipsChange::reject);
    ui->labelLogo->setPixmap(QIcon::fromTheme("dialog-question").pixmap(ICON_SIZE));
}

TipsChange::~TipsChange()
{
    delete ui;
}

bool TipsChange::question(QString title, QString content, bool next)
{
    m_oTipTitle = title;
    if (CFG->get("EXEC", title, false).toBool()) return true;
    setFixedSize(424,164);
    ui->labelContent->setText(content);
    qDebug()<<"109content=="<<content;
    if(title==_("Delete")){
        connect(ui->m_oCancelBtn, &QPushButton::clicked, this, &TipsChange::reject);
        connect(ui->m_oEnsureBtn, &QPushButton::clicked, this, &TipsChange::accept);
    }
    if(m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    return !TipsChange::exec();
}

void TipsChange::close(){
    this->accept();
    if(m_oTipTitle==_("Delete")){
        this->reject();
    }
}

void TipsChange::keyPressEvent(QKeyEvent *event)
{

    switch (event->key())
    {
    case Qt::Key_Escape:
        this->accept();
        if(m_oTipTitle==_("Delete")){
            this->reject();
        }
        break;
    case  Qt::Key_Enter:
        this->reject();
        if(m_oTipTitle==_("Delete")){
            this->accept();
        }
        break;
    case Qt::Key_Return:
        this->reject();
        if(m_oTipTitle==_("Delete")){
            this->accept();
        }
        break;
    }
}

void TipsChange::slotsTipsAgain(bool b)
{
    CFG->set("EXEC", m_oTipTitle, b);
}
