// Askama Template
use askama::Template;

use actix_web::HttpRequest;

// define struct linked to template context
#[derive(Template)]
// define path to use template in
#[template(path="index.html")]
pub struct TplIndex<'a> {
    // replace with lang
    pub lang: &'a str,
}

#[derive(Template)]
// define path to use template in
#[template(path="hemp.html")]
pub struct TplHemp<'a> {
    // replace with lang
    pub lang: &'a str,
}

#[derive(Template)]
// define path to use template in
#[template(path="spider.html")]
pub struct TplSpider<'a> {
    // replace with lang
    pub lang: &'a str,
}

#[derive(Template)]
// define path to use template in
#[template(path="cube.html")]
pub struct TplCube<'a> {
    // replace with lang
    pub lang: &'a str,
}

#[derive(Template)]
// define path to use template in
#[template(path="cyber.html")]
pub struct TplCyber<'a> {
    // replace with lang
    pub lang: &'a str,
}

#[derive(Template)]
// define path to use template in
#[template(path="offgrid.html")]
pub struct TplOff<'a> {
    // replace with lang
    pub lang: &'a str,
}

// linked to error template
#[derive(Template)]
#[template(path="error.html")]
pub struct TplError<'a> {
    pub lang: &'a str,
    pub error_msg: &'a str,

}

// Get Language from Client Header
pub fn get_lang( req: &HttpRequest ) -> String {
    // get Accept-Language header
    if let Some( accept_language ) = req.headers().get( "Accept-Language" ) {
        if let Ok( s ) = accept_language.to_str() {
            println!("language is {}", s);
            // take first two characters of header
            return s.to_lowercase()[..2].to_string();
        }
    }
    
    String::from("en")
}

// Define askama filter 
mod filters {
    use crate::build_actix::config::LOC;

    pub fn translate(key: &str, lang: &str) -> askama::Result<String> {

        let translation = LOC.get(key).ok_or_else(|| {

            eprintln!("no translation available for key {}", key);
            askama::Error::from(std::fmt::Error)
        })?;
        Ok( String::from(
            translation.get( lang )
            .unwrap_or( translation.get( "en" ).ok_or_else(|| {
                eprintln!("no translation available for lan {} in key {}", lang, key );
                askama::Error::from(std::fmt::Error)
            })?)
            .as_str()
            .ok_or_else( || {
                eprintln!("lang {} in key {} is not a string", lang, key );
                askama::Error::from(std::fmt::Error)
            })?,
        ))
    }

}



