use {
    ab_glyph::{Font, FontRef, Glyph},
    vga::{
        colors::Color16,
        writers::{Graphics640x480x16, GraphicsWriter},
    },
};

lazy_static::lazy_static! {
    pub static ref VGAE: spin::Mutex<Graphics640x480x16> = {
        let xyz =  Graphics640x480x16::new();
        xyz.set_mode();
        spin::Mutex::new(xyz)
    };
     pub static ref VGAE_BUFF_OFFSET_X: spin::Mutex<u8> = spin::Mutex::new(0);
     pub static ref VGAE_BUFF_OFFSET_Y: spin::Mutex<u8> = spin::Mutex::new(0);
}

pub fn draw_char(previous_character: Option<char>, character: char, _offset: usize) {
    let mode = *VGAE.lock();
    let mut offset_x = VGAE_BUFF_OFFSET_X.lock();
    let mut offset_y = VGAE_BUFF_OFFSET_Y.lock();

    if *offset_x == 39 {
        *offset_x = 0;
        *offset_y += 1;
    }

    let font = FontRef::try_from_slice(include_bytes!(
        "../../ableos/assets/fonts/unifont-14.0.01.ttf"
    ))
    .unwrap();

    let font2 = FontRef::try_from_slice(include_bytes!(
        "../../ableos/assets/fonts/unifont_upper-14.0.01.ttf"
    ))
    .unwrap();

    let mut has_char = false;
    for x in font.codepoint_ids() {
        if x.1 == character {
            has_char = true;
            break;
        }
    }

    let used_font;
    match has_char {
        true => used_font = font,
        false => used_font = font2,
    }

    let font_scale = 1.6;

    match character {
        '\n' => {}
        _ => {
            let previous_glyph: Glyph = used_font
                .glyph_id(previous_character.unwrap_or(' '))
                .with_scale_and_position(
                    20.0 * font_scale,
                    ab_glyph::point(
                        *offset_x as f32 * (10.0 * font_scale),
                        *offset_y as f32 + (18.0 * font_scale),
                    ),
                );

            let mut q_glyph: Glyph = used_font.glyph_id(character).with_scale_and_position(
                20.0 * font_scale,
                ab_glyph::point(
                    *offset_x as f32 * (10.0 * font_scale),
                    *offset_y as f32 + (18.0 * font_scale),
                ),
            );
            {
                // figure it our latter bruh
                let spacing = used_font.kern_unscaled(previous_glyph.id, q_glyph.id);
                // info!("{spacing}   {:?}    {:?}", previous_glyph.id, q_glyph.id);
                q_glyph.position.x += spacing * font_scale;
            }

            if let Some(q) = used_font.outline_glyph(q_glyph) {
                q.draw(|x, y, c| {
                    if c > 0.015 {
                        let corner = q.px_bounds().min;
                        mode.set_pixel(
                            x as usize + corner.x as usize,
                            y as usize + corner.y as usize,
                            Color16::Green,
                        );
                    }
                });
            }

            *offset_x += 1;
        }
    }
}
pub fn num_to_vga16(num: u8) -> Color16 {
    use Color16::*;
    match num {
        0 => Black,
        1 => Blue,
        2 => Green,
        3 => Cyan,
        4 => Red,
        5 => Magenta,
        6 => Brown,
        7 => LightGrey,
        8 => DarkGrey,
        9 => LightBlue,
        10 => LightGreen,
        11 => LightCyan,
        12 => LightRed,
        13 => Pink,
        14 => Yellow,
        15 => White,
        // NOTE: Leasve the in
        _ => Color16::Pink,
    }
}
