

use std::collections::HashMap;
use colored::*;

///
///     #Example
///     
/// 
///     let Unconditionals = vec!["--Utestflag1","--Utestflag2"];
///     let Conditionals = vec!["-utestflag3"];
///     let args = parse(Conditionals, Unconditionals).expect("Arguments not properly formatted"); //Will return an error if unconditional arguments not given or input arguments not properly formatted.
///     let conds = args.0; // hashmap of conditionals as keys and data as values
///     let unconds = args.1; // same for unconditionals
/// 


pub fn parse<'a>(conditionals:Vec::<&str>,unconditionals:Vec::<&str>) -> Result<(HashMap<String,String>,HashMap<String,String>),String>{
    use std::env;
    let mut conditionals_map: HashMap<String,String> = HashMap::with_capacity(conditionals.len());
    let mut unconditionals_map: HashMap<String,String> = HashMap::with_capacity(unconditionals.len());

    for i in conditionals {
        conditionals_map.insert(i.to_string(),"".to_string());
    }
    for i in &unconditionals {
        unconditionals_map.insert(i.to_string(),"".to_string());
    }

    let args = env::args().collect::<Vec<String>>();

    for (n,i) in args.iter().enumerate() {
        if conditionals_map.contains_key(i) {
            if args.len() < n + 1 {
                println!("args not properly formated");
                return Err(String::from("Results not properly formatted"));
            }
            conditionals_map.insert(i.to_string(),args[n+1].clone());

        }
    }

    for (n,i) in args.iter().enumerate() {
        if unconditionals_map.contains_key(i) {
            if args.len() < n + 1 {
                println!("args not properly formated");
                return Err(String::from("Results not properly formatted"));
            }
           unconditionals_map.insert(i.to_string(),args[n+1].clone());

        }

    }
    
    for i in &unconditionals {
        if unconditionals_map.get_key_value(*i).unwrap().1 == ""{
            let error = format!("Mandatory flag {} not included",i.blue());
            print!("\n \n");
            print!("{}","mandatory flags ".red());
            for i in unconditionals {
                print!("{} ",i.green());
            }
            print!("{}"," were not included \n \n".red());
            return Err(error);
        }
    }

    

    let to_return = Ok((conditionals_map,unconditionals_map));
    return to_return;
}

