#include "execdialog.h"
#include "mainwindow.h"
#include "ui_execdialog.h"
#include "src/frame/xatom-helper.h"
#include "src/frame/configuration.h"
#include "src/po/translation.h"
#include "src/model/logicmain.h"
#include <QDebug>
#include <QPushButton>
#include <QMessageBox>
#include <ukuistylehelper.h>

ExecDialog *ExecDialog::m_oInstance = nullptr;
QMutex      ExecDialog::m_oMutex;

ExecDialog::ExecDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ExecDialog)
{
    QFont f;
    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->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)));
    connect(this, SIGNAL(content(QString)), ui->inputPage, SIGNAL(content(QString)));
    connect(this, SIGNAL(result(QString)), ui->inputPage, SIGNAL(result(QString)));
    connect(ui->tip, SIGNAL(checked(bool)), this, SLOT(slotsTipsAgain(bool)));
    //setWindowIcon(QIcon::fromTheme("burner"));
    //setWindowTitle(_("Burner"));
    f = ui->buttonBox->font();
    f.setPixelSize(14);
    ui->buttonBox->setFont(f);
	ui->buttonBox->clear();					//删除ui文件内置的两个按钮
	ui->stackedWidget->setContentsMargins(0,0,0,0);
	ui->buttonBox->setContentsMargins(0,0,11,0);
	ui->verticalLayout->setContentsMargins(0,0,0,9);
	m_oCancelBtn = ui->buttonBox->addButton(_("Cancel"), QDialogButtonBox::AcceptRole);
    m_oEnsureBtn = ui->buttonBox->addButton(_("OK"), QDialogButtonBox::RejectRole);

	//ui->inputPage->setGeometry(15,24,380,30);

}

ExecDialog *ExecDialog::instance()
{
    if (nullptr == m_oInstance)
    {
        QMutexLocker locker(&m_oMutex);
        if (nullptr == m_oInstance)
        {
            m_oInstance = new ExecDialog(MainWindow::Instance());

        }
    }
    return m_oInstance;
}

int ExecDialog::information(QString title, QString content, bool next)
{
    //setWindowTitle(_("Burner") + " - " + title);
    //setWindowTitle(title);
	setFixedSize(424,164);
    ui->stackedWidget->setCurrentIndex(1);
    ui->tip->information(content, next);
    //ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok);
    //ui->buttonBox->button(QDialogButtonBox::Ok)->setText(_("OK"));

	ui->buttonBox->removeButton(m_oCancelBtn);
    if(m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    return !ExecDialog::exec();
}

void ExecDialog::error(QString title, QString content, bool next)
{
    m_oTipTitle = title;
    if (CFG->get("EXEC", title, false).toBool()) return;
    //setWindowTitle(_("Burner") + " - " + title);
    //setWindowTitle(title);
    setFixedSize(424,164);
    ui->stackedWidget->setCurrentIndex(1);
    //ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok);
    //ui->buttonBox->button(QDialogButtonBox::Ok)->setText(_("OK"));
    qDebug()<<"90content=="<<content;
    ui->buttonBox->removeButton(m_oCancelBtn);
    ui->tip->error(content, next);
    if(m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    ExecDialog::exec();
}

bool ExecDialog::question(QString title, QString content, bool next)
{
    m_oTipTitle = title;
    if (CFG->get("EXEC", title, false).toBool()) return true;
	setFixedSize(424,164);
    /*setWindowTitle(_("Burner") + " - " + title);
    ui->stackedWidget->setCurrentIndex(1);
    ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    ui->buttonBox->button(QDialogButtonBox::Ok)->setText(_("OK"));
    ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(_("Cancel"));
    ui->tip->question(content, next);*/

	//setWindowTitle(title);
    ui->tip->question(content, next);
    qDebug()<<"109content=="<<content;
	ui->stackedWidget->setCurrentIndex(1);
    if(m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    return !ExecDialog::exec();
}

QString ExecDialog::input(QString title, QString content, QString result)
{
    //setWindowTitle(_("Burner") + " - " + title);
    //setWindowTitle(title);
	setFixedSize(336,198);
    this->content(content);
    this->result(result);
    ui->stackedWidget->setCurrentIndex(0);
    ui->inputPage->setFixedHeight(88);
    //ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    //ui->buttonBox->button(QDialogButtonBox::Ok)->setText(_("OK"));
    //ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(_("Cancel"));
    if(m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    if (!ExecDialog::exec())
    {
        return ui->inputPage->result();
    }
    return QString();
}

bool ExecDialog::input(QString &inputed, QString title , QString content, QString result)
{
    //setWindowTitle(_("Burner") + " - " + title);
    //setWindowTitle(title);
	setFixedSize(336,198);
    this->content(content);
    this->result(result);
    ui->stackedWidget->setCurrentIndex(0);
    ui->inputPage->setFixedHeight(88);
    //ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    //ui->buttonBox->button(QDialogButtonBox::Ok)->setText(_("OK"));
    //ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(_("Cancel"));
    inputed.clear();

    if(m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    if (!ExecDialog::exec())
    {
        inputed = ui->inputPage->result();
        return true;
    }
    return false;
}

void ExecDialog::paintEvent(QPaintEvent *event)
{
    QPalette pal = palette();
    if ("ukui-black" == MAIN->theme())
        pal.setBrush(QPalette::Background, QColor("#484848"));
    else
        pal.setBrush(QPalette::Background, QColor("#FFFFFF"));
    setPalette(pal);
    QDialog::paintEvent(event);
}

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

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

QPushButton* ExecDialog::replayEnsure(){
	return m_oEnsureBtn;
}

QPushButton* ExecDialog::replayCancel(){
	return m_oCancelBtn;
}
void ExecDialog::close(){
     this->accept();
}
void ExecDialog::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Escape:
        this->accept();
        return;
    case  Qt::Key_Enter:
        this->reject();
        return;
    case Qt::Key_Return:
        this->reject();
        return;
    }

    QDialog::keyPressEvent(event);
}
