/*
 * Copyright (C) 2022 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/>.
 *
**/
#include "surewindow.h"
#include "ui_surewindow.h"
#include "configuration.h"
#include <QDebug>
#include <QStandardItemModel>

SureWindow::SureWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SureWindow),
    configuration(Configuration::instance())
{
    ui->setupUi(this);
    curFontSize = configuration->getFontSize();
    m_ptToPx = configuration->getPtToPx();
    ui->tipLabel->setFontSize(14 + curFontSize);
    //ui->tipLabel->setWordWrap(true);
    connect(ui->cancelBtn, &QPushButton::clicked, this, [&]() { emit cantelButtonclicked(); });
    connect(ui->confirmBtn, &QPushButton::clicked, this, [&]() { emit confirmButtonclicked(); });
}

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

void SureWindow::setTips(const QString tips)
{
    ui->cancelBtn->setStyleSheet("QPushButton{background: rgba(255, 255, 255, 0.2);border-radius: 8px;color: white;}"
                                 "QPushButton:hover{background: rgba(255, 255, 255, 0.4);border-radius: 8px;}"
                                 "QPushButton:pressed {background: rgba(255, 255, 255, 0.3);border-radius: 8px;}");
    ui->confirmBtn->setStyleSheet("QPushButton{background: rgba(255, 255, 255, 0.2);border-radius: 8px;color: white;}"
                                  "QPushButton:hover{background: rgba(255, 255, 255, 0.4);border-radius: 8px;}"
                                  "QPushButton:pressed {background: rgba(255, 255, 255, 0.3);border-radius: 8px;}");
    sysFont.setPointSize((16 + curFontSize) *m_ptToPx);
    ui->confirmBtn->setFont(sysFont);
    ui->cancelBtn->setFont(sysFont);
    ui->confirmBtn->show();
    ui->tipLabel->show();
    ui->listView->hide();
    ui->tipLabel->setText(tips);
}

void SureWindow::setWarning(QVector<InhibitInfo::InhibitorInfo> &list, int type)
{
    switch (type) {
    case 0:
        ui->tipLabel->setText(tr("The following program is running to prevent the system from reboot!"));
        break;
    case 1:
        ui->tipLabel->setText(tr("The following program is running to prevent the system from shutting down!"));
        break;
    case 2:
        ui->tipLabel->setText(tr("The following program is running to prevent the system from suspend!"));
        break;
    case 3:
        ui->tipLabel->setText(tr("The following program is running to prevent the system from hibernate!"));
        break;
    default:
        break;
    }
    ui->tipLabel->adjustSize();
    adjustSize();
    ui->listView->show();

    QStandardItemModel *model = new QStandardItemModel(this);
    for (auto iter = list.begin(); iter != list.end(); ++iter) {
        QIcon icon;
        QString appName = iter->name;
        QString iconName = iter->icon;

        if (!iconName.isEmpty() && QIcon::hasThemeIcon(iconName)) {
            icon = QIcon::fromTheme(iconName);
        } else if (QIcon::hasThemeIcon("application-x-desktop")) {
            icon = QIcon::fromTheme("application-x-desktop");
        }
        model->appendRow(new QStandardItem(icon, appName));
    }

    ui->listView->verticalScrollMode();
    ui->listView->setStyleSheet("color:white; background-color: rgba(255,255,255,30%); border-radius: 12px;");
    sysFont.setPointSize((10 + curFontSize) *m_ptToPx);
    ui->listView->setFont(sysFont);
    ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->listView->setIconSize(QSize(32,32));
    ui->listView->setModel(model);
    ui->listView->setFixedSize(520, 320);
    adjustSize();
    ui->cancelBtn->setFixedSize(120, 48);

    ui->confirmBtn->hide();
    ui->cancelBtn->setStyleSheet("QPushButton{background: rgba(255, 255, 255, 0.2);border-radius: 8px;color: white;}"
                                 "QPushButton:hover{background: rgba(255, 255, 255, 0.4);border-radius: 8px;}"
                                 "QPushButton:pressed {background: rgba(255, 255, 255, 0.3);border-radius: 8px;}");
    sysFont.setPointSize((16 + curFontSize) *m_ptToPx);
    ui->cancelBtn->setFont(sysFont);
}
