#include "SimMsg.h"
#include "test.h"

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

// 定义全局变量
TotalData g_totalData;
int dataReceivedCount = 0;  // 改名为避免冲突

void testWriteLog(const std::string &msg)
{
    std::cout << msg << std::endl;
}

string topic_name_cmd = "Command";
string topic_name_tlm = "Telemetry";

SimMsg* test_part = nullptr;
string servername = "test_Service";

// 实现TotalData的打印函数
void TotalData::printAllData() const {
    cout << "\n==================================================" << endl;
    cout << "              总结构体数据汇总" << endl;
    cout << "==================================================" << endl;
    
    int totalBytes = 0;
    int receivedCount = 0;
    
    // 打印每个子结构体的数据
    auto printSubData = [&](const std::string& name, const std::vector<uint8_t>& data, int index) {
        if (!data.empty() && received[index]) {
            receivedCount++;
            totalBytes += data.size();
            cout << name << " [" << data.size() << "字节]: ";
            
            for (size_t i = 0; i < data.size(); ++i) {
                if (i > 0) cout << " ";
                cout << hex << uppercase << setw(2) << setfill('0') << (int)data[i];
                
                // 每16字节换行
                if ((i + 1) % 16 == 0 && i < data.size() - 1) {
                    cout << endl << "                 ";
                }
            }
            cout << dec << endl;
        } else {
            cout << name << ": 未收到" << endl;
        }
    };
    
    printSubData("Test1数据", test1.data, 0);
    printSubData("Test2数据", test2.data, 1);
    printSubData("Test3数据", test3.data, 2);
    printSubData("Test4数据", test4.data, 3);
    printSubData("Test5数据", test5.data, 4);
    printSubData("Test6数据", test6.data, 5);
    printSubData("Test7数据", test7.data, 6);
    printSubData("Test8数据", test8.data, 7);
    printSubData("Test9数据", test9.data, 8);
    printSubData("Test10数据", test10.data, 9);
    
    cout << "==================================================\n" << endl;
}

//初始化
void test_init(uint8_t domainid, std::string appname)
{
    std::vector<std::string> parameters;
    string  expression = "dest = '"+ servername + "'";
    if (nullptr == test_part)
    {
        test_part=new SimMsg(domainid, 3000, appname, testWriteLog);
        test_part->create_pub(topic_name_cmd);
        test_part->create_pub(topic_name_tlm);

        test_part->create_sub(topic_name_cmd, command_callback, expression, parameters);
        test_part->create_sub(topic_name_tlm, telemetry_callback, expression, parameters);
    }
}

// 遥控接收回调函数
void command_callback(std::string src, std::string dest, std::string type,
                      std::string reserve1, std::string reserve2,
                      std::vector<uint8_t>& data) {
    
    // 存储数据到对应的子结构体
    if (src == "test1_Service") {
        g_totalData.test1.data = data;
        g_totalData.received[0] = true;
    } else if (src == "test2_Service") {
        g_totalData.test2.data = data;
        g_totalData.received[1] = true;
    } else if (src == "test3_Service") {
        g_totalData.test3.data = data;
        g_totalData.received[2] = true;
    } else if (src == "test4_Service") {
        g_totalData.test4.data = data;
        g_totalData.received[3] = true;
    } else if (src == "test5_Service") {
        g_totalData.test5.data = data;
        g_totalData.received[4] = true;
    } else if (src == "test6_Service") {
        g_totalData.test6.data = data;
        g_totalData.received[5] = true;
    } else if (src == "test7_Service") {
        g_totalData.test7.data = data;
        g_totalData.received[6] = true;
    } else if (src == "test8_Service") {
        g_totalData.test8.data = data;
        g_totalData.received[7] = true;
    } else if (src == "test9_Service") {
        g_totalData.test9.data = data;
        g_totalData.received[8] = true;
    } else if (src == "test10_Service") {
        g_totalData.test10.data = data;
        g_totalData.received[9] = true;
    } else {
        return;  // 未知来源，直接返回
    }
    
    dataReceivedCount++;
    
    // 如果所有数据都已收到，则打印总结构体数据
    if (g_totalData.allReceived()) {
        g_totalData.printAllData();
        g_totalData.reset();
    }
}

// 遥测接收回调函数
void telemetry_callback(std::string src, std::string dest, std::string type,
                      std::string reserve1, std::string reserve2,
                      std::vector<uint8_t>& data) {
    // 简单打印遥测数据
    std::cout << "[遥测数据] 来自 " << src << " 到 " << dest 
              << " (" << data.size() << "字节)" << std::endl;
}

// AD硬件服务化遥控发布
void test_command_Pub(uint8_t* data, string dest, uint16_t len)
{
    test_part->publish(topic_name_cmd, "test_Service", dest, "command", data, len);
}

// AD硬件服务化遥测发布
void test_telemetry_Pub(uint8_t* data, string dest, uint16_t len)
{
    test_part->publish(topic_name_tlm, "test_Service", dest, "telemetry", data, len);
}