package com.teppefall.subpixel;

/**
 * @author janerik
 * 
 * SubpixelRenderer.java
 * 
 * Darkstar 2D renderer. You can prototype stuff like this in Surface.
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.teppefall.ds.console.Console;
import com.teppefall.ds.render2d.D2DRenderer;
import com.teppefall.ds.render2d.Darkstar;

public class SubpixelRenderer implements D2DRenderer {

	Color color = Color.BLACK, otherColor = Color.WHITE;
	Rectangle2D plane;
	boolean split = false, gradient = false, infoGraphics = true;
	BufferedImage arrowsImage, circleImage;
	
	public void destroy() {}
	public void initialize(Darkstar parent, Color foreground, Color background) {
		try {
			arrowsImage = ImageIO.read(getClass().getResource("/images/subpixel.png"));
			circleImage = ImageIO.read(getClass().getResource("/images/subpixel2.png"));
		}
		catch(IOException e) {
			Console.warn(this, e);
		}
		catch(Exception e) {
			Console.warn(this, e);
		}
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public Color getColor() {
		return color;
	}

	public void setSplit(boolean split) {
		this.split = split;
	}
	
	public void setGrid(boolean grid) {
		this.gradient = grid;
	}

	public void setInfoGraphics(boolean usingInformationGraphics) {
		this.infoGraphics = usingInformationGraphics;
	}
	
	public boolean isSplit() {
		return split;
	}
	
	public boolean isGradient() {
		return gradient;
	}

	public boolean isInfoGraphics() {
		return infoGraphics;
	}
	
	public void render(Graphics2D g, Dimension size) {
		g.setColor(color);

		if(color == Color.BLACK) {
			otherColor = Color.WHITE;
		}
		else if(color == Color.WHITE) {
			otherColor = Color.BLACK;
		}
		else {
			try {
				otherColor = new Color(color.getGreen(), color.getBlue(), color.getRed());
			}
			catch(Exception e) {
				Console.info(this, "color problem");
				otherColor = Color.GREEN;
			}
		}

		if(isGradient()) {
			GradientPaint gradient = new GradientPaint(0, 0, color, size.width, size.height, otherColor);
	        g.setPaint(gradient);
		}
		if(isSplit()) {
			g.fillRect(0,0,size.width/2, size.height);
			if(isGradient()) {
				GradientPaint gradient = new GradientPaint(0, 0, color, size.width/2, size.height, otherColor);
		        g.setPaint(gradient);
			}
			else {
				g.setColor(otherColor);
			}
			
			g.fillRect(size.width/2,0,size.width/2, size.height);
		}
		else {
			g.fillRect(0,0,size.width, size.height);
		}
		
		if(isInfoGraphics()) {
			g.drawImage(arrowsImage, null, size.width/2-arrowsImage.getWidth()/2+2, size.height/2-arrowsImage.getHeight()/2);
			g.drawImage(circleImage, null, size.width/2-circleImage.getWidth()/2+2, size.height/2-circleImage.getHeight()/2);
		}
		
	}
	
	
}

