use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;

fn main() {
    let out_dir = env::var("OUT_DIR").expect("Failed to get OUT_DIR env var");

    // -------------------------------------------------------------------------------------
    // setup feedly API secrets
    // -------------------------------------------------------------------------------------
    let feedly_client_id = match env::var("FEEDLY_CLIENT_ID") {
        Ok(client_id) => client_id,
        Err(_) => {
            println!("FEEDLY_CLIENT_ID not defined");
            "FEEDLY_CLIENT_ID_FALLBACK".to_owned()
        }
    };

    let feedly_client_secret = match env::var("FEEDLY_CLIENT_SECRET") {
        Ok(client_secret) => client_secret,
        Err(_) => {
            println!("FEEDLY_CLIENT_SECRET not defined");
            "FEEDLY_CLIENT_SECRET_FALLBACK".to_owned()
        }
    };

    let feedly_dest_path = format!("{}/feedly_secrets.rs", out_dir);
    let feedly_dest_path = Path::new(&feedly_dest_path);

    let mut f = File::create(&feedly_dest_path).expect("Cant create feedly secrets file");

    let feedly_secrets = format!(r#"
    #[derive(Debug)]
    pub struct FeedlySecrets {{
        pub client_id: String,
        pub client_secret: String,
    }}

    impl FeedlySecrets {{
        pub fn new() -> Self {{
            FeedlySecrets {{
                client_id: obfstr::obfstr!("{}").into(),
                client_secret: obfstr::obfstr!("{}").into(),
            }}
        }}
    }}
    "#, feedly_client_id, feedly_client_secret);
    f.write_all(&feedly_secrets.as_bytes()).expect("failed to write feedly secrets.rs");


    // -------------------------------------------------------------------------------------
    // setup password encryption key
    // -------------------------------------------------------------------------------------
    let password_crypt_key = match env::var("PASSWORD_CRYPT_KEY") {
        Ok(pass_crypt_key) => pass_crypt_key,
        Err(_) => {
            println!("PASSWORD_CRYPT_KEY not defined");
            "PASSWORD_CRYPT_KEY_FALLBACK".to_owned()
        }
    };

    let password_dest_path = format!("{}/password_crypt_key.rs", out_dir);
    let password_dest_path = Path::new(&password_dest_path);

    let resource_folder = password_dest_path.parent().expect("Can't extract parent folder from path");

    std::fs::create_dir_all(resource_folder).expect("couldn't create resource folder");

    let mut f = File::create(&password_dest_path).expect("Cant create password crypt file");

    let password_secrets = format!(r#"
    #[derive(Debug)]
    pub struct PasswordCryptKey;

    impl PasswordCryptKey {{
        pub fn get() -> String {{
            obfstr::obfstr!("{}").into()
        }}
    }}
    "#, password_crypt_key);
    f.write_all(&password_secrets.as_bytes())
        .expect("failed to write password_crypt_key.rs");

    
    // -------------------------------------------------------------------------------------
    // setup mercury parser secrets
    // -------------------------------------------------------------------------------------
    let mercury_user = match env::var("MERCURY_PARSER_USER") {
        Ok(mercury_user) => mercury_user,
        Err(_) => {
            println!("MERCURY_PARSER_USER not defined");
            "MERCURY_PARSER_USER_FALLBACK".to_owned()
        }
    };

    let mercury_key = match env::var("MERCURY_PARSER_KEY") {
        Ok(mercury_key) => mercury_key,
        Err(_) => {
            println!("MERCURY_PARSER_KEY not defined");
            "MERCURY_PARSER_KEY_FALLBACK".to_owned()
        }
    };

    let mercury_dest_path = format!("{}/mercury_parser_key.rs", out_dir);
    let mercury_dest_path = Path::new(&mercury_dest_path);

    let mut f = File::create(&mercury_dest_path).expect("Cant create mercury parser secrets file");

    let mercury_parser_secrets = format!(r#"
    #[derive(Debug)]
    pub struct MercuryParserKey {{
        pub mercury_user: String,
        pub mercury_key: String,
    }}

    impl MercuryParserKey {{
        pub fn new() -> Self {{
            MercuryParserKey {{
                mercury_user: obfstr::obfstr!("{}").into(),
                mercury_key: obfstr::obfstr!("{}").into(),
            }}
        }}
    }}
    "#,
        mercury_user, mercury_key
    );

    f.write_all(&mercury_parser_secrets.as_bytes())
        .expect("failed to write mercury parser secrets.rs");
}
