use actix_web::{ HttpRequest, HttpResponse };
// general purpose error
use actix_web::error::Error;

use crate::build_actix::template;
use crate::build_actix::error;

use askama::Template;

pub async fn index( req: HttpRequest ) -> Result<HttpResponse, Error> {
    // if response Ok return HttpResponseBuilder
    HttpResponse::Ok()
    // set response content type html
    .content_type("text/html")
    // set response body to template context
    .body(
        // render template context
        template::TplIndex {
            // lang to value of Accept-Language header
            lang : &template::get_lang(&req),
        }
        // render template context into String
        .render()
        .map_err( |e| {
            eprintln!("error_tplrender : {}", e );
            error::crash( template::get_lang(&req), "error_tplrender" )
        })?,
    ).await
}

pub async fn hemp( req: HttpRequest ) -> Result<HttpResponse, Error> {
    // if response Ok return HttpResponseBuilder
    HttpResponse::Ok()
    // set response content type html
    .content_type("text/html")
    // set response body to template context
    .body(
        // render template context
        template::TplHemp {
            // lang to value of Accept-Language header
            lang : &template::get_lang(&req),
        }
        // render template context into String
        .render()
        .map_err( |e| {
            eprintln!("error_tplrender : {}", e );
            error::crash( template::get_lang(&req), "error_tplrender" )
        })?,
    ).await
}

pub async fn spider( req: HttpRequest ) -> Result<HttpResponse, Error> {
    // if response Ok return HttpResponseBuilder
    HttpResponse::Ok()
    // set response content type html
    .content_type("text/html")
    // set response body to template context
    .body(
        // render template context
        template::TplSpider {
            // lang to value of Accept-Language header
            lang : &template::get_lang(&req),
        }
        // render template context into String
        .render()
        .map_err( |e| {
            eprintln!("error_tplrender : {}", e );
            error::crash( template::get_lang(&req), "error_tplrender" )
        })?,
    ).await
}

pub async fn cube( req: HttpRequest ) -> Result<HttpResponse, Error> {
    // if response Ok return HttpResponseBuilder
    HttpResponse::Ok()
    // set response content type html
    .content_type("text/html")
    // set response body to template context
    .body(
        // render template context
        template::TplCube {
            // lang to value of Accept-Language header
            lang : &template::get_lang(&req),
        }
        // render template context into String
        .render()
        .map_err( |e| {
            eprintln!("error_tplrender : {}", e );
            error::crash( template::get_lang(&req), "error_tplrender" )
        })?,
    ).await
}

pub async fn cyber( req: HttpRequest ) -> Result<HttpResponse, Error> {
    // if response Ok return HttpResponseBuilder
    HttpResponse::Ok()
    // set response content type html
    .content_type("text/html")
    // set response body to template context
    .body(
        // render template context
        template::TplCyber {
            // lang to value of Accept-Language header
            lang : &template::get_lang(&req),
        }
        // render template context into String
        .render()
        .map_err( |e| {
            eprintln!("error_tplrender : {}", e );
            error::crash( template::get_lang(&req), "error_tplrender" )
        })?,
    ).await
}

pub async fn off( req: HttpRequest ) -> Result<HttpResponse, Error> {
    // if response Ok return HttpResponseBuilder
    HttpResponse::Ok()
    // set response content type html
    .content_type("text/html")
    // set response body to template context
    .body(
        // render template context
        template::TplOff {
            // lang to value of Accept-Language header
            lang : &template::get_lang(&req),
        }
        // render template context into String
        .render()
        .map_err( |e| {
            eprintln!("error_tplrender : {}", e );
            error::crash( template::get_lang(&req), "error_tplrender" )
        })?,
    ).await
}