extern crate log;
use clipboard_sync::{ClipboardSync, Character};
// use std::net::IpAddr;
// use trust_dns_resolver::{
//     config::{ResolverConfig, ResolverOpts},
//     Resolver,
// };
use tokio::sync::Mutex;
use std::sync::Arc;
use clap::{App, Arg};


#[cfg(feature = "ekko_simulated")]
const SERVER:&str = "192.168.0.116:5000";
#[cfg(not(feature = "ekko_simulated"))]
const SERVER:&str = "172.19.36.79";

fn param_parser() -> (Character, String) {
    let mut character = Character::Consumer;
    let matches = App::new("sstar_clipboard_sync")
        .version("1.0")
        .author("Ekko.bao")
        .about("sstar clipboard sync")
        .arg(
            Arg::with_name("producer")
                .short('p')
                .long("producer")
                .conflicts_with("consumer")
                .help("Data producer")
        )
        .arg(
            Arg::with_name("consumer")
                .short('c')
                .long("consumer")
                .conflicts_with("producer")
                .help("Data consumer")
        )
        .group(clap::ArgGroup::new("Character")
            .args(&["consumer", "producer"])
            .required(true)
            .multiple(false))
        .arg(
            Arg::with_name("info")
                .short('i')
                .long("info")
                .value_name("info")
                .default_value("info.json")
                .help("The file of running info")
        )
        .get_matches();
    character = if matches.is_present("producer") {
        Character::Producer
    } else if matches.is_present("consumer") {
        Character::Consumer
    } else {
        character
    };
    let info_file = matches.value_of("info").unwrap();
    (character, info_file.to_string())
}

async fn start(info: &str, c:&Character) {
    let mut ins: ClipboardSync = match ClipboardSync::new(SERVER, &info, &c)  {
        Ok(ins) => ins,
        Err(e) => {
            log::error!("create clipboard sync instance fail: {}", e);
            return;
        }
    };
    match ins.login().await {
        Ok(()) => log::info!("login success"),
        Err(e) => {
            log::error!("login fail: {}", e);
            return;
        }
    }
    let ctx = Arc::new(Mutex::new(ins));
    let handle = clipboard_sync::start_msg_sync(ctx.clone()).await.unwrap();
    let _ = handle.await;
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    env_logger::init();
    // // 创建自定义 DNS 解析器
    // let resolver = Resolver::new(ResolverConfig::default(), ResolverOpts::default()).unwrap();

    // // 要解析的域名
    // let response = resolver.lookup_ip("www.baidu.com");
    // match response {
    //     Ok(response) => {
    //         for ip in response {
    //             match ip {
    //                 IpAddr::V4(v4) => println!("IPv4: {}", v4),
    //                 IpAddr::V6(v6) => println!("IPv6: {}", v6),
    //             }
    //         }
    //     }
    //     Err(e) => {
    //         println!("Error: {}", e);
    //     }
    // }
    let (character, info) = param_parser();
    start(&info, &character).await;
    Ok(())
}
