#include "test.h"
#include <iostream>
#include <cstring>
#include <chrono>
#include <thread>
#include <fstream>
#include <iomanip>
#include <ctime>

// 添加全局变量
std::ofstream logFile;
int totalTests = 0;
int failedTests = 0;
int successTests = 0;
uint64_t totalCheckSum = 0;
bool testRunning = true;
const int MAX_TESTS = 10000;

// 初始化日志文件
void initLogFile() {
    // 生成带时间戳的日志文件名
    std::time_t now = std::time(nullptr);
    std::tm* timeinfo = std::localtime(&now);
    char buffer[80];
    std::strftime(buffer, sizeof(buffer), "test_log_%Y%m%d_%H%M%S.bin", timeinfo);
    
    logFile.open(buffer, std::ios::binary | std::ios::out);
    if (!logFile.is_open()) {
        std::cerr << "无法打开日志文件: " << buffer << std::endl;
        exit(1);
    }
    std::cout << "日志文件已创建: " << buffer << std::endl;
}

// 保存数据到日志文件
void saveToLogFile(const std::string& src, const std::vector<uint8_t>& data) {
    if (!logFile.is_open()) return;
    
    // 写入时间戳
    std::time_t timestamp = std::time(nullptr);
    logFile.write(reinterpret_cast<const char*>(&timestamp), sizeof(timestamp));
    
    // 写入数据来源长度和来源
    uint16_t srcLen = src.length();
    logFile.write(reinterpret_cast<const char*>(&srcLen), sizeof(srcLen));
    logFile.write(src.c_str(), srcLen);
    
    // 写入数据长度和数据
    uint32_t dataLen = data.size();
    logFile.write(reinterpret_cast<const char*>(&dataLen), sizeof(dataLen));
    logFile.write(reinterpret_cast<const char*>(data.data()), data.size());
    
    // 刷新缓冲区确保数据写入
    logFile.flush();
}

// 计算校验和
uint8_t calculateChecksum(const std::vector<uint8_t>& data) {
    uint8_t checksum = 0;
    for (size_t i = 0; i < data.size() - 1; i++) {
        checksum += data[i];
    }
    return checksum;
}

int main() {
    // 初始化日志文件
    initLogFile();
    
    // 初始化AD硬件服务化
    test_init(0, "AD_Hardware_Service");

    uint8_t test_data[] = {0x55, 0xAA, 0x55, 0xAA};
    uint16_t data_len = sizeof(test_data);
    
    std::string dest = "test_Service";
    
    auto startTime = std::chrono::steady_clock::now();
    
    while (totalTests < MAX_TESTS && testRunning) {
        totalTests++;
        
        // 重置总校验和
        totalCheckSum = 0;
        
        std::cout << "\n第 " << totalTests << " 次测试 (" 
                  << successTests << " 成功/" << failedTests << " 失败)..." << std::endl;

        // 发送遥控数据
        test_command_Pub(test_data, dest, data_len);
        
        // 等待10ms
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
        
        // 打印本次测试的总校验和
        std::cout << "总校验和: " << totalCheckSum 
                  << " (0x" << std::hex << std::uppercase << totalCheckSum 
                  << std::dec << ")" << std::endl;
    }
    
    auto endTime = std::chrono::steady_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
    
    std::cout << "\n========================================" << std::endl;
    std::cout << "测试完成！" << std::endl;
    std::cout << "总测试次数: " << totalTests << std::endl;
    std::cout << "成功次数: " << successTests << std::endl;
    std::cout << "失败次数: " << failedTests << std::endl;
    std::cout << "总耗时: " << duration.count() << "ms" << std::endl;
    std::cout << "平均周期: " << (duration.count() / (double)totalTests) << "ms" << std::endl;
    
    if (logFile.is_open()) {
        logFile.close();
        std::cout << "日志文件已保存" << std::endl;
    }
    
    return 0;
}