//! Render the peripheral-test screen (pm-ui) with mock input state → PNG, to check the layout
//! before flashing. `cargo run --bin periphsim`.

use embedded_graphics::{pixelcolor::Rgb565, prelude::*};
use pm_ui::PeriphState;

const W: u32 = 320;
const H: u32 = 480;

struct Fb {
    px: Vec<Rgb565>,
}
impl DrawTarget for Fb {
    type Color = Rgb565;
    type Error = core::convert::Infallible;
    fn draw_iter<I: IntoIterator<Item = Pixel<Rgb565>>>(&mut self, pixels: I) -> Result<(), Self::Error> {
        for Pixel(p, c) in pixels {
            if p.x >= 0 && p.y >= 0 && (p.x as u32) < W && (p.y as u32) < H {
                self.px[(p.y as u32 * W + p.x as u32) as usize] = c;
            }
        }
        Ok(())
    }
}
impl OriginDimensions for Fb {
    fn size(&self) -> Size {
        Size::new(W, H)
    }
}

fn main() {
    let mut fb = Fb { px: vec![Rgb565::BLACK; (W * H) as usize] };
    let st = PeriphState { a: true, b: false, x: 2900, y: 1100, beep: true };
    pm_ui::draw_peripheral_test(&mut fb, &st).unwrap();
    let img = image::RgbImage::from_fn(W, H, |x, y| {
        let c = fb.px[(y * W + x) as usize];
        image::Rgb([(c.r() << 3) | (c.r() >> 2), (c.g() << 2) | (c.g() >> 4), (c.b() << 3) | (c.b() >> 2)])
    });
    img.save("periph.png").unwrap();
    println!("wrote periph.png");
}
