package uk.co.beercave.gunboat.entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool.Poolable;

import uk.co.beercave.gunboat.ResourceManager;
import uk.co.beercave.gunboat.renderers.SeaRenderer;
import uk.co.beercave.gunboat.renderers.SeaRenderer.Renderable;

public class Boat extends GameObject implements Poolable{
	private static Array<Color> boatColors;
	public float dx = 0;
	public Renderable sprite;
	public Renderable attackWarning;
	public boolean attacking;
	public float attackTime;
	public float width = 200;
	public Boat(){
		if (boatColors == null) initColors();
		floating = true;
		
	}
	public void init() {
		alive = true;
		sprite = SeaRenderer.newSprite();
		sprite.tx = ResourceManager.boat;
		sprite.color = boatColors.random();
		sprite.parent = this;
	}
	private void initColors() {
		boatColors = new Array<Color>();
		//boatColors.add(new Color(1,0,0,1));
		//boatColors.add(new Color(0,1,0,1));
		//boatColors.add(new Color(0,0,1,1));
		//boatColors.add(new Color(0,1,1,1));
		//boatColors.add(new Color(1,0,1,1));
		//boatColors.add(new Color(1,1,0,1));
		
		boatColors.add(colourFromHex("FF0686"));
		boatColors.add(colourFromHex("FFFB06"));
		boatColors.add(colourFromHex("06FF7F"));
		boatColors.add(colourFromHex("7F06FF"));
		boatColors.add(colourFromHex("0687FF"));
		boatColors.add(colourFromHex("06FFFB"));
	}
	private Color colourFromHex(String hex){
		Gdx.app.log("", "colour from hex : " + hex);
		String xr = hex.substring(0, 2);
		String xg = hex.substring(2, 4);
		String xb = hex.substring(4, 6);
		
		Gdx.app.log("", "R=" + xr + " G=" + xg + " B=" + xb);
		
		float r = (float)Integer.valueOf(xr, 16) * 1/255f;
		float g = Integer.valueOf(xg, 16) * 1/255f;
		float b = Integer.valueOf(xb, 16) * 1/255f;
		
		Gdx.app.log("", "R=" + new Float(r).toString() + " G=" + new Float(g).toString() + " B=" + new Float(b).toString());

		return new Color(r,g,b,1);
	}
	@Override
	public void reset() {
		Gdx.app.log("", "Releasing boat");
		SeaRenderer.releaseSprite(sprite);
		if (attackWarning != null) SeaRenderer.releaseSprite(attackWarning);
		
	}
}
