#include <QKeyEvent>
#include "powerlistwidget.h"

PowerListWidget::PowerListWidget(LockDialogModel *model ,QWidget *parent)
    : m_modelLockDialog(model),
      MyListWidget(parent)
{
    initBtnShowFlag();
    initUI();
    initConnections();
}

PowerListWidget::~PowerListWidget()
{

}

bool PowerListWidget::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyRelease) {
        MyListWidget *listWidget = qobject_cast<MyListWidget *>(obj);
        if (listWidget) {
            if (listWidget && listWidget->hasFocus()) {   //Tab键切入时，设置焦点状态
                listWidget->setCurrentRow(listWidget->currentRow(), QItemSelectionModel::SelectionFlag::SelectCurrent);
                listWidget->currentItemChanged(listWidget->currentItem(), nullptr);
            } else {    //Tab键切出时，清空焦点状态
                listWidget->setCurrentRow(listWidget->currentRow(), QItemSelectionModel::SelectionFlag::Clear);
            }
        }
    } else if (event->type() == QEvent::MouseButtonRelease) { // 电源管理界面点击后清空选中状态
        MyListWidget *listWidget = qobject_cast<MyListWidget *>(obj);
        if (listWidget)
            this->setCurrentRow(0, QItemSelectionModel::SelectionFlag::Clear);
    }
    return MyListWidget::eventFilter(obj, event);
}

void PowerListWidget::initUI()
{
    setFlow(QListWidget::LeftToRight);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    QString powerListStyle = "QListWidget{background:rgba(255, 255, 255, 0%); border: 0px;}"
                             "QListWidget::item{background:rgba(255, 255, 255, 0%);}";
    setStyleSheet(powerListStyle);
    setFixedSize(QSize(BTN_ITEM_SIZE_WIDTH*sizeof(powerBtnList)/sizeof(powerBtnList[0])+1, BTN_ITEM_SIZE_HEIGHT));
//    powerBtnList[0].func = &PowerListWidget::setSystemSuspend1();
    onUpdateListInfo();
}

void PowerListWidget::initConnections()
{
    connect(this, &PowerListWidget::itemClicked, this, &PowerListWidget::onListItemClicked);

}
void PowerListWidget::initBtnShowFlag()
{
    for (int i = 0; i < sizeof(powerBtnList)/sizeof(powerBtnList[0]) ; i++)
    {
        if (powerBtnList[i].canFuncName == QString("CanPowerOff"))
            powerBtnList[i].m_show_flag = m_modelLockDialog->getCanPowerOff();

        if (powerBtnList[i].canFuncName == QString("CanSuspend"))
            powerBtnList[i].m_show_flag = m_modelLockDialog->getCanSuspend();

        if (powerBtnList[i].canFuncName == QString("CanReboot"))
            powerBtnList[i].m_show_flag = m_modelLockDialog->getCanReboot();

        if (powerBtnList[i].canFuncName == QString("CanHibernate"))
            powerBtnList[i].m_show_flag = m_modelLockDialog->getCanHibernate();

    }

}

void PowerListWidget::onUpdateListInfo()
{
    this->clear();

    for (int i = 0; i < sizeof(powerBtnList)/sizeof(powerBtnList[0]) ; i++)
    {
        qWarning() << __LINE__ << __FUNCTION__<< "11111111111" << i << powerBtnList[i].m_strName << powerBtnList[i].m_show_flag ;
        if (!powerBtnList[i].m_show_flag)
            continue;

        PowerItemWidget *btnWdg = new PowerItemWidget(this);
        btnWdg->setObjectName(powerBtnList[i].m_strName);
        QListWidgetItem *btnItem = new QListWidgetItem();
        btnItem->setSizeHint(QSize(powerBtnList[i].m_item_width, powerBtnList[i].m_item_height));

        this->insertItem(this->count(), btnItem);
        this->setItemWidget(btnItem, btnWdg);
        btnWdg->setText(powerBtnList[i].m_strName);
        btnWdg->setToolTip(powerBtnList[i].m_strToolTip);
        btnWdg->setIcon(QIcon(powerBtnList[i].m_strIcon).pixmap(powerBtnList[i].m_icon_width, powerBtnList[i].m_icon_width));
    }
}

void PowerListWidget::onListItemClicked(QListWidgetItem *item)
{
    qWarning()  << __FUNCTION__ << __LINE__  ;
    QWidget *widget = itemWidget(item);
    PowerItemWidget *currentItem = qobject_cast<PowerItemWidget *>(widget);
    for (int i = 0; i < sizeof(powerBtnList)/sizeof(powerBtnList[0]) ; i++)
    {
        if(powerBtnList[i].m_strName == currentItem->objectName())
        {
            if (powerBtnList[i].setFuncName == "Hibernate" || powerBtnList[i].setFuncName == "Suspend") {
                QStringList sleepLockcheck = m_modelLockDialog->getSleepLockcheck();
                if (!sleepLockcheck.isEmpty()) {
                    Q_EMIT showInhibitWarning(sleepLockcheck, powerBtnList[i].m_inhibitType);
                    break;
                }
            }
            if (powerBtnList[i].setFuncName == "Reboot" || powerBtnList[i].setFuncName == "PowerOff") {
                QStringList shutdownLockcheck = m_modelLockDialog->getShutdownLockcheck();
                if (shutdownLockcheck.count() > 0) {
                    Q_EMIT showInhibitWarning(shutdownLockcheck, powerBtnList[i].m_inhibitType);
                    break;
                } else if (m_modelLockDialog->getLoggedInUsersCount() > 1) {
                    Q_EMIT mulUsersLogined(powerBtnList[i].m_strName);
                    break;
                }
            }
            qWarning()  << __LINE__  << "=================" << powerBtnList[i].setFuncName << __FUNCTION__;
            Q_EMIT m_modelLockDialog->setPowerManager(powerBtnList[i].setFuncName);
            break;
        }
    }
}

void PowerListWidget::updateWidgetSize()
{
    if (count() * 40 + 10 > maximumHeight()) {
        setFixedHeight(maximumHeight());
    } else {
        setFixedHeight(count() * 40 + 10);
    }
    adjustSize();
}

void PowerListWidget::setSystemPowerStatus(QString statusName)
{
    qWarning()  << __LINE__  << __FUNCTION__ << statusName;

}
