#define CPPHTTPLIB_OPENSSL_SUPPORT

#include <QString>
#include <codecvt>
#include <string>
#include <iostream>
#include <httplib.h>
#include <json.hpp>
#include "client.h"
#include <QDebug>
#include <QTextCodec>
using json = nlohmann::json;
using namespace std;

HTTPClient httpClient;
FanManage fans;

void delNameFromList(QString element){
    fans.interruptedFans.erase(std::remove(fans.interruptedFans.begin(), fans.interruptedFans.end(), element), fans.interruptedFans.end());
    fans.standbyFans.erase(std::remove(fans.standbyFans.begin(), fans.standbyFans.end(), element), fans.standbyFans.end());
    fans.normalFans.erase(std::remove(fans.normalFans.begin(), fans.normalFans.end(), element), fans.normalFans.end());
    fans.failureFans.erase(std::remove(fans.failureFans.begin(), fans.failureFans.end(), element), fans.failureFans.end());
    fans.hutdownFans.erase(std::remove(fans.hutdownFans.begin(), fans.hutdownFans.end(), element), fans.hutdownFans.end());
    fans.runFans.erase(std::remove(fans.runFans.begin(), fans.runFans.end(), element), fans.runFans.end());
}


bool isInLists(QString element) {
    // 使用 std::find_first_of 函数查找元素是否在列表中
    auto it1 = std::find_first_of(fans.standbyFans.begin(), fans.standbyFans.end(), &element, &element + 1);
    auto it2 = std::find_first_of(fans.hutdownFans.begin(), fans.hutdownFans.end(), &element, &element + 1);
    auto it3 = std::find_first_of(fans.runFans.begin(), fans.runFans.end(), &element, &element + 1);

    // 如果元素在任意一个列表中，则返回 true，否则返回 false
    return it1 != fans.standbyFans.end() || it2 != fans.hutdownFans.end() || it3 != fans.runFans.end();
}

 HTTPClient::HTTPClient() : client("https://121.36.33.250:9000") {
    header = {{"GroupId", "dam"}};
    loginIndex = "/services/application/api/fan_app/login";
    fanListIndex = "/services/application/api/fan_app/fanList";
    headroomDetailIndex = "/services/application/api/fan_app/headroomDetail";
    gyroDetailIndex = "/services/application/api/fan_app/gyroDetail";
    featureHistoryIndex = "/services/application/api/fan_app/featureHistory";
    client.enable_server_certificate_verification(false); // 取消SSL验证
}

 void HTTPClient::getFanList(){
    if (auto res = client.Post(fanListIndex, header)) {
        json resJson = json::parse(res->body);
        for (const auto& data : resJson["data"]) {
            FanClass fan;
            fan.fanCode = QString::fromStdString(data["fanCode"]);
            if (data["functions"]["gyro"]["features"][0]["featureValue"].is_number()) {
                fan.angle = data["functions"]["gyro"]["features"][0]["featureValue"].get<double>();
            }
            if (data["functions"]["headRoom"]["features"][0]["featureValue"].is_number()){
            fan.distance = data["functions"]["headRoom"]["features"][0]["featureValue"].get<double>();
            }
            fan.gyroState = QString::fromStdString(data["functions"]["flange"]["state"]);
            fan.flangeState = QString::fromStdString(data["functions"]["gyro"]["state"]);
            fan.headroomState = QString::fromStdString(data["functions"]["headRoom"]["state"]);
            fans.allFans[fan.fanCode] = fan;
            if (isInLists(fan.fanCode)){
                return;
            }
            delNameFromList(fan.fanCode);
            if (fan.gyroState == QString("正常") && fan.flangeState == QString("正常") && fan.headroomState == QString("正常")){
                fans.normalFans.push_back(fan.fanCode);
                fans.states[fan.fanCode] = QString("正常运行");
            }
            if (fan.gyroState == QString("故障") || fan.flangeState == QString("故障") || fan.headroomState == QString("故障")){
                fans.failureFans.push_back(fan.fanCode);
                fans.states[fan.fanCode] = QString("故障停机");
            }
            if (fan.gyroState == QString("通讯中断") || fan.flangeState == QString("通讯中断") || fan.headroomState == QString("通讯中断")){
                fans.interruptedFans.push_back(fan.fanCode);
                fans.states[fan.fanCode] = QString("通讯中断");
            }
//            std::cout << fan.distance << std::endl;

        }
    }
 }

 QString HTTPClient::getFlangeList(int fanId) {
     json bodyJson = {{"fanId",fanId}};
     std::string body = bodyJson.dump();
     if (auto res = client.Post(headroomDetailIndex, header, body, "application/json")) {
         QString body = QString::fromStdString(res->body);
         return body;
     }
     else {
         return QString("error");
     }
 }

 QString HTTPClient::getHeadroomList(int fanId) {
     json bodyJson = { {"fanId",fanId} };
     std::string body = bodyJson.dump();
     if (auto res = client.Post(headroomDetailIndex, header, body, "application/json")) {
         QString body = QString::fromStdString(res->body);
         return body;
     }
     else {
         return QString("error");
     }
 }

 QString HTTPClient::getGyroList(int fanId) {
     json bodyJson = { {"fanId",fanId} };
     std::string body = bodyJson.dump();
     if (auto res = client.Post(gyroDetailIndex, header, body, "application/json")) {
         QString body = QString::fromStdString(res->body);
         return body;
     }
     else {
         return QString("error");
     }
 }

 QString HTTPClient::getHistoryList(int fanId) {
     if (auto res = client.Post(gyroDetailIndex, header)) {
         QString body = QString::fromStdString(res->body);
         return body;
     }
     else {
         return QString("error");
     }
 }



