/*
* Copyright (C) 2020, Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
*
*/
#include "closeOrHide.h"
#include "ui_closeOrHide.h"
#include "QDebug"
#include <X11/Xlib.h>
#include "xatom-helper.h"
#include "constant_class.h"
#include "gsettingsubject.h"
#include <math.h>
#ifdef NOTKAIMING
#include "ukuiwindowhelper.h"
#endif
#include "globalinstance.h"

extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed);

close_or_hide::close_or_hide(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::close_or_hide)
{
    ui->setupUi(this);
    ui->surebtn->setText("");
    ui->cancelbtn->setText("");
//    this->setProperty("blurRegion", QRegion(QRect(1, 1, 1, 1)));
//    setAttribute(Qt::WA_TranslucentBackground);
//    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);

    ui->backrunRadio->setChecked(1);

    // 添加窗管协议
//    XAtomHelper::setStandardWindowHint(this->winId());
//    XAtomHelper::setStandardWindowRadius(this->winId(),WINDOWN_RADIUS);
#ifdef NOTKAIMING
    if(GlobalInstance::getInstance()->session_desktop() == GlobalInstance::KYLIN_WLCOM){
        UkuiWindowHelper *windowHelper = new UkuiWindowHelper(this);
        windowHelper->removeTitleBar();
    } else {
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    }
#else
    kdk::UkuiStyleHelper::self()->removeHeader(this);
#endif
    ui->closeInfoLabel->setText(tr("Please select the state after closing:"));
    ui->closeInfoLabel->setWordWrap(true);
    ui->closeInfoLabel->setAlignment(Qt::AlignTop);
    //调色板
    QPalette palette = ui->surebtn->palette();
    palette.setColor(QPalette::Button,QColor(61,107,229,255));
    palette.setBrush(QPalette::ButtonText, QBrush(Qt::white));
    //保留按钮
    ui->surebtn->setPalette(palette);
    //退出按钮
    QPalette palette1 = ui->closebtn->palette();
    QColor ColorPlaceholderText1(255,255,255,0);
    QBrush brush;
    brush.setColor(ColorPlaceholderText1);
    palette.setBrush(QPalette::Button, brush);
    ui->closebtn->setPalette(palette1);

    ui->closebtn->setIcon(QIcon::fromTheme("window-close-symbolic"));
    ui->closebtn->setProperty("isWindowButton", 0x2);
    ui->closebtn->setProperty("useIconHighlightEffect", 0x8);
    ui->closebtn->setFlat(true);
//    主题框架1.0.6-5kylin2

    //配置重要按钮
    ui->surebtn->setProperty("isImportant", true);
    ui->cancelbtn->setProperty("useButtonPalette", true);
    //关闭按钮去掉聚焦状态
    ui->closebtn->setFocusPolicy(Qt::NoFocus);


    //按钮文本居中显示，避免使用stylesheet，添加label于pushbutton上
    QLabel *sureLabel = new QLabel();
    sureLabel->setObjectName("ukui-clock_QLabel_sureLabel");
    sureLabel->setText(tr("sure"));
    sureLabel->adjustSize();
    QHBoxLayout *myLayout = new QHBoxLayout(ui->surebtn);
    myLayout->addWidget(sureLabel);
    myLayout->setContentsMargins(0,2,0,4);
    myLayout->setAlignment(Qt::AlignHCenter);
    ui->surebtn->setLayout(myLayout);

    QLabel *cancelLabel = new QLabel();
    cancelLabel->setObjectName("ukui-clock_QLabel_cancelLabel");
    cancelLabel->setText(tr("cancel"));
    cancelLabel->adjustSize();
    QHBoxLayout *myLayout1 = new QHBoxLayout(ui->cancelbtn);
    myLayout1->addWidget(cancelLabel);
    myLayout1->setContentsMargins(0,2,0,4);
    myLayout1->setAlignment(Qt::AlignHCenter);
    ui->cancelbtn->setLayout(myLayout1);

    ui->questionIconLabel->setPixmap(QIcon::fromTheme("dialog-question").pixmap(24,24));

    settingsStyle();
}

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

void close_or_hide::on_closebtn_clicked()
{
    this->hide();
    close_flag = 0;
}

void close_or_hide::on_surebtn_clicked()
{
    if(ui->backrunRadio->isChecked()==true){
        this->hide();
         close_flag = 1;
    }else{
        this->hide();
        close_flag = 2;
    }
}

void close_or_hide::on_cancelbtn_clicked()
{
    this->hide();
}

void close_or_hide::settingsStyle()
{
    GsettingSubject * subject = GsettingSubject::getInstance();
    connect(subject,&GsettingSubject::iconChnaged, this,[=](){
        ui->questionIconLabel->setPixmap(QIcon::fromTheme("dialog-question").pixmap(24,24));
    });
}

void close_or_hide::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    QPainterPath rectPath;
    rectPath.addRect(this->rect());
    p.fillPath(rectPath,palette().color(QPalette::Base));
}

void close_or_hide::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        this->dragPosition = event->globalPos() - frameGeometry().topLeft();
        this->mousePressed = true;
    }
    QWidget::mousePressEvent(event);
}

void close_or_hide::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        this->mousePressed = false;
        this->setCursor(Qt::ArrowCursor);
    }

    QWidget::mouseReleaseEvent(event);
}

void close_or_hide::mouseMoveEvent(QMouseEvent *event)
{
    if (this->mousePressed) {
        move(event->globalPos() - this->dragPosition);
        this->setCursor(Qt::ClosedHandCursor);
    }

    QWidget::mouseMoveEvent(event);
}
