import java.awt.AWTException;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

@SuppressWarnings("serial")
public class Paint extends Canvas {
	
	protected Float[][] board;
	protected int maximum;
	protected int[][] input;
	protected String style;
	protected static Color[] greyshades = new Color[256];
	protected static Color[] colour = new Color[256];
	public static int pixel = 2;
	
    public Paint(Board boo, String norm) {
    	input = boo.getInput();
    	board = boo.getData();
    	maximum = boo.boardmax();
    	style = norm;
    }

    public void initialize(int pixel){
		Paint.pixel=pixel;
    	for(int l=0; l<256;l++){
    		Paint.greyshades[l]=new Color(l,l,l);
    	}
    	Paint.colour[0]=Color.white;
    	Paint.colour[1]=Color.red;
    	Paint.colour[2]=Color.blue;
    	Paint.colour[3]=Color.yellow;
    	Paint.colour[4]=Color.cyan;
    	Paint.colour[5]=Color.green;
    	Paint.colour[6]=Color.pink;
    	Paint.colour[7]=Color.darkGray;
    	Paint.colour[8]=Color.magenta;
    	Paint.colour[9]=Color.orange;
    	Random rando = new Random();
    	for(int l=10; l<256;l++){
    		int a = (int) (rando.nextFloat()*255);
    		int b = (int) (rando.nextFloat()*255);
    		int c = (int) (rando.nextFloat()*255);
    		Paint.colour[l]=new Color(a,b,c);
    	}
    	
    }
    
    public String getStil(){
    	return style;
    }
    
	public Float[][] getBoard(){
		return board;
	}
	
	public int[][] getInput(){
		return input;
	}
	
	public static void save(String Pfad,int xstart,int ystart,int height,int width) throws AWTException{
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Robot robot;
		try {
				robot = new Robot();
				BufferedImage bi=robot.createScreenCapture(new Rectangle((xstart+12),(ystart+45),height,width));
				ImageIO.write(bi, "png", new File(Pfad));
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	
    public void paint(Graphics g) {
    	 
    	if(this.getStil().equals("black")) {
    	    	int j = board[1].length;
    	    	int i = board.length;
    			for(int l=0; l<i;l++){
    				for(int m=0;m<j;m++){
    					if(this.board[l][m]==0){
    						g.setColor(Color.white);
    					} else {
    						g.setColor(Color.black);
    					}
    					g.fillRect(l*pixel,m*pixel,pixel,pixel);
    			        g.drawRect(l*pixel,m*pixel,pixel,pixel);
    				}
    			}
    	    } else if  (this.getStil().equals("colour")) {
    	    	int j = board[1].length;
    	    	int i = board.length;
    	    	
    			for(int l=0; l<i;l++){
    				for(int m=0;m<j;m++){
    					int here = input[l][m];
    					g.setColor(Paint.colour[here]);
    			        g.fillRect(l*pixel,m*pixel,pixel,pixel);
    			        g.drawRect(l*pixel,m*pixel,pixel,pixel);
    				}
    			}
    	    } else {
    	    	int j = board[1].length;
            	int i = board.length;

        		for(int l=0; l<i;l++){
        			for(int m=0;m<j;m++){
        				int here = (int) (255 * (1-this.board[l][m]));
        				g.setColor(Paint.greyshades[here]);
        				g.fillRect(l*pixel,m*pixel,pixel,pixel);
    			        g.drawRect(l*pixel,m*pixel,pixel,pixel);	
        			}
        		}
    	    }
    	    	
    	}

}
