use bgpkit_parser::parse_ris_live_message;
use serde_json::json;
use tungstenite::{connect, Message};
use url::Url;

const RIS_LIVE_URL: &str = "ws://ris-live.ripe.net/v1/ws/?client=rust-bgpkit-parser";

/// This is an example of subscribing to RIS-Live's streaming data from one host (`rrc21`).
///
/// For more RIS-Live details, check out their documentation at https://ris-live.ripe.net/manual/
fn main() {
    // connect to RIPE RIS Live websocket server
    let (mut socket, _response) = connect(Url::parse(RIS_LIVE_URL).unwrap())
        .expect("Can't connect to RIS Live websocket server");

    // subscribe to messages from one collector
    // let msg = json!({"type": "ris_subscribe", "data": {"host": "rrc21"}}).to_string();
    let msg = json!({"type": "ris_subscribe", "data": null}).to_string();
    socket.send(Message::Text(msg)).unwrap();

    loop {
        let msg = socket.read().expect("Error reading message").to_string();
        if msg.is_empty() {
            continue;
        }
        match parse_ris_live_message(msg.as_str()) {
            Ok(elems) => {
                for elem in elems {
                    println!("{}", elem);
                }
            }
            Err(error) => {
                println!("{:?}", error);
            }
        }
    }
}
