package de.heatup.objects;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

import de.heatup.cfg.Config;
import de.heatup.ui.PanelBuilder;

public class StaticObject extends JComponent {
	private static final long serialVersionUID = 1L;
	protected BufferedImage bf1;
	protected Graphics2D g2d;
	private boolean doRescale = false;
	private double rescaleFactorX = 0.0;
	private double rescaleFactorY = 0.0;
	private boolean scale = false;
	protected Point currentGridPosition;
	private char staticObjectType;
	private int staticObjectWidth;
	private int staticObjectHeight;
	private int imgcounter = 0;
	
	public StaticObject(char staticObjectType, Point location, int staticObjectWidth, int staticObjectHeight) {
		this.staticObjectType = staticObjectType;
		this.staticObjectWidth = staticObjectWidth;
		this.staticObjectHeight = staticObjectHeight;
		loadBufferedImage();
		setAutoBounds(location.x, location.y, staticObjectWidth, staticObjectHeight);
	}

	public StaticObject(char staticObjectType, Point location) {
		this(staticObjectType, location, Config.singleTilePixels, Config.singleTilePixels);	
    }

	public char getStaticObjectType() {
		return this.staticObjectType;
	}

	public void setStaticObjectType(char staticObjectType) {
		this.staticObjectType = staticObjectType;
	}

	public void paintComponent(Graphics g) {
		this.g2d = (Graphics2D)g;
		this.g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		//this.g2d.setStroke(new BasicStroke(8.0f));
		if ( this.bf1 == null ) {
			loadBufferedImage();
		}

		
		this.imgcounter++;
		if ( staticObjectType == '0' && this.imgcounter > 0 && this.imgcounter < 5 ) {
			System.out.println("hit 0");
			staticObjectType = '2';
			loadBufferedImage();
		} 
		
		if ( staticObjectType == '2' && this.imgcounter > 5 && this.imgcounter < 10 ) {
			System.out.println("hit 2");
			staticObjectType = '0';
			loadBufferedImage();
		}
		if ( staticObjectType == '2' && this.imgcounter > 10 ) {
			staticObjectType = '0';
			this.imgcounter=0;
		}
		
		
//		this.imgcounter++;
//		if ( staticObjectType == 'p' && this.imgcounter > 0 && this.imgcounter < 5 ) {
//			System.out.println("hit 1");
//			staticObjectType = 'p';
//			loadBufferedImage();
//		} if ( staticObjectType == 'p' && this.imgcounter > 5 && this.imgcounter < 10 ) {
//			System.out.println("hit 2");
//			staticObjectType = 'q';
//			loadBufferedImage();
//		}
//		if ( staticObjectType == 'q' && this.imgcounter > 10 && this.imgcounter < 15 ) {
//			System.out.println("hit 3");
//			staticObjectType = 's';
//			loadBufferedImage();
//		}
//		if ( staticObjectType == 's' && this.imgcounter > 15 && this.imgcounter < 20 ) {
//			System.out.println("hit 4");
//			staticObjectType = 't';
//			loadBufferedImage();
//		}
//		if ( staticObjectType == 't' && this.imgcounter > 50) {
//			System.out.println("hit 5");
//			this.imgcounter = 0;
//			staticObjectType = 'p';
//		}
		
		/*
		 * Skalieren der Bilder wenn größer als Bounds
		 */
		if ( scale ) {
			this.g2d.scale(((double)this.getSize().width/(double)bf1.getWidth()) , ((double)this.getSize().height/(double)bf1.getHeight()));
		}
		
		this.g2d.drawImage(bf1, 0, 0, this);			
		this.g2d.finalize();
	}

		
	protected void loadBufferedImage() {
		this.bf1 = PanelBuilder.getImageLoader().getBufferedImage(this.staticObjectType);
	}
	
	public void setImage(BufferedImage image) {
		this.bf1 = image;
	}
		
	public void setAutoBounds(int x, int y) {
		this.setSize(this.bf1.getWidth(), this.bf1.getHeight());
		this.setLocation(x, y);
	}

	public void setAutoBounds(int x, int y, int width, int height) {
		this.setSize(width, height);
		this.setLocation(x, y);
		if ( ((bf1.getWidth() != this.getSize().width ) || (bf1.getHeight() != this.getSize().height)) ) {
			scale = true;
		}
	}
	
	public String getDebugInfo() {
		String info = "Debug: " + "Rescale? " + this.doRescale + "\tScale X:" + this.rescaleFactorX + "\tScale Y: " + this.rescaleFactorY + "\tsH: " + this.staticObjectHeight + "\tsW: " + this.staticObjectWidth + "\tStaticObjectType: " + this.staticObjectType + "\tbW : " + this.bf1.getWidth() + "\tbH :" + this.bf1.getHeight();
		return info;
	}
}