#include "onclosedialog.h"
#include <QApplication>
#include <kysdk/applications/klabel.h>

#include "macro.h"

#define ICON_EDIT_SIZE 16,16

OnCloseDialog::OnCloseDialog(QWidget *parent) : KDialog(parent)
{
    setWindowIcon(QIcon::fromTheme("ukui-system-monitor"));
    setWindowTitle(tr("System Monitor"));

    const QByteArray ifid(MENU_SCHEMA);
    if(QGSettings::isSchemaInstalled(ifid)) {
        m_pSystemMonitorGSetting = new QGSettings(ifid);
    }

    initUI();
    initStatus();
    setConnect();
}

OnCloseDialog::~OnCloseDialog()
{

}

void OnCloseDialog::initUI()
{
    setFixedSize(376, 288);
    KLabel *exitSettingTitleLabel = new KLabel();
    exitSettingTitleLabel->setLabelType(KLabelType::NormalType);
    const auto radio = qApp->devicePixelRatio();
    exitSettingTitleLabel->setPixmap(QIcon::fromTheme("dialog-warning").pixmap(ICON_EDIT_SIZE*radio));
    exitSettingTitleLabel->setText(tr("When clicking the close button"));
    QFont ft = exitSettingTitleLabel->font();
    ft.setBold(true);
    exitSettingTitleLabel->setFont(ft);

    m_pMinimizeBtn = new QRadioButton(tr("Minimize to tray"));
    m_pExitBtn = new QRadioButton(tr("Exit Application"));
    m_pRadioBtnGroup = new QButtonGroup();
    m_pRadioBtnGroup->addButton(m_pMinimizeBtn, 0);
    m_pRadioBtnGroup->addButton(m_pExitBtn, 1);

    m_pRememberCheckBox = new QCheckBox(tr("No more inquiries"));

    m_pCancelBtn = new KPushButton();
    m_pCancelBtn->setText(tr("Cancel"));
    m_pConfirmBtn = new KPushButton();
    m_pConfirmBtn->setText(tr("Confirm"));
    m_pConfirmBtn->setBackgroundColorHighlight(true);
    m_pConfirmBtn->setEnabled(true);
    QHBoxLayout *bottomBtnsHorLayout= new QHBoxLayout;
    bottomBtnsHorLayout->setContentsMargins(0, 0, 0, 0);
    bottomBtnsHorLayout->addStretch();
    bottomBtnsHorLayout->addWidget(m_pCancelBtn);
    bottomBtnsHorLayout->addWidget(m_pConfirmBtn);

    m_pMainVerLayout = new QVBoxLayout;
    m_pMainVerLayout->setContentsMargins(48, 8, 16, 16);
    m_pMainVerLayout->addWidget(exitSettingTitleLabel);
    m_pMainVerLayout->addWidget(m_pMinimizeBtn);
    m_pMainVerLayout->addWidget(m_pExitBtn);
    m_pMainVerLayout->addWidget(m_pRememberCheckBox);
    m_pMainVerLayout->addSpacing(24);
    m_pMainVerLayout->addLayout(bottomBtnsHorLayout);

    mainWidget()->setLayout(m_pMainVerLayout);
}

void OnCloseDialog::initStatus()
{
    bool directlyExit = m_pSystemMonitorGSetting->get(DIRECTLY_EXIT).toBool();
    m_pExitBtn->setChecked(directlyExit);
    m_pMinimizeBtn->setChecked(!directlyExit);
    m_pRememberCheckBox->setChecked(false);
}

void OnCloseDialog::setConnect()
{
    connect(m_pCancelBtn, &KPushButton::clicked, this, [=](){
        this->close();
    });

    connect(m_pConfirmBtn, &KPushButton::clicked, this, [=](){
        int checkedBtnId = m_pRadioBtnGroup->checkedId();
        m_pSystemMonitorGSetting->set(DIRECTLY_EXIT, checkedBtnId == 1 ? true : false);
        if (checkedBtnId == 1) {
            Q_EMIT needSaveSetting();
            qApp->exit();
        } else {
            Q_EMIT closeMainWindow();
        }

        bool inquiries = m_pRememberCheckBox->isChecked();
        m_pSystemMonitorGSetting->set(INQUIRIES, !inquiries);

        this->close();
    });

}

void OnCloseDialog::keyPressEvent(QKeyEvent * event)
{
    switch (event->key())
    {
    case Qt::Key_Enter:
        if (m_pConfirmBtn->isEnabled())
            m_pConfirmBtn->clicked();
        break;
    case Qt::Key_Return:
        if (m_pConfirmBtn->isEnabled())
            m_pConfirmBtn->clicked();
        break;
    default:
        QDialog::keyPressEvent(event);
    }
}

