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 java.util.Random;

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;
	// TEST
	protected BufferedImage bf2;
	protected BufferedImage bf3;
	protected BufferedImage bf4;
	protected BufferedImage bf5;
	protected boolean loaded = false;
	protected Random rand;
	// TEST
	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;
	private char imgChar;
	// For object animation
	private int tickCounter = 0;
	private int ticker = 0;
	
	public StaticObject(char staticObjectType, Point location, int staticObjectWidth, int staticObjectHeight) {
		this.staticObjectType = staticObjectType;
		this.staticObjectWidth = staticObjectWidth;
		this.staticObjectHeight = staticObjectHeight;
		this.setImgChar(staticObjectType);
		loadBufferedImage();
		setAutoBounds(location.x, location.y, staticObjectWidth, staticObjectHeight);
	}

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

	public void setImgChar(char c) {
		this.imgChar = c;
	}
	
	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();
		}

		
		// Part for Animation
		this.tickCounter++;
		if ( (this.tickCounter%10) == 0 ) {
			this.tickCounter = 0;

			if ( this.staticObjectType == '0' || this.staticObjectType == '1' || this.staticObjectType == '2' || this.staticObjectType == '3' || this.staticObjectType == '4' || this.staticObjectType == '5' || this.staticObjectType == '6' || this.staticObjectType == '7' || this.staticObjectType == '8' || this.staticObjectType == '9') {
				if ( ! this.loaded ) {
					loadBufferedImage_to(2,'0');
					loadBufferedImage_to(3,'1');
					this.loaded = true;
				}
				
				if ( ticker == 0 ) {
//					setImgChar('0');
//					loadBufferedImage();
					setImage(this.bf2);
					ticker = 1;
					
				} else if ( ticker == 1 ) {
//					setImgChar('2');
//					loadBufferedImage();
					setImage(this.bf3);
					ticker = 0;
				}
			}

			if ( this.staticObjectType == '-') {
				if ( ! this.loaded ) {
					loadBufferedImage_to(2,'-');
					loadBufferedImage_to(3,'m');
					loadBufferedImage_to(4,'n');
					this.loaded = true;
					rand = new Random();
					
				}

				
				ticker = rand.nextInt(3);
				
				if ( ticker == 0 ) {
//					setImgChar('0');
//					loadBufferedImage();
					setImage(this.bf2);
					ticker = 1;
					
				} 
				else if ( ticker == 1 ) {
//					setImgChar('2');
//					loadBufferedImage();
					setImage(this.bf3);
					ticker = 2;
				} 
				else if ( ticker == 2 ) {
//					setImgChar('2');
//					loadBufferedImage();
					setImage(this.bf4);
					ticker = 0;
				}
			}
		
		}		
		
		/*
		 * 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.imgChar);
	}

	protected void loadBufferedImage_to(int n, char imgChar) {
		if ( n == 2 ) {
			this.bf2 = PanelBuilder.getImageLoader().getBufferedImage(imgChar);			
		} else if ( n == 3 ) {
			this.bf3 = PanelBuilder.getImageLoader().getBufferedImage(imgChar);
		} else if ( n == 4 ) {
			this.bf4 = PanelBuilder.getImageLoader().getBufferedImage(imgChar);
		} else if ( n == 5 ) {
			this.bf5 = PanelBuilder.getImageLoader().getBufferedImage(imgChar);
		} 
	}
	
	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;
	}
}