#include "LedMqttSubscriber.h"
#include "toml.hpp"
#include <iostream>
#include <random>
#include <chrono>
#include <sstream>

// 生成随机clientId辅助函数
std::string generateRandomClientId(const std::string &prefix = "client_")
{
    // 用时间戳+随机数生成一个简单的唯一字符串
    auto now = std::chrono::system_clock::now().time_since_epoch().count();

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(1000, 9999);

    std::stringstream ss;
    ss << prefix << now << "_" << dis(gen);
    return ss.str();
}

MqttClient::MqttClient(const std::string &configPath)
{
    try
    {
        auto data = toml::parse(configPath);
        auto mqttConfig = toml::find(data, "mqtt");

        broker = toml::find<std::string>(mqttConfig, "host");
        clientId = generateRandomClientId();
        username = toml::find_or<std::string>(mqttConfig, "username", "");
        password = toml::find_or<std::string>(mqttConfig, "password", "");
        topic = toml::find<std::string>(mqttConfig, "topic");
        qos = toml::find_or<int>(mqttConfig, "qos", 1);

        client = new mqtt::async_client(broker, clientId);
        connOpts.set_clean_session(true);
        if (!username.empty())
            connOpts.set_user_name(username);
        if (!password.empty())
            connOpts.set_password(password);

        client->set_callback(callback);
    }
    catch (const std::exception &e)
    {
        std::cerr << "加载 MQTT 配置失败: " << e.what() << std::endl;
    }
}

bool MqttClient::start()
{
    try
    {
        client->connect(connOpts)->wait();
        client->subscribe(topic, qos)->wait();
        std::cout << "已订阅主题：" << topic << std::endl;
        return true;
    }
    catch (const mqtt::exception &e)
    {
        std::cerr << "MQTT 启动失败: " << e.what() << std::endl;
        return false;
    }
}

void MqttClient::Callback::message_arrived(mqtt::const_message_ptr msg)
{
    std::cout << "接收到消息: " << msg->to_string() << std::endl;
}
