#include "tip_burnresult.h"
#include "ui_tip_burnresult.h"
#include "src/frame/xatom-helper.h"
#include "src/po/translation.h"
#include "src/model/logicmain.h"

#include <QIcon>
#include <QDebug>
#include <ukuistylehelper.h>
using namespace kdk;


#define ICON_SIZE QSize(22, 22)
#define ICON_SIZE_CLOSE QSize(16, 16)
#define ICON_SIZE_LOGO QSize(24, 24)

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

Tip_BurnResult::Tip_BurnResult(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Tip_BurnResult)
{
    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);
    }

    //setAttribute(Qt::WA_ShowModal, true);


    ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok);
    ui->buttonBox->button(QDialogButtonBox::Ok)->setIcon(QIcon());
    ui->buttonBox->button(QDialogButtonBox::Ok)->setText(_("OK"));
    connect(this, SIGNAL(success(QString)), this, SLOT(slotsSuccess(QString)));
    connect(this, SIGNAL(fail(QString)), this, SLOT(slotsFailed(QString)));

    //close 按钮
    ui->close->setProperty("isWindowButton", 0x2);
    ui->close->setFocusPolicy(Qt::NoFocus); // 当为子窗口时，避免与回车关闭窗口冲突
    ui->close->setIcon(QIcon::fromTheme("window-close-symbolic"));
    ui->close->setIconSize(ICON_SIZE_CLOSE);
    connect(ui->close, SIGNAL(clicked(bool)), this, SLOT(slotsClose()));
    ui->close->setStyleSheet("QPushButton{background:transparent}");

    // 标题的logo
   // ui->logo->setPixmap(QIcon::fromTheme("burner").pixmap(ICON_SIZE_LOGO));

    //确定、取消按钮
    f = ui->buttonBox->font();
    f.setPixelSize(14);
    ui->buttonBox->setFont(f);
   // ui->title->setFont(f);
}

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

Tip_BurnResult *Tip_BurnResult::instance()
{
    if (nullptr == m_oInstance)
    {
        QMutexLocker locker(&m_oMutex);
        if (nullptr == m_oInstance)
        {
            m_oInstance = new Tip_BurnResult();
        }
    }
    return m_oInstance;
}

void Tip_BurnResult::updatePosition(QPoint p)
{
    QPoint sp;
    //setFixedSize(424, 164);
    sp.setX(p.x()-225);
    sp.setY(p.y()-115);

    move(sp);
}

void Tip_BurnResult::slotsSuccess(QString name)
{
    QFont f;
    //qDebug() << "Tip_BurnResult Tip_BurnResult::slotsSuccess";
    f = ui->labelContent->font();
    f.setPixelSize(14);
    //f.setWeight(400);
    f.setWeight(QFont::Medium);
    f.setFamily("Noto Sans CJK SC Medium");
   // f.setBold(true);
    ui->labelContent->setFont(f);
    ui->labelContent->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    ui->labelContent->setText(name + _(" Success"));
    ui->labelLogo->setPixmap(QIcon::fromTheme("ukui-dialog-success").pixmap(ICON_SIZE));
    this->raise();//控件置顶
    if (m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    this->exec();//控件显示
}

void Tip_BurnResult::slotsFailed(QString name)
{
    QFont f;
    //qDebug() << "Tip_BurnResult Tip_BurnResult::slotsFailed";
    f = ui->labelContent->font();
    f.setPixelSize(14);
    f.setWeight(QFont::Medium);
  //  f.setBold(true);
    f.setFamily("Noto Sans CJK SC Medium");
    ui->labelContent->setFont(f);
    ui->labelContent->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    ui->labelContent->setText(name + _(" Failed"));
    ui->labelContent->setFixedWidth(this->width() - 60 - 25);
    ui->labelContent->setWordWrap(true);
    ui->labelContent->adjustSize();
    ui->labelLogo->setPixmap(QIcon::fromTheme("dialog-error").pixmap(ICON_SIZE));
    this->raise();//控件置顶
    if (m_bIsWayland)
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    this->exec();//控件显示
}

void Tip_BurnResult::slotsTitle(QString title)
{
   // ui->title->setText(_("Burner") + " - " + title);
   // qDebug()<<__LINE__<<__func__<<title;
}


void Tip_BurnResult::slotsClose()
{
    this->hide();
}

void Tip_BurnResult::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 Tip_BurnResult::on_buttonBox_clicked(QAbstractButton *button)
{

    if(QDialogButtonBox::Ok == ui->buttonBox->standardButton(button))
    {
        this->hide();
    }
    //qDebug() << "clicked ok按钮";
}
