#ifndef CLEARANCEANALYTICUI_20_NGNFZD_H
#define CLEARANCEANALYTICUI_20_NGNFZD_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QListView>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QStandardItemModel>
#include <QDebug>
#include <QTimer>
#include <QAbstractItemView>
#include <QIcon>
#include <QModelIndex>

#include "../HTTPClient/client.h"
#include "../MonitorUI/MonitorUI.h"
#include "tools.h"

QT_BEGIN_NAMESPACE

class ClearanceAnalyticWidget:public QWidget
{
public:
    QHBoxLayout *horizontalLayout;
    QListView *deviceList;
    QWidget *displayWidget;
    QGridLayout *gridLayout;
    QWidget *infoWidget;
    QVBoxLayout *verticalLayout;
    QLabel *infoLabel;
    QLabel *trendLabel;
    QStandardItemModel *deviceModel;
    QTimer* timer;

    MonitorWidget* monitor;
    int     curId;

    void setupUi(QWidget *ClearanceAnalyticWidget)
    {
        if (ClearanceAnalyticWidget->objectName().isEmpty())
            ClearanceAnalyticWidget->setObjectName(QString::fromUtf8("ClearanceAnalyticWidget"));
        ClearanceAnalyticWidget->resize(1228, 773);
        QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
        sizePolicy.setHorizontalStretch(0);
        sizePolicy.setVerticalStretch(0);
        sizePolicy.setHeightForWidth(ClearanceAnalyticWidget->sizePolicy().hasHeightForWidth());
        ClearanceAnalyticWidget->setSizePolicy(sizePolicy);

        horizontalLayout = new QHBoxLayout(ClearanceAnalyticWidget);
        horizontalLayout->setSpacing(0);
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));

        horizontalLayout->setContentsMargins(20, 20, 20, 20);
        deviceList = new QListView(ClearanceAnalyticWidget);
        deviceList->setObjectName(QString::fromUtf8("deviceList"));
        deviceModel = new QStandardItemModel();
        deviceList->setModel(deviceModel);
        deviceList->setEditTriggers(QAbstractItemView::NoEditTriggers);

        horizontalLayout->addWidget(deviceList);

        displayWidget = new QWidget(ClearanceAnalyticWidget);
        displayWidget->setObjectName(QString::fromUtf8("displayWidget"));
        gridLayout = new QGridLayout(displayWidget);
        gridLayout->setSpacing(0);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        gridLayout->setContentsMargins(0, 0, 0, 0);

        horizontalLayout->addWidget(displayWidget);

        infoWidget = new QWidget(ClearanceAnalyticWidget);
        infoWidget->setObjectName(QString::fromUtf8("infoWidget"));
        verticalLayout = new QVBoxLayout(infoWidget);
        verticalLayout->setSpacing(0);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        verticalLayout->setContentsMargins(0, 0, 0, 0);
        infoLabel = new QLabel(infoWidget);
        infoLabel->setObjectName(QString::fromUtf8("infoLabel"));

        verticalLayout->addWidget(infoLabel);

        trendLabel = new QLabel(infoWidget);
        trendLabel->setObjectName(QString::fromUtf8("trendLabel"));

        verticalLayout->addWidget(trendLabel);


        horizontalLayout->addWidget(infoWidget);

        horizontalLayout->setStretch(0, 5);
        horizontalLayout->setStretch(1, 10);
        horizontalLayout->setStretch(2, 5);

        retranslateUi(ClearanceAnalyticWidget);

        QMetaObject::connectSlotsByName(ClearanceAnalyticWidget);

        addDevice();
        QModelIndex index = deviceList->model()->index(0, 0); // 获取第2行的索引
        deviceList->setCurrentIndex(index);

        timer = new QTimer();
        QObject::connect(timer, &QTimer::timeout, [&]() {
            // 计时器超时时更新标签
            addDevice();
            httpClient.getHeadroomList(curId);
            addHearoomFeature(curId);
        });
        timer->start(3000);


        connect(deviceList, &QListView::doubleClicked, [&](const QModelIndex& index) {
            if (index.isValid()) {
                QString fanCode = index.data().toString();
                curId = fans.allFans[fanCode].fanId;
                httpClient.getHeadroomList(curId);
                addHearoomFeature(curId);
            }
        });
    } // setupUi

    void retranslateUi(QWidget *ClearanceAnalyticWidget)
    {
        ClearanceAnalyticWidget->setWindowTitle(QCoreApplication::translate("ClearanceAnalyticWidget", "Form", nullptr));
//        infoLabel->setText(QCoreApplication::translate("ClearanceAnalyticWidget", "\345\256\236\346\227\266\344\277\241\346\201\257", nullptr));
        trendLabel->setText(QCoreApplication::translate("ClearanceAnalyticWidget", "\345\233\276\350\241\250\344\277\241\346\201\257", nullptr));
    } // retranslateUi

    void addDevice(){
//        httpClient.getFanList();
        map allFans = fans.states;
        if (!monitor->isInitState()){
            if (monitor->displayList.size() > deviceModel->rowCount()){
                for (int i = 0; i <= allFans.size() - deviceModel->rowCount() + 1; i++) {
                    QStandardItem* emptyItem = new QStandardItem("");
                    deviceModel->appendRow(emptyItem);
                }
            }else if (monitor->displayList.size() < deviceModel->rowCount()){
                for (int i = 0; i <= deviceModel->rowCount() - allFans.size() + 2; i++) {
                    deviceModel->removeRow(0);
                }
            }
        }else{
            if (allFans.size() > deviceModel->rowCount()){
                for (int i = 0; i <= allFans.size() - deviceModel->rowCount() + 1; i++) {
                    QStandardItem* emptyItem = new QStandardItem("");
                    deviceModel->appendRow(emptyItem);
                }
            }else if (allFans.size() < deviceModel->rowCount()){
                for (int i = 0; i <= deviceModel->rowCount() - allFans.size() + 2; i++) {
                    deviceModel->removeRow(0);
                }
            }
        }

        int count = 0;
        for (auto it = allFans.begin(); it != allFans.end(); ++it) {
            if (monitor->shouldDisplay(it->first)){
                QStandardItem* item = deviceModel->item(count);

                // 修改QStandardItem对象的文本和图标
                item->setText(it->first);
//                qDebug()<<it->second;
                item->setIcon(getDevIcon(it->second));
                count++;
            }
        }

    }

    void addHearoomFeature(int fanId){
        vector headrooms = fans.idMaps[fanId].headrooms;
        QString text;
        for (auto it = headrooms.begin(); it != headrooms.end(); ++it) {
            if (it->featureValue != 0){
                text += it->featureName + " : " + QString::number(it->featureValue) + "\n";
                //            qDebug() << it->featureName;
            }else{
                text += it->featureName + " : " + QString("数据为空") + "\n";
            }
        }
        infoLabel->setText(text);
    }

    ClearanceAnalyticWidget(MonitorWidget* monitor) : monitor(monitor)
    {
        setupUi(this);
    }


};



QT_END_NAMESPACE

#endif // CLEARANCEANALYTICUI_20_NGNFZD_H
