extern crate image;
use std::fs::File;
use std::path::Path;

pub struct Board {
   pub width: usize,
   pub height: usize,
   pub data: Vec<String>
}

impl Board {
   pub fn new(x: usize, y: usize) -> Board {
      Board {
         width: x,
         height: y,
         data: vec!["".to_string(); x*y]
      }
   }
   pub fn colour(&self, x: usize, y: usize) -> (u8,u8,u8) {
      let s = self.get(x, y);
      match s.as_ref() {
         "" => { (255,255,255) }
         "Y" => { (0,0,255) }
         "N" => { (255,0,0) }
         "?" => { (255,255,0) }
         "t" => { (0,0,0) }
         _ => { panic!("Unknown sigil: {}", s) }
      }
   }
   pub fn get(&self, x: usize, y: usize) -> String {
      self.data[ y*self.width + x ].clone()
   }
   pub fn set(&mut self, x: usize, y: usize, s: String) {
      self.data[ y*self.width + x ] = s.clone()
   }
   pub fn screenshot(&self, path: String) {
      let mut imgbuf = image::ImageBuffer::new( self.width as u32, self.height as u32 );

      for x in 0 .. self.width {
         for y in 0 .. self.height {
            let (r,g,b) = self.colour(x, y);
            imgbuf.put_pixel(x as u32, y as u32, image::Rgb([ r, g, b ]));
         }
      }

      let ref mut fout = File::create(&Path::new( &path )).unwrap();
      let _ = image::ImageRgb8(imgbuf).save(fout, image::PNG);
   }
}
