use ::log::{debug, error, info, trace, warn};
use encoding_rs::Encoding;
use tokio::runtime::Runtime;
use crate::utils::cxx_string_to_string;

mod log;
mod network;
mod config;
pub mod utils;

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        fn download_file(url: &CxxString, path: &CxxString) -> Result<()>;

        fn http_get(url: &CxxString) -> Result<String>;

        fn init_log(is_debug: bool);

        fn log_trace(msg: &CxxString);

        fn log_debug(msg: &CxxString);

        fn log_info(msg: &CxxString);

        fn log_warning(msg: &CxxString);

        fn log_error(msg: &CxxString);
    }
}

fn log_error(msg: &cxx::CxxString) {
    let msg = cxx_string_to_string(msg);
    error!("{}", msg);
}

fn log_warning(msg: &cxx::CxxString) {
    let msg = cxx_string_to_string(msg);
    warn!("{}", msg);
}

fn log_info(msg: &cxx::CxxString) {
    let msg = cxx_string_to_string(msg);
    info!("{}", msg);
}

fn log_debug(msg: &cxx::CxxString) {
    let msg = cxx_string_to_string(msg);
    debug!("{}", msg);
}

fn log_trace(msg: &cxx::CxxString) {
    let msg = cxx_string_to_string(msg);
    trace!("{}", msg);
}

fn init_log(is_debug: bool) {
    log::init_log(is_debug);
}

fn get_runtime() -> Runtime {
    Runtime::new().expect("创建Tokio运行时失败")
}

fn download_file(
    url: &cxx::CxxString,
    path: &cxx::CxxString,
) -> Result<(), Box<dyn std::error::Error>> {
    let rt = get_runtime();
    let url = cxx_string_to_string(url);
    let path = cxx_string_to_string(path);
    rt.block_on(network::download_file(&url, &path))?;
    Ok(())
}

fn http_get(url: &cxx::CxxString) -> Result<String, Box<dyn std::error::Error>> {
    let rt = get_runtime();
    let url = cxx_string_to_string(url);
    rt.block_on(network::http_get(&url))
}


