use std::io;

use png::ColorType;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ImageError {
    #[error("can't make the layer fit {0}x{1}")]
    WrongSize(usize, usize),
    #[error("unsupported color type {0:?}")]
    InvalidInputColorType(ColorType),
    #[error("can't save an image with {0} layers as png")]
    WrongNumberOfLayersToSave(usize),

    #[error("general IO error")]
    IoError(#[from] io::Error),
    #[error("error in encoding PNG")]
    PngEncodingError(#[from] png::EncodingError),
    #[error("error in decoding PNG")]
    PngDecodingError(#[from] png::DecodingError),
}

pub struct Image {
    pub width: usize,
    pub height: usize,
    pub layers: Vec<(u8, Vec<u8>)>
}

impl Image {
    pub fn new(width: usize, height: usize) -> Self {
        Image {
            width, height, 
            layers: vec![]
        }
    }

    pub fn add(&mut self, quality: u8, pixels: Vec<u8>) -> Result<(), ImageError> {
        if pixels.len() != self.width * self.height {
            return Err(ImageError::WrongSize(self.width, self.height));
        }
        self.layers.push((quality, pixels));

        Ok(())
    }
}