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.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.ld52.agent.Agent;

import effects.BallShadow;

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

	public State currentState;
	public State previousState;
	protected Animation<TextureRegion> main;
	private Texture t1;
	// private TextureRegion main;
	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 = 800f;
	public float height = 100;
	private float originX;
	private float originY;
	public boolean isDead = true;
	public boolean fired;
	public final int BALLLIFE = 300;// was 100
	private int ballLife = 0;
	public boolean firedOne;
	private float initDistance;
	public BallShadow shadow;

	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 = 25;
		rect.height = 25;
		originX = _x + 30;
		originY = _y + 50;

		_sw = 25;
		_sh = 25;

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

		shadow = new BallShadow(_x, _y, ep.x, ep.y);

		// still frames
		t1 = new Texture(Gdx.files.internal("ball.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);
		frames.clear();// for now main is the same as onehundred
		color = Color.WHITE;
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(main.getKeyFrame(_stateTimer, true));
	}

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

	public void update(State cs, float dt) {
		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;
				ballLife = BALLLIFE;
				rect.x = originX;
				rect.x = originY;
			}

		}
	}

	/**
	 * @weasels the array of weasels
	 */
//	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);

		TextureRegion region;
		switch (currentState) {
		case MAIN:
			region = main.getKeyFrame(_stateTimer, false);
			break;
		default:
			region = main.getKeyFrame(_stateTimer, false);
			break;
		}
		if (currentState != previousState)
			isAnimationOver = false;

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

		return region;
	}

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

	public void render(Agent 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() {
	}

}
