#include "settingdialog.h"

#include <QFrame>
#include <QPainter>
#include <QVariant>
#include <QApplication>

#include <kysdk/applications/klabel.h>

#include "macro.h"

SettingDialog::SettingDialog(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();
}

SettingDialog::~SettingDialog()
{

}

void SettingDialog::initUI()
{
    setFixedSize(452, 243);
    KLabel *exitSettingTitleLabel = new KLabel();
    exitSettingTitleLabel->setLabelType(KLabelType::NormalType);
    exitSettingTitleLabel->setText(tr("Exit settings"));
    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_pExitSettingFrame = new SettingFrame();
    QVBoxLayout *VLayout = new QVBoxLayout;
    VLayout->setContentsMargins(16, 0, 0, 0);
    VLayout->addWidget(m_pMinimizeBtn);
    VLayout->addWidget(m_pExitBtn);
    m_pExitSettingFrame->setLayout(VLayout);

    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(24, 10, 24, 24);
    m_pMainVerLayout->addWidget(exitSettingTitleLabel);
    m_pMainVerLayout->addWidget(m_pExitSettingFrame);
    m_pMainVerLayout->addSpacing(24);
    m_pMainVerLayout->addLayout(bottomBtnsHorLayout);

    mainWidget()->setLayout(m_pMainVerLayout);
    update();
}

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

void SettingDialog::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);

        this->close();
    });

}

SettingFrame::SettingFrame(QWidget *parent) : QFrame(parent)
{
}

SettingFrame::~SettingFrame()
{
}

void SettingFrame::paintEvent(QPaintEvent *event)
{
    QPalette palette = qApp->palette();
    QColor color = palette.color(QPalette::Active, QPalette::Window);

    QPainter painter(this);
    painter.save();
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setBrush(color);
    painter.setPen(Qt::transparent);
    QRect rect = this->rect();
    painter.drawRoundedRect(rect.x() + 1, rect.y() + 1, rect.width() - 2, rect.height() - 2, 6, 6);
    QFrame::paintEvent(event);
}
