
#ifndef MONITORTAGPWEJEQ_H
#define MONITORTAGPWEJEQ_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStackedWidget>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QTimer>
#include <QContextMenuEvent>
#include <QComboBox>
#include <QDebug>


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

QT_BEGIN_NAMESPACE

class MonitorTag:public QWidget
{
public:
    QHBoxLayout *horizontalLayout;
    QStackedWidget *stackedWidget;
    QWidget *detailPage;
    QHBoxLayout *horizontalLayout_3;
    QVBoxLayout *verticalLayout;
    QLabel *pictureLabel;
    QLabel *nameLabel;
    QGridLayout *gridLayout;
    QPushButton *clearanceButton;
    QPushButton *flangeButton;
    QPushButton *gyroscopeButton;
    QLabel *distanceLabel;
    QLabel *angelLabel;
    QWidget *simplifyPage;
    QWidget *horizontalLayoutWidget;
    QHBoxLayout *horizontalLayout_2;
    QLabel *pictureLabel_2;
    QLabel *statusLabel;
    QComboBox *stateBox;

    FanClass    fan;
    QTimer      timer;
    int         state;

    void setupUi(QWidget *MonitorTag)
    {
        if (MonitorTag->objectName().isEmpty())
            MonitorTag->setObjectName(QString::fromUtf8("MonitorTag"));

        horizontalLayout = new QHBoxLayout(MonitorTag);
        horizontalLayout->setSpacing(0);
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
        horizontalLayout->setContentsMargins(10, 10, 10, 10);
        stackedWidget = new QStackedWidget(MonitorTag);
        stackedWidget->setObjectName(QString::fromUtf8("stackedWidget"));

        detailPage = new QWidget();
        detailPage->setObjectName(QString::fromUtf8("detailPage"));
        horizontalLayout_3 = new QHBoxLayout(detailPage);
        horizontalLayout_3->setSpacing(0);
        horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3"));
        horizontalLayout_3->setContentsMargins(0, 0, 0, 0);
        verticalLayout = new QVBoxLayout();
        verticalLayout->setSpacing(0);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));

        stateBox = new QComboBox(MonitorTag);
        QStringList options = {"远程模式", "待机等风", "停机维护", "启动"};
        stateBox->addItems(options);

        auto it1 = std::find(fans.standbyFans.begin(), fans.standbyFans.end(), fan.fanCode);
        auto it2 = std::find(fans.hutdownFans.begin(), fans.hutdownFans.end(), fan.fanCode);
        auto it3 = std::find(fans.runFans.begin(), fans.runFans.end(), fan.fanCode);
        if (it1 != fans.standbyFans.end()){
            stateBox->setCurrentIndex(1);
        }
        else if (it2 != fans.hutdownFans.end()){
            stateBox->setCurrentIndex(2);
        }
        else if (it3 != fans.runFans.end()){
            stateBox->setCurrentIndex(3);
        }
        else{
            stateBox->setCurrentIndex(0);
        }

        connect(stateBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [this](int index) {this->stateChanged(index);});
        verticalLayout->addWidget(stateBox);


        pictureLabel = new QLabel(detailPage);
        pictureLabel->setObjectName(QString::fromUtf8("pictureLabel"));
        pictureLabel->setAlignment(Qt::AlignCenter);

        verticalLayout->addWidget(pictureLabel);

        nameLabel = new QLabel(detailPage);
        nameLabel->setObjectName(QString::fromUtf8("nameLabel"));
        nameLabel->setAlignment(Qt::AlignCenter);

        verticalLayout->addWidget(nameLabel);

        verticalLayout->setStretch(0, 1);
        verticalLayout->setStretch(1, 9);
        verticalLayout->setStretch(1, 2);

        horizontalLayout_3->addLayout(verticalLayout);

        gridLayout = new QGridLayout();
        gridLayout->setSpacing(0);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        clearanceButton = new QPushButton(detailPage);
        clearanceButton->setObjectName(QString::fromUtf8("ClearanceButton"));

        gridLayout->addWidget(clearanceButton, 0, 1, 1, 1);

        flangeButton = new QPushButton(detailPage);
        flangeButton->setObjectName(QString::fromUtf8("FlangeButton"));

        gridLayout->addWidget(flangeButton, 0, 0, 1, 1);

        gyroscopeButton = new QPushButton(detailPage);
        gyroscopeButton->setObjectName(QString::fromUtf8("GyroscopeButton"));

        gridLayout->addWidget(gyroscopeButton, 0, 2, 1, 1);

        distanceLabel = new QLabel(detailPage);
        distanceLabel->setObjectName(QString::fromUtf8("distanceLabel"));

        gridLayout->addWidget(distanceLabel, 2, 0, 1, 3);

        angelLabel = new QLabel(detailPage);
        angelLabel->setObjectName(QString::fromUtf8("angelLabel"));

        gridLayout->addWidget(angelLabel, 1, 0, 1, 3);


        horizontalLayout_3->addLayout(gridLayout);

        stackedWidget->addWidget(detailPage);
        simplifyPage = new QWidget();
        simplifyPage->setObjectName(QString::fromUtf8("simplifyPage"));

        horizontalLayoutWidget = new QWidget(simplifyPage);
        horizontalLayoutWidget->setObjectName(QString::fromUtf8("horizontalLayoutWidget"));
        horizontalLayout_2 = new QHBoxLayout(horizontalLayoutWidget);
        horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
        horizontalLayout_2->setContentsMargins(0, 0, 0, 0);
        pictureLabel_2 = new QLabel(horizontalLayoutWidget);
        pictureLabel_2->setObjectName(QString::fromUtf8("pictureLabel_2"));
        pictureLabel_2->setAlignment(Qt::AlignCenter);

        horizontalLayout_2->addWidget(pictureLabel_2);

        statusLabel = new QLabel(horizontalLayoutWidget);
        statusLabel->setObjectName(QString::fromUtf8("statusLabel"));
        statusLabel->setAlignment(Qt::AlignCenter);

        statusLabel->setText(fan.fanCode + fans.states[fan.fanCode]);

        simplifyPage->setLayout(horizontalLayout_2);

        horizontalLayout_2->addWidget(statusLabel);

        stackedWidget->addWidget(simplifyPage);

        horizontalLayout->addWidget(stackedWidget);

        if (state == 1){
            stackedWidget->setCurrentIndex(1);
        }else{
            stackedWidget->setCurrentIndex(0);
        }

        nameLabel->setText(fan.fanCode);
        if (fan.angle != 0){
            angelLabel->setText("角度: " + QString::number(fan.angle));
        }else{
            angelLabel->setText("角度: 数据为空");
        }

        if (fan.distance != 0){
            distanceLabel->setText("距离: " + QString::number(fan.distance));
        }else{
            distanceLabel->setText("距离: 数据为空");
        }

        QMetaObject::connectSlotsByName(MonitorTag);
        if (fans.states[fan.fanCode] == QString("正常运行")){
            QPixmap pixmap(":/Static/fanNormal.png");
            pixmap = pixmap.scaled(100, 100, Qt::KeepAspectRatio);
            pictureLabel->setPixmap(pixmap);
            pictureLabel_2->setPixmap(pixmap);
            this->setStyleSheet("QWidget#MonitorTag{background-color: #374065;border-radius: 5px;border: 3px solid;border-color: #1AFA29;}");
        }else if (fans.states[fan.fanCode] == QString("通讯中断")){
            QPixmap pixmap(":/Static/fanfanInterrupted.png");
            pixmap = pixmap.scaled(100, 100, Qt::KeepAspectRatio);
            pictureLabel->setPixmap(pixmap);
            pictureLabel_2->setPixmap(pixmap);
            this->setStyleSheet("QWidget#MonitorTag{background-color: #374065;border-radius: 5px;border: 3px solid;border-color: white;}");
        }else if (fans.states[fan.fanCode] == QString("待机等风")){
            QPixmap pixmap(":/Static/fanStandby.png");
            pixmap = pixmap.scaled(100, 100, Qt::KeepAspectRatio);
            pictureLabel->setPixmap(pixmap);
            pictureLabel_2->setPixmap(pixmap);
            this->setStyleSheet("QWidget#MonitorTag{background-color: #374065;border-radius: 5px;border: 3px solid;border-color: #F4EA2A;}");
        }else if (fans.states[fan.fanCode] == QString("停机维护")){
            QPixmap pixmap(":/Static/fanHutdown.png");
            pixmap = pixmap.scaled(100, 100, Qt::KeepAspectRatio);
            pictureLabel->setPixmap(pixmap);
            pictureLabel_2->setPixmap(pixmap);
            this->setStyleSheet("QWidget#MonitorTag{background-color: #374065;border-radius: 5px;border: 3px solid;border-color: #C0397C;}");
        }else if (fans.states[fan.fanCode] == QString("故障停机")){
            QPixmap pixmap(":/Static/fanError.png");
            pixmap = pixmap.scaled(100, 100, Qt::KeepAspectRatio);
            pictureLabel->setPixmap(pixmap);
            pictureLabel_2->setPixmap(pixmap);
            this->setStyleSheet("QWidget#MonitorTag{background-color: #374065;border-radius: 5px;border: 3px solid;border-color: #d81e06;}");
        }else if (fans.states[fan.fanCode] == QString("启动")){
            QPixmap pixmap(":/Static/fanRun.png");
            pixmap = pixmap.scaled(100, 100, Qt::KeepAspectRatio);
            pictureLabel->setPixmap(pixmap);
            pictureLabel_2->setPixmap(pixmap);
            this->setStyleSheet("QWidget#MonitorTag{background-color: #374065;border-radius: 5px;border: 3px solid;border-color: #0caba2;}");
        }



        if (fan.headroomState == QString("正常")){
            clearanceButton->setIcon(QIcon(":/Static/headNormal.png"));
        }
        if (fan.headroomState == QString("故障")){
            clearanceButton->setIcon(QIcon(":/Static/headError.png"));
        }
        if (fan.flangeState == QString("正常")){
            flangeButton->setIcon(QIcon(":/Static/flangeNormal.png"));
        }
        if (fan.flangeState == QString("故障")){
            flangeButton->setIcon(QIcon(":/Static/flangeError.png"));
        }
        if (fan.gyroState == QString("正常")){
            gyroscopeButton->setIcon(QIcon(":/Static/gyroNormal.png"));
        }
        if (fan.gyroState == QString("故障")){
            gyroscopeButton->setIcon(QIcon(":/Static/gyroError.png"));
        }
    } // setupUi

    void stateChanged(int index){
//        "远程模式", "待机等风", "停机维护", "启动"

        switch (index){
        case 0:
            delNameFromList(fan.fanCode);
            break;
        case 1:
            delNameFromList(fan.fanCode);
            fans.standbyFans.push_back(fan.fanCode);
            fans.states[fan.fanCode] = QString("待机等风");
            break;
        case 2:
            delNameFromList(fan.fanCode);
            fans.hutdownFans.push_back(fan.fanCode);
            for (auto it = fans.runFans.begin(); it != fans.runFans.end(); ++it) {qDebug() << *it;}
            fans.states[fan.fanCode] = QString("停机维护");
            break;
        case 3:
            delNameFromList(fan.fanCode);
            fans.runFans.push_back(fan.fanCode);
            fans.states[fan.fanCode] = QString("启动");
            break;
        }
    }





    MonitorTag(FanClass fan, int state) : fan(fan), state(state)
    {
        setupUi(this);
    }



};


QT_END_NAMESPACE

#endif // MONITORTAGPWEJEQ_H
