package entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
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.TextureAtlas;
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.ld50.game.HF;

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

	public State currentState;
	public State previousState;
	private Animation<TextureRegion> main;
	private TextureAtlas atlas;
	private float _animBreakingTimer;
	public Color color;
	public Rectangle rect;
	public boolean isMain = true;// start with main
	public boolean isDead;// move off screen.
	public int _isDeadTimer;
	private int _sw;// sprite width
	private int _sh;// sprite height
	private float speed;
	public Vector2 sp;
	public Vector2 ep;
	private Vector2 dir;
	public boolean isWait = true;
	public boolean getDir;
	public final int BULLETLIFE = 100;
	private int bulletLife = BULLETLIFE;
	private boolean fired;
	private boolean isCollidingWithGlass;// add when glass is around. i think i can do that. lol

	public Bullet(int _x, int _y) {
		rect = new Rectangle();
		rect.x = _x;
		rect.y = _y;
		rect.width = 10;
		rect.height = 10;

		_sw = 10;
		_sh = 10;
		// this is for the animations
		currentState = State.MAIN;
		previousState = State.MAIN;
		_animBreakingTimer = 0f;
		// declare all the static frames (static states (no animation))
		speed = 800f;
		// ok when a growl is created i need it to go from where it is at the mouse, is that possible?
		atlas = new TextureAtlas(Gdx.files.internal("bullet.txt"));
		// animated frames
		Array<TextureRegion> frames = new Array<TextureRegion>();
		frames.add(new TextureRegion(atlas.findRegion("bullet"), (0 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(atlas.findRegion("bullet"), (1 * _sw), (0 * _sh), _sw, _sh));
		main = new Animation<TextureRegion>(.06f, frames, PlayMode.LOOP);
		frames.clear();// definitely starts right. then we say, when the line is one length + one _sw worth which would be what in actual numbers?
		// these two are specifically sprite settings to set the sprite's initial bounds and region
		// assign random color
		color = new Color(new Color(MathUtils.random(.2f, 1f), MathUtils.random(.2f, 1f), MathUtils.random(.2f, 1f), 1));
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(main.getKeyFrame(_animBreakingTimer, true));
	}

	public Vector2 GetDirection(Vector2 sp, Vector2 ep) {
		rect.x = sp.x;
		rect.y = sp.y;
		return ep.sub(sp).nor();
	}

	public void Update(float dt) {
		/*
		 * if (rect.overlaps(Global.PP) || isBusted) { isBusted = true; isMain = false; isDead = true; } else {
		 */

		if (isDead) {
			rect.x = 10000;
			rect.y = 10000;
		} else if (fired) {
			isMain = true;
			rect.x += dir.x * speed * dt;
			rect.y += dir.y * speed * dt;
		}
		if (getDir) {
			getDir = false;
			dir = GetDirection(sp, ep);
		}
		if (bulletLife > 0)
			bulletLife--;
		if (bulletLife <= 0 || isCollidingWithGlass)
			isDead = true;
		// System.out.println("x: " + rect.x);
		// System.out.println("y: " + rect.y);
		// System.out.println("bullet life: " + bulletLife);
	}

	// for all the animations: //this isn't even updated unless i call it from the other screen oh bother. *facepalm*
	public TextureRegion getFrame(float dt) {
		Update(dt);

		currentState = getState();

		TextureRegion region;
		switch (currentState) {
		case MAIN:
			region = main.getKeyFrame(_animBreakingTimer, true);
			break;
		default:
			region = main.getKeyFrame(_animBreakingTimer, false);
			break;
		}
		if (isMain)
			_animBreakingTimer += dt;
		else
			_animBreakingTimer = 0;

		return region;
	}

	public void FireBullet(float _x, float _y, float _mouseX, float _mouseY) {
		sp = new Vector2(_x, _y);
		ep = new Vector2(_mouseX, _mouseY);
		dir = GetDirection(sp, ep);
		bulletLife = BULLETLIFE;
		rect.x = _x;
		rect.y = _y;
		isDead = false;
		fired = true;
	}

	public State getState() {
		if (isMain)
			return State.MAIN;
		else
			return State.MAIN;
	}

	public void render(HF game, float dt) {
		game.batch.draw(getFrame(dt), rect.x, rect.y);
	}

}
