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

#define ICON_SIZE QSize(30, 30)

TipsChange::TipsChange(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TipsChange)
{
    this->setAccessibleName("kylin-burner_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);
    }
    
    ui->buttonBox->setAccessibleName("kylin-burner_QDialogButtonBox_TipsChange_buttonBox");
    ui->labelContent->setAccessibleName("kylin-burner_QLabel_TipsChange_labelContent");
    ui->labelLogo->setAccessibleName("kylin-burner_QLabel_TipsChange_labelLogo");
    ui->title->onlyShowInDialog();
    connect(ui->title, SIGNAL(close()), this, SLOT(slotsClose()));
    connect(this, SIGNAL(windowIconChanged(QIcon)), ui->title, SLOT(onWindowIconChange(QIcon)));
    connect(this, SIGNAL(windowTitleChanged(QString)), ui->title, SLOT(onWindowTitleChange(QString)));
    ui->labelContent->setAlignment(Qt::AlignLeft | Qt::AlignTop);

    ui->buttonBox->clear();
    m_oCancelBtn = ui->buttonBox->addButton(_("Cancel"), QDialogButtonBox::RejectRole);
    m_oEnsureBtn = ui->buttonBox->addButton(_("OK"), QDialogButtonBox::AcceptRole);
    ui->labelLogo->setPixmap(QIcon::fromTheme("dialog-question").pixmap(ICON_SIZE));

    slotChangeUIFontSize(LogicMain::instance()->GetSystemFontSize());
    connect(LogicMain::instance(), &LogicMain::systemFontSizeChanged, this, &TipsChange::slotChangeUIFontSize);
}

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->setFocus();
    ui->labelContent->setText(content);
    m_oEnsureBtn->setProperty("isImportant", true);
    if(m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    return !TipsChange::exec();
}
void TipsChange::slotsClose(){
    this->reject();
}

void TipsChange::slotChangeUIFontSize(int fontSize)
{
     int scaleRatio = qAbs(fontSize - 10.0);

     QFont font;
     font = ui->labelContent->font();
     font.setPixelSize(16 + scaleRatio);
     ui->labelContent->setFont(font);

}

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);
}
