/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see .
*/
use super::{sprite::SpriteDraw, AtlasLayout, Renderer};
use hurrycurry_protocol::glam::Vec2;
use sdl2::rect::Rect;
pub struct FontTextures {
    pub glyphs: [Rect; 128],
}
impl FontTextures {
    pub fn init(layout: &AtlasLayout) -> Self {
        FontTextures {
            glyphs: (0..128)
                .into_iter()
                .map(|n| {
                    layout
                        .get(&format!("letter_{n}+a"))
                        .copied()
                        .unwrap_or(Rect::new(0, 0, 0, 0))
                })
                .collect::>()
                .try_into()
                .unwrap(),
        }
    }
}
impl<'a> Renderer<'a> {
    pub fn draw_text(&mut self, position: Vec2, text: &str) -> Vec2 {
        let mut cursor = position;
        let mut line_height = 0f32;
        for c in text.chars() {
            if (c as u32) < 128 {
                let r = self.font_textures.glyphs[c as usize];
                self.draw_ui(SpriteDraw::overlay(
                    r,
                    cursor,
                    Vec2::new(r.width() as f32, r.height() as f32),
                    None,
                ));
                cursor.x += r.width() as f32;
                line_height = line_height.max(r.height() as f32)
            }
        }
        (cursor - position.y) + Vec2::Y * line_height
    }
}