#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/ukuistylehelper.h>

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

ExecDialog::ExecDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ExecDialog)
{
    this->setObjectName("kylin-burner_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->buttonBox->setAccessibleName("kylin-burner_QDialogButtonBox_ExecDialog_buttonBox");
    ui->inputPage->setAccessibleName("kylin-burner_Input_ExecDialog_inputPage");
    ui->stackedWidget->setAccessibleName("kylin-burner_QStackedWidget_ExecDialog_stackedWidget");
    ui->tip->setAccessibleName("kylin-burner_tips_ExecDialog_tip");
    ui->title->setAccessibleName("kylin-burner_WindowTitle_ExecDialog_title");
    ui->widget->setAccessibleName("kylin-burner_QWidget_ExecDialog_widget");

    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)
{
	setFixedSize(424,164);
    ui->stackedWidget->setCurrentIndex(1);
    ui->tip->information(content, next);
	ui->buttonBox->removeButton(m_oCancelBtn);
    return !ExecDialog::exec();
}

void ExecDialog::error(QString title, QString content, bool next)
{
    m_oTipTitle = title;
    if (CFG->get("EXEC", title, false).toBool()) return;
    ui->stackedWidget->setCurrentIndex(1);
    qDebug()<<"90content=="<<content;
    ui->buttonBox->removeButton(m_oCancelBtn);
    ui->tip->setFixedSize(380,150);
    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);

    ui->tip->question(content, next);
    qDebug()<<"(ExecDialog)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)
{
	setFixedSize(336,198);
    this->content(content);
    this->result(result);
    ui->stackedWidget->setCurrentIndex(0);
    ui->inputPage->setFixedHeight(88);
    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)
{
	setFixedSize(336,198);
    this->content(content);
    this->result(result);
    ui->stackedWidget->setCurrentIndex(0);
    ui->inputPage->setFixedHeight(88);
    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);
}
