//   a a a
// f       b
// f       b
// f       b
//   g g g
// e       c
// e       c
// e       c
//   d d d

use crate::font::{Font, Glyph};

pub struct RGB {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

pub trait Canvas {
    fn set_pixel(&mut self, x: usize, y: usize, color: &RGB);

    fn fill(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: &RGB) {
        for x in x1..x2 {
            for y in y1..y2 {
                self.set_pixel(x, y, color);
            }
        }
    }

    fn square(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: &RGB) {
        for x in x1..x2 + 1 {
            self.set_pixel(x, y1, color);
        }
        for x in x1..x2 + 1 {
            self.set_pixel(x, y2, color);
        }
        for y in y1..y2 + 1 {
            self.set_pixel(x1, y, color);
        }
        for y in y1..y2 + 1 {
            self.set_pixel(x2, y, color);
        }
    }
}

pub fn print<A>(
    canvas: &mut impl Canvas,
    font: &impl Font<A>,
    sx: usize,
    sy: usize,
    text: &str,
    color: &RGB,
) where
    A: Glyph,
{
    let mut x = sx;
    for c in text.chars().map(|c| font.glyph(c)) {
        c.draw(canvas, x, sy, color);
        let (dx, _) = c.extents();
        x = x + dx + 1;
    }
}