
#ifndef MONITOROVOZEZ_H
#define MONITOROVOZEZ_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QtWidgets/QScrollArea>
#include <QDebug>
#include <QTimer>
#include <QMouseEvent>
#include <QApplication>
#include <QIcon>

#include "MonitorTagUI.h"
#include "../HTTPClient/client.h"

QT_BEGIN_NAMESPACE

class MonitorWidget:public QWidget
{
public:
    QVBoxLayout *verticalLayout;
    QHBoxLayout *operationLayout;
    QPushButton *exchangeButton;
    QSpacerItem *horizontalSpacer;
    QGridLayout *monitorLayout;
    QHBoxLayout *filterlLayout;
    QPushButton *interruptedButton;
    QPushButton *normalButton;
    QPushButton *standbyButton;
    QPushButton *hutdownButton;
    QPushButton *failureButton;
    QPushButton *runButton;
    QWidget     *fillWidget;
    int         state;

    QList<QList<MonitorTag*>> tagList;
    QTimer      *timer;

    void setupUi(QWidget *MonitorWidget)
    {
        state = 0;
        if (MonitorWidget->objectName().isEmpty())
            MonitorWidget->setObjectName(QString::fromUtf8("MonitorWidget"));
        verticalLayout = new QVBoxLayout(MonitorWidget);
        verticalLayout->setSpacing(0);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        verticalLayout->setContentsMargins(0, 0, 0, 0);
        operationLayout = new QHBoxLayout();
        operationLayout->setObjectName(QString::fromUtf8("operationLayout"));
        exchangeButton = new QPushButton(MonitorWidget);
        exchangeButton->setObjectName(QString::fromUtf8("exchangeButton"));

        operationLayout->addWidget(exchangeButton);

        horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

        operationLayout->addItem(horizontalSpacer);


        verticalLayout->addLayout(operationLayout);

        monitorLayout = new QGridLayout();
        monitorLayout->setObjectName(QString::fromUtf8("monitorLayout"));
        monitorLayout->setSpacing(30);
        monitorLayout->setContentsMargins(20, 20, 20, 20);
        monitorLayout->setColumnStretch(0, 1);
        monitorLayout->setColumnStretch(1, 1);
        monitorLayout->setColumnStretch(2, 1);
        monitorLayout->setColumnStretch(3, 1);
        monitorLayout->setColumnStretch(4, 1);
        monitorLayout->setColumnStretch(5, 1);
        monitorLayout->setColumnStretch(6, 1);
        fillWidget = new QWidget();
        fillWidget->setLayout(monitorLayout);
        connect(exchangeButton, &QPushButton::clicked, this, [=](){this->changeState();});


        QScrollArea *scrollArea = new QScrollArea();

        scrollArea->setWidget(fillWidget);
        scrollArea->setWidgetResizable(true);
        scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

        verticalLayout->addWidget(scrollArea);

        filterlLayout = new QHBoxLayout();
        filterlLayout->setObjectName(QString::fromUtf8("filterlLayout"));
        interruptedButton = new QPushButton(MonitorWidget);
        interruptedButton->setObjectName(QString::fromUtf8("interruptedButton"));
        interruptedButton->setIcon(QIcon(":/Static/fanInterrupted.png"));

        filterlLayout->addWidget(interruptedButton);

        normalButton = new QPushButton(MonitorWidget);
        normalButton->setObjectName(QString::fromUtf8("normalButton"));
        normalButton->setIcon(QIcon(":/Static/fanNormal.png"));

        filterlLayout->addWidget(normalButton);

        standbyButton = new QPushButton(MonitorWidget);
        standbyButton->setObjectName(QString::fromUtf8("standbyButton"));
        standbyButton->setIcon(QIcon(":/Static/fanStandby.png"));


        filterlLayout->addWidget(standbyButton);

        hutdownButton = new QPushButton(MonitorWidget);
        hutdownButton->setObjectName(QString::fromUtf8("hutdownButton"));
        hutdownButton->setIcon(QIcon(":/Static/fanHutdown.png"));

        filterlLayout->addWidget(hutdownButton);

        failureButton = new QPushButton(MonitorWidget);
        failureButton->setObjectName(QString::fromUtf8("failureButton"));
        failureButton->setIcon(QIcon(":/Static/fanError.png"));

        filterlLayout->addWidget(failureButton);

        runButton = new QPushButton(MonitorWidget);
        runButton->setObjectName(QString::fromUtf8("runButton"));
        runButton->setIcon(QIcon(":/Static/fanRun.png"));

        filterlLayout->addWidget(runButton);
        filterlLayout->setContentsMargins(10, 10, 10, 10);
        filterlLayout->setSpacing(20);

        verticalLayout->addLayout(filterlLayout);

        verticalLayout->setStretch(0, 2);
        verticalLayout->setStretch(1, 30);
        verticalLayout->setStretch(2, 3);

        MonitorWidget->setLayout(verticalLayout);

        QMetaObject::connectSlotsByName(MonitorWidget);
        retranslateUi(MonitorWidget);

        addTag();
        timer = new QTimer(MonitorWidget);
        QObject::connect(timer, &QTimer::timeout, [&]() {
            // 计时器超时时更新标签
            addTag();
        });
        timer->start(5000);

        connect(exchangeButton, &QPushButton::clicked, this, [=](){this->exchangeClicked();});

    } // setupUi

    void retranslateUi(QWidget *MonitorWidget)
    {
        MonitorWidget->setWindowTitle(QCoreApplication::translate("MonitorWidget", "Form", nullptr));
        exchangeButton->setText(QCoreApplication::translate("MonitorWidget", "\345\244\247\345\260\217\345\233\276\346\240\207\345\210\207\346\215\242", nullptr));
    } // retranslateUi

    void exchangeClicked()
    {
        int count = monitorLayout->count();

        // 遍历布局中的所有子对象
        for (int i = 0; i < count; ++i) {
            // 获取第 i 个子对象的指针
            QLayoutItem *item = monitorLayout->itemAt(i);
            MonitorTag *tag = dynamic_cast<MonitorTag*>(item->widget());
            // 判断子对象的类型
            if (tag) {
                if (this->state == 1) {
                    tag->stackedWidget->setCurrentIndex(1);
                } else if (this->state == 0) {
                    tag->stackedWidget->setCurrentIndex(0);
                }
            }
        }

    }

    void changeState()
    {
        if (state == 0){
            state = 1;
        } else if (state == 1) {
            state = 0;
        }
    }

    void addTag()
    {
        httpClient.getFanList();
        map allFans = fans.allFans;
        // 获取与 clicked() 信号相关的接收器数量

        while (QLayoutItem *item = monitorLayout->takeAt(0)) {
            if (QWidget *widget = item->widget()) {
                widget->deleteLater(); // 或使用 delete 关键字删除控件的内存
            }
            delete item;
        }
        tagList.clear();
        int count = 0;
        int index = 0;
        QList<MonitorTag*> tags;
        for (auto it = allFans.begin(); it != allFans.end(); ++it) {
            if (index == 6){
                tagList.append(tags);
                tags.clear();
                index = 0;
                count++;
            }
            MonitorTag *newTag = new MonitorTag(it->second, state);
            monitorLayout->addWidget(newTag, count, index);
            tags.append(newTag);
            index++;
        }
        if (allFans.size() < 28){
            fillWidget->setFixedHeight(228 * 4);
            for (int i = 0; i < 28 - allFans.size(); i++) {
                if (index == 6){
                    index = 0;
                    count++;
                }
                QWidget *w = new QWidget();
                monitorLayout->addWidget(w, count, index);
                index++;
            }
            monitorLayout->setRowStretch(0, 1);
            monitorLayout->setRowStretch(1, 1);
            monitorLayout->setRowStretch(2, 1);
            monitorLayout->setRowStretch(3, 1);
        }
        fillWidget->setFixedWidth(1900);
        setButtonMes();
//        int c = monitorLayout->count();
//        qDebug() << c;

    }

    void setButtonMes(){
        interruptedButton->setText(QString("通讯中断") + QString::number(fans.interruptedFans.size()));
        normalButton->setText(QString("正常运行") + QString::number(fans.normalFans.size()));
        standbyButton->setText(QString("待机等风") + QString::number(fans.standbyFans.size()));
        hutdownButton->setText(QString("停机维护") + QString::number(fans.hutdownFans.size()));
        failureButton->setText(QString("故障停机") + QString::number(fans.interruptedFans.size()));
        runButton->setText(QString("启动") + QString::number(fans.runFans.size()));
    }

    MonitorWidget()
    {
        setupUi(this);
    }


};






QT_END_NAMESPACE

#endif // MONITOROVOZEZ_H




