#include "blockwidget.h"
#include "klabel.h"
#include "statusbutton.h"
#include "../lock-dialog/lockdialogmodel.h"
#include <QLabel>
#include <QListView>
#include <QStandardItemModel>
#include <QDebug>
#include <QVBoxLayout>
#include <QHBoxLayout>

BlockWidget::BlockWidget(LockDialogModel *model, QWidget* parent)
    : QWidget(parent)
    , m_modelLockDialog(model)
{
    initUi();
    setFocusPolicy(Qt::NoFocus);
}

BlockWidget::~BlockWidget()
{

}

void BlockWidget::initUi()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    this->setLayout(mainLayout);

    m_tipLabel = new KLabel(this);
    m_tipLabel->setAlignment(Qt::AlignCenter);
    QHBoxLayout *listLayout = new QHBoxLayout();
    listLayout->setAlignment(Qt::AlignCenter);
    m_listView = new QListView(this);
    listLayout->addWidget(m_listView);


    QHBoxLayout *buttonLayout = new QHBoxLayout();
    m_cancelButton = new QPushButton(this);
    m_cancelButton->setText(tr("Cancel"));
    m_cancelButton->setFocusPolicy(Qt::NoFocus);
    buttonLayout->addWidget(m_cancelButton);
    m_confirmButton = new QPushButton(this);
    m_confirmButton->setText(tr("Confrim"));
    m_confirmButton->setFocusPolicy(Qt::NoFocus);
    buttonLayout->addWidget(m_confirmButton);
    buttonLayout->setAlignment(Qt::AlignCenter);

    mainLayout->setSpacing(46);
    mainLayout->addStretch();
    mainLayout->addWidget(m_tipLabel);
    mainLayout->addLayout(listLayout);
    mainLayout->addLayout(buttonLayout);
    mainLayout->addStretch();

    m_curFontSize = m_modelLockDialog->getCurFontSize();
    m_ptToPx = m_modelLockDialog->getPtToPx();
    m_tipLabel->setFontSize((14 + m_curFontSize)*m_ptToPx);

    connect(m_cancelButton, &QPushButton::clicked, this, [&]() { emit cancelButtonclicked(); });
    connect(m_confirmButton, &QPushButton::clicked, this, [&]() { emit confirmButtonclicked(); });
}

void BlockWidget::setTips(const QString tips)
{
    m_cancelButton->setFixedSize(96, 36);
    m_confirmButton->setFixedSize(96, 36);
    m_tipLabel->setFixedWidth(this->width());
    m_cancelButton->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;}");
    m_confirmButton->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 + m_curFontSize) *m_ptToPx);
    m_confirmButton->setFont(sysFont);
    m_cancelButton->setFont(sysFont);
    m_confirmButton->show();
    m_tipLabel->show();
    m_listView->hide();
    m_tipLabel->setText(tips);
}

void BlockWidget::setWarning(QStringList list, int type)
{
    m_tipLabel->setFixedWidth(this->width());
    switch (type) {
    case 0:
        m_tipLabel->setText(tr("The following program is running to prevent the system from reboot!"));
        break;
    case 1:
        m_tipLabel->setText(tr("The following program is running to prevent the system from shutting down!"));
        break;
    case 2:
        m_tipLabel->setText(tr("The following program is running to prevent the system from suspend!"));
        break;
    case 3:
        m_tipLabel->setText(tr("The following program is running to prevent the system from hibernate!"));
        break;
    default:
        break;
    }
    m_listView->show();

    QStandardItemModel *model = new QStandardItemModel(this);
    QIcon icon;
    QString iconName = list.at(0);
    QString appName = list.at(1);

    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));

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

    m_confirmButton->hide();
    m_cancelButton->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 + m_curFontSize) *m_ptToPx);
    m_cancelButton->setFont(sysFont);
}

