use image::Rgb;

pub type PixelBinaryMsg = [u8; 11];
pub type PixelBinaryMsgs = Vec<PixelBinaryMsg>;
pub type PixelStringMsg = Box<[u8]>;
pub type PixelStringMsgs = Vec<PixelStringMsg>;

pub trait Add {
    fn add(&mut self, item: (u32, u32, &Rgb<u8>));
}

impl Add for PixelBinaryMsgs {
    fn add(&mut self, item: (u32, u32, &Rgb<u8>)) {
        let pb: &[u8] = &[80, 66];
        let x = (item.0 as u16).to_le_bytes();
        let y = (item.1 as u16).to_le_bytes();

        let color: &Rgb<u8> = item.2;
        let r = color.0[0].to_le_bytes();
        let g = color.0[1].to_le_bytes();
        let b = color.0[2].to_le_bytes();

        let msg = [pb, &x, &y, &r, &g, &b, &[0]].concat();
        self.push(msg.try_into().unwrap());
    }
}

impl Add for PixelStringMsgs {
    fn add(&mut self, item: (u32, u32, &Rgb<u8>)) {
        let color: &Rgb<u8> = item.2;
        let r = color.0[0];
        let g = color.0[1];
        let b = color.0[2];
        let msg = format!("PX {} {} {r:02x}{g:02x}{b:02x}\n", item.0, item.1)
            .as_bytes()
            .to_vec()
            .into_boxed_slice();
        self.push(msg);
    }
}
