use crate::models::Function;

pub fn show_area_query(a: &Option<i32>, first: bool) -> rinja::Result<String> {
    let char = if first { '?' } else { '&' };

    if let Some(a) = a {
        return Ok(format!("{}area={}", char, a));
    } else {
        return Ok(String::new());
    }
}

pub fn cond_show(show: &bool, text: &str) -> rinja::Result<String> {
    return if *show {
        Ok(String::from(text))
    } else {
        Ok(String::new())
    };
}

pub fn insert_value(option: &Option<String>) -> rinja::Result<String> {
    if let Some(val) = option {
        let s = format!(r#"value="{val}""#);
        return Ok(s);
    }

    Ok(String::new())
}

pub fn is_some_and_eq<T>(option: &Option<T>, other: &T) -> rinja::Result<bool>
where
    T: Eq,
{
    Ok(option.as_ref().is_some_and(|x| x == other))
}

pub fn invert(b: &bool) -> rinja::Result<bool> {
    return Ok(!b);
}

pub fn show_tree(f: &Function) -> rinja::Result<String> {
    let mut tags = String::from(r#"<span class="tag is-info is-light">Posten</span>"#);

    if f == &Function::Fuehrungsassistent || f == &Function::Wachhabender {
        tags.push_str(r#"<span class="tag is-info is-light">Führungsassistent</span>"#);
    }

    if f == &Function::Wachhabender {
        tags.push_str(r#"<span class="tag is-info">Wachhabender</span>"#);
    }

    Ok(format!(r#"<div class="tags">{tags}</div>"#))
}
