#include "app.hpp"
#include <iostream>
#include <string>

/**
 * @brief 主程序入口点。
 *
 * 解析命令行参数，执行对应操作。
 * 支持 --help、--version、--demo 三个命令行选项。
 *
 * @param argc 参数个数
 * @param argv 参数数组
 * @return int 0 表示正常退出，非 0 表示错误
 */
int main(int argc, char* argv[]) {
    if (argc == 1) {
        bntvs::printHelp();
        return 0;
    }

    std::string arg = argv[1];

    if (arg == "--help" || arg == "-h") {
        bntvs::printHelp();
    } else if (arg == "--version" || arg == "-v") {
        std::cout << bntvs::getVersion() << std::endl;
    } else if (arg == "--demo") {
        std::cout << "\n========================================\n";
        std::cout << "  BNTVS_SW 功能演示\n";
        std::cout << "========================================\n\n";

        // SU-COM: 基础通用通信
        std::cout << "--- SU-COM: 基础通用通信 ---\n";
        bntvs::initEthernetComm("192.168.1.100", 8080);
        bntvs::initRS422Comm("COM1", 115200);
        bntvs::initCANComm("can0", 250000);
        bntvs::initAnalogComm(0);
        bntvs::initDigitalComm(1);

        // SU-PROT: 协议兼容
        std::cout << "\n--- SU-PROT: 协议兼容 ---\n";
        std::vector<uint8_t> canfd_data(64, 0x55);
        auto proto = bntvs::detectCANProtocol(canfd_data);
        std::cout << "检测到协议: " << (proto == bntvs::CANProtocolType::CANFD ? "CAN FD" : "CAN 2.0") << std::endl;
        auto converted = bntvs::convertCANFrame(proto, bntvs::CANProtocolType::CAN2_0, canfd_data);
        std::cout << "CAN FD → CAN 2.0 转换后长度: " << converted.size() << " 字节\n";

        // SU-STOR: 数据存储
        std::cout << "\n--- SU-STOR: 数据存储 ---\n";
        bntvs::startStorageService();
        bntvs::BusDataRecord record;
        record.record_id = 1;
        record.bus_type = "CAN";
        record.timestamp = std::chrono::system_clock::now();
        record.frame_id = 0x123;
        record.data_content = {0x01, 0x02, 0x03, 0x04};
        record.data_length = 4;
        record.protocol_type = "CAN2.0";
        record.node_id = "NODE_A";
        bntvs::storeBusData(record);

        // SU-CONV: 信号转换
        std::cout << "\n--- SU-CONV: 信号转换 ---\n";
        auto analog_frame = bntvs::convertAnalogToBus(3.5, 0x200);
        auto digital_frame = bntvs::convertDigitalToBus(5, 0x201);

        // SU-ROUTE: 总线路由
        std::cout << "\n--- SU-ROUTE: 总线路由 ---\n";
        bntvs::RouteEntry route;
        route.dest_network  = "192.168.2.0";
        route.netmask       = "255.255.255.0";
        route.next_hop      = "192.168.1.1";
        route.out_interface = "eth0";
        route.priority      = 10;
        bntvs::addStaticRoute(route);
        bntvs::routeDataFrame(record);
        auto can_converted = bntvs::convertEthernetToCAN(record);

        // SU-ANALY: 总线分析
        std::cout << "\n--- SU-ANALY: 总线分析 ---\n";
        bntvs::startBusMonitoring(bntvs::BusType::CAN,
            [](const bntvs::BusDataRecord& r) {
                std::cout << "  收到帧: ID=0x" << std::hex << r.frame_id << std::dec << std::endl;
            });
        bntvs::getNetworkStatus(bntvs::BusType::CAN);
        auto fault = bntvs::diagnoseBusFault(record);
        std::cout << "故障诊断结果: \"" << fault << "\"\n";
        bntvs::stopBusMonitoring();

        // 波形分析
        std::cout << "\n--- SU-ANALY: 波形分析 ---\n";
        std::vector<double> sine_wave;
        for (int i = 0; i < 100; ++i) {
            sine_wave.push_back(std::sin(2.0 * 3.14159 * 10.0 * i / 100.0));
        }
        auto waveform = bntvs::analyzeWaveform(sine_wave, 1000.0);
        std::cout << "波形分析: " << waveform.analysis_summary << std::endl;

        // SU-FAULT: 故障注入
        std::cout << "\n--- SU-FAULT: 故障注入 ---\n";
        bntvs::injectShortCircuit(bntvs::BusType::CAN, 100);
        bntvs::injectEMI(bntvs::BusType::ETHERNET, 0.5, 200);
        bntvs::injectErrorFrame(bntvs::BusType::CAN, "CRC错误");
        bntvs::injectNodeOffline("NODE_B", 500);
        bntvs::injectBaudRateAnomaly(bntvs::BusType::CAN, 50000, 300);

        // SU-TEST: 总线测试
        std::cout << "\n--- SU-TEST: 总线测试 ---\n";
        auto short_result = bntvs::testShortBusPerformance(bntvs::BusType::CAN, 10);
        auto long_result  = bntvs::testLongBusPerformance(bntvs::BusType::CAN, 10);
        auto stress_result = bntvs::testLongDurationStability(bntvs::BusType::CAN, 24);

        // SU-REPLAY: 数据回放
        std::cout << "\n--- SU-REPLAY: 数据回放 ---\n";
        auto results = bntvs::queryBusData("", "2024-01-01", "2024-12-31");
        bntvs::replayBusData({record}, 1.0, nullptr);

        std::cout << "\n========================================\n";
        std::cout << "  演示结束\n";
        std::cout << "========================================\n";
    } else {
        std::cerr << "未知选项: " << arg << "\n";
        bntvs::printHelp();
        return 1;
    }

    return 0;
}
