use anyhow::Result;
use gmqtt_client::{Message, MqttClientBuilder, QoS};
use tokio::runtime;
use tracing::info;

const MQTT_BROKER: &str = "tcp://localhost:1883";

fn main() -> Result<()> {
    let filter = tracing_subscriber::EnvFilter::from_default_env();
    tracing_subscriber::fmt().with_env_filter(filter).init();

    let (_, mqtt_worker) = MqttClientBuilder::new(MQTT_BROKER)
        .on_connected_callback(on_mqtt_connected)
        .on_message_callback(on_mqtt_message)
        .subscribe("client/example/pubsub", QoS::AtLeastOnce)
        .build()?;

    let worker_runtime = runtime::Builder::new_multi_thread()
        .enable_all()
        .worker_threads(1)
        .build()?;

    worker_runtime.spawn(async { mqtt_worker.run().await });

    std::thread::park();
    Ok(())
}

fn on_mqtt_connected() {
    info!("MQTT connected");
}

fn on_mqtt_message(message: &Message) {
    println!(
        "MQTT message received: {}",
        String::from_utf8_lossy(message.payload())
    );
}
