#[macro_use]
extern crate json_config;
#[macro_use]
extern crate serde_json;

use json_config::ConfigurationBuilder;
use json_config::ConfigurationSource;
use json_config::ConfigurationDefinitionParams;

fn main(){
    
    let base_config_str = r#"
    {
        "appName": "json_config Demo",
        "appVersion": "1.0",
        "database": {
            "host": "dev.database.com",
            "port": 3000
        }
    }"#;

    let mut builder = config!(vec![        
        from_str!(base_config_str),
        from_file!("config/translations.json"),
        from_file!("config/keystore.json"),

        bundle!("QA", vec![
            from_json!({
                "database": {
                    "host": "qa.database.com",
                    "port": 3001
                }
            }),
            from_file!("config/keystore_qa.json")  
        ]),

        bundle!("PROD",vec![
            from_json!({
                "database": {
                    "host": "prod.database.com",
                    "port": 3002
                }
            }),
            from_file!("config/keystore_prod.json") 
        ])
    ]);
   
    builder.merge_bundle(&"PROD");

    //emulates retrieving a partial configuration via 
    //a remote API (i.e. REST) in json form
    let remote_config: String = get_remote_config("fr");
    builder.merge_source(&ConfigurationSource::StringContent(remote_config));
    
    println!("{}", builder.to_string_pretty());

    //emulates retrieving a partial configuration via 
    //a remote API (i.e. REST) in json form
    let remote_config: String = get_remote_config("fr");
    
    let builder = config!(vec![
        from_compiled!("json_config.json"),
        from_str!(remote_config)
    ]);

    println!("{}", builder.to_string_pretty());
}

fn get_remote_config(_lang: &str) -> String{
    return String::from(r#"{ 
        "translations": { 
            "T001": "Bienvenue",
            "T002": "Je vous remercie",
            "T003": "Bonne journée"
        }
    }"#);
}