#![feature(custom_test_frameworks)]
#![test_runner(my_runner)]
// use std::future::Future;

use colored::*;
use lazy_static::lazy_static;
use riven::RiotApi;
use tokio::runtime::current_thread::Runtime;


lazy_static! {
    static ref API_KEY: String = {
        let api_key = std::fs::read_to_string("apikey.txt").unwrap(); // TODO don't use unwrap.
        api_key.trim().to_owned()
    };
    static ref RIOT_API: RiotApi<'static> = {
        RiotApi::with_key(&API_KEY)
    };
}

fn my_runner(tests: &[()]) {
    std::process::exit({
        let mut rt = Runtime::new().expect("Failed to create runtime.");

        let result = rt.block_on(async {
            let a = async {
                let result = plus_one(&RIOT_API).await;
                print!("plus_one ... ");
                match &result {
                    Ok(_) => println!("{}", "ok".green()),
                    Err(msg) => println!("{}: {}", "error".red(), msg),
                };
                result
            };
            let b = async {
                let result = plus_two(&RIOT_API).await;
                print!("plus_two ... ");
                match &result {
                    Ok(_) => println!("{}", "ok".green()),
                    Err(msg) => println!("{}: {}", "error".bright_red(), msg),
                };
                result
            };
            let err = None;
            let err = err.or(a.await.err());
            let err = err.or(b.await.err());
            err.map_or(Ok(()), |e| Err(e))
        });
        match &result {
            Ok(_) => 0,
            Err(_) => 2,
        }
    });
}

macro_rules! rassert {
    ( $x:expr ) => {
        {
            if $x { Ok(()) } else { Err(stringify!($x)) }?
        }
    };
}
macro_rules! rassert_eq {
    ( $a:expr, $b:expr ) => { rassert!($a == $b) };
}

async fn plus_one(riot_api: &'static RiotApi<'static>) -> Result<(), String> {
    rassert_eq!("world", "world");
    Ok(())
}

async fn plus_two(riot_api: &'static RiotApi<'static>) -> Result<(), String> {
    rassert_eq!("hello", "world");
    Ok(())
}