package entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.ld58.game.rcr;

import effects.Explosion;

public class Ball extends Sprite {
	public enum State {
		MAIN
	};

	public State currentState;
	public State previousState;
	protected Animation<TextureRegion> main;
	private Texture t1;
	private TextureRegion main2;
	private TextureRegion main3;
	private TextureRegion main4;
	private TextureRegion main5;
	private TextureRegion main6;
	private TextureRegion main7;
	private TextureRegion main8;
	private TextureRegion main9;
	private TextureRegion main10;
	private TextureRegion texture;
	public Color color;
	public Color _color;
	public Rectangle rect;
	private int _sw;// sprite width
	private int _sh;// sprite height
	protected boolean isAnimationOver;
	protected float _stateTimer;

	public Vector2 sp;
	public Vector2 ep;
	public Vector2 dir;
	public Vector2 initDir;
	public float dist;
	public float newDist;
	public float speed = 400f;// was 800f
	public float height = 100;
//	private float originX;
//	private float originY;
	public boolean isDead = true;
	public boolean fired;
	public int ballLife = 0;
	public boolean firedOne;
	// private float initDistance;
	public Explosion explosion;
	public boolean canHit;

	/**
	 * @param _x           start position x.
	 * @param _y           start position y.
	 * @param endingPointX = end point x.
	 * @param endingPointY = end point y.
	 */
	public Ball(float _x, float _y, float endingPointX, float endingPointY) {
		rect = new Rectangle();
		rect.x = (int) _x + 30;
		rect.y = (int) _y + 50;
		rect.width = 10;
		rect.height = 10;
		// originX = _x + 30;
		// originY = _y + 50;

		_sw = 10;
		_sh = 10;

		sp = new Vector2(_x, _y);
		ep = new Vector2(endingPointX, endingPointY);
		dir = GetDirection(sp, ep);
		initDir = GetDirection(sp, ep);
		dist = u.UsefulMethods.GetDistance(sp, ep);
		canHit = true;
		explosion = new Explosion(_x, _y);

		// still frames
		t1 = new Texture(Gdx.files.internal("bomb.png"));
		Array<TextureRegion> frames = new Array<TextureRegion>();
		frames.add(new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh));
		main = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		main2 = new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh);
		main3 = new TextureRegion(t1, (2 * _sw), (0 * _sh), _sw, _sh);
		main4 = new TextureRegion(t1, (3 * _sw), (0 * _sh), _sw, _sh);
		main5 = new TextureRegion(t1, (0 * _sw), (1 * _sh), _sw, _sh);
		main6 = new TextureRegion(t1, (1 * _sw), (1 * _sh), _sw, _sh);
		main7 = new TextureRegion(t1, (2 * _sw), (1 * _sh), _sw, _sh);
		main8 = new TextureRegion(t1, (3 * _sw), (1 * _sh), _sw, _sh);
		main9 = new TextureRegion(t1, (0 * _sw), (2 * _sh), _sw, _sh);
		main10 = new TextureRegion(t1, (1 * _sw), (2 * _sh), _sw, _sh);
		frames.clear();// for now main is the same as onehundred
		color = new Color(MathUtils.random(.01f, 1f), MathUtils.random(.01f, 1f), MathUtils.random(.01f, 1f), 1);
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setTheRegion();// picks a random region!
		setRegion(main.getKeyFrame(_stateTimer, true));
	}

	private void setTheRegion() {
		int randomTexture = MathUtils.random(1, 10);
		if (randomTexture == 1)
			texture = main.getKeyFrame(_stateTimer, true);
		else if (randomTexture == 2)
			texture = main2;
		else if (randomTexture == 3)
			texture = main3;
		else if (randomTexture == 4)
			texture = main4;
		else if (randomTexture == 5)
			texture = main5;
		else if (randomTexture == 6)
			texture = main6;
		else if (randomTexture == 7)
			texture = main7;
		else if (randomTexture == 8)
			texture = main8;
		else if (randomTexture == 9)
			texture = main9;
		else if (randomTexture == 10)
			texture = main10;
		else
			texture = main.getKeyFrame(_stateTimer, true);

	}

	public TextureRegion getTextureRegion() {
		return texture;
	}

	public Vector2 GetDirection(Vector2 sp, Vector2 ep) {
		return ep.sub(sp).nor();
	}

	public void update(State cs, float dt) {
		// System.out.println("where am I? x:" + rect.x + "y: " + rect.y);
		if (isDead) {
			// rect.x = 9000;
			// rect.y = 9000;
			System.out.println("ball isdead: " + isDead);
		} else {
			if (fired) {
				rect.x += dir.x * speed * dt;
				rect.y += dir.y * speed * dt;
			}
			if (ballLife > 0)
				ballLife--;
			if (ballLife <= 0) {
				isDead = true;
				fired = false;
				explosion.rect.x = rect.x;
				explosion.rect.y = rect.y;
				explosion.isDead = false;
				explosion.isResetAnimation = false;
				// ballLife = BALLLIFE;
				// rect.x = originX;
				// rect.x = originY;
			}

		}
	}

	/**
	 * enemies the array of enemies
	 */
//	private Vector2 getClosestEnemy(Array<Weasel> weasels) {
//		float closestDistance = 1000000;
//		Vector2 _sp = new Vector2(rect.x, rect.y);// starting point is always this initial 'living point'.
//		Vector2 _ep = new Vector2(ep.x, ep.y);// we don't actually know this. This is what we are determining.
//		HashMap<Float, Vector2> _distances = new HashMap<Float, Vector2>();
//		for (Iterator<Weasel> iter2 = weasels.iterator(); iter2.hasNext();) {
//			Weasel w = iter2.next();
//			ep = new Vector2(w.rect.x, w.rect.y);
//			float dist = UsefulMethods.GetDistance(_sp, ep);
//			if (dist < closestDistance) {
//				closestDistance = dist;
//				_ep = ep;
//			}
//			_distances.put(closestDistance, _ep);
//		}// now these can be chained with any number of other entities below to add more enemies or objects that can be sorted and checked against.
//			// when done chaining these figure out who's the closest entity and return those coords. We don't even care who it is. ;-)
//		for (Float key : _distances.keySet()) {
//			if (key < closestDistance)
//				closestDistance = key;
//		}
//		return _distances.get(closestDistance);
//	}

	// for all the animations: //this isn't even updated unless i call it from the
	// other screen oh bother. *facepalm* yeah. :P
	public TextureRegion getFrame(OrthographicCamera camera, float dt) {
		currentState = getState();
		update(currentState, dt);

		if (currentState != previousState)
			isAnimationOver = false;

		if (!isAnimationOver && currentState == previousState) {
			_stateTimer += dt;
		} else
			_stateTimer = 0;
		previousState = currentState;

		return texture;
	}

	public State getState() {
		return State.MAIN;
	}

	public void render(rcr game, OrthographicCamera camera, float dt) {// hmmm i think it just needs placed before? it gets here.
		game.batch.setColor(color);
		game.batch.draw(getFrame(camera, dt), rect.x, rect.y);
	}

	public void dispose() {
	}

}
