//! Source Sans Pro and Phosphor font registration for every Blacksite egui host.

use std::sync::Arc;

pub const SOURCE_SANS_REGULAR: &str = "blacksite-source-sans-regular";
pub const SOURCE_SANS_BOLD: &str = "blacksite-source-sans-bold";

pub fn font_definitions() -> egui::FontDefinitions {
    let mut fonts = egui::FontDefinitions::default();
    egui_phosphor_icons::add_fonts(&mut fonts);
    fonts.font_data.insert(
        SOURCE_SANS_REGULAR.to_owned(),
        Arc::new(egui::FontData::from_static(include_bytes!(
            "../assets/fonts/source-sans/SourceSansPro-Regular.ttf"
        ))),
    );
    fonts.font_data.insert(
        SOURCE_SANS_BOLD.to_owned(),
        Arc::new(egui::FontData::from_static(include_bytes!(
            "../assets/fonts/source-sans/SourceSansPro-Bold.ttf"
        ))),
    );
    fonts
        .families
        .entry(egui::FontFamily::Proportional)
        .or_default()
        .insert(0, SOURCE_SANS_REGULAR.to_owned());
    fonts.families.insert(
        egui::FontFamily::Name(SOURCE_SANS_REGULAR.into()),
        vec![SOURCE_SANS_REGULAR.to_owned()],
    );
    fonts.families.insert(
        egui::FontFamily::Name(SOURCE_SANS_BOLD.into()),
        vec![SOURCE_SANS_BOLD.to_owned(), SOURCE_SANS_REGULAR.to_owned()],
    );
    let fallbacks = fonts
        .families
        .get(&egui::FontFamily::Proportional)
        .cloned()
        .unwrap_or_default();
    for (family, font_names) in &mut fonts.families {
        let egui::FontFamily::Name(name) = family else {
            continue;
        };
        if name.starts_with("phosphor-") {
            for fallback in &fallbacks {
                if !font_names.contains(fallback) {
                    font_names.push(fallback.clone());
                }
            }
        }
    }
    fonts
}
