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.utils.Array;
import com.ld50.game.HF;

public class Gunfire 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
	public boolean isWait = true;
	public boolean getDir;
	public final int BULLETLIFE = 10;
	private int bulletLife = BULLETLIFE;
	private boolean fired;

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

		_sw = 50;
		_sh = 50;
		// this is for the animations
		currentState = State.MAIN;
		previousState = State.MAIN;
		_animBreakingTimer = 0f;
		// 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("boom.txt"));
		// animated frames
		Array<TextureRegion> frames = new Array<TextureRegion>();
		frames.add(new TextureRegion(atlas.findRegion("boom"), (0 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(atlas.findRegion("boom"), (1 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(atlas.findRegion("boom"), (2 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(atlas.findRegion("boom"), (3 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(atlas.findRegion("boom"), (4 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(atlas.findRegion("boom"), (5 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(atlas.findRegion("boom"), (6 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(atlas.findRegion("boom"), (7 * _sw), (0 * _sh), _sw, _sh));
		main = new Animation<TextureRegion>(.001f, frames, PlayMode.NORMAL);
		frames.clear();
		int rand = MathUtils.random(0, 3);
		if (rand == 0)
			color = Color.GRAY;
		else if (rand == 1)
			color = Color.YELLOW;
		else if (rand == 2)
			color = Color.ORANGE;
		else if (rand == 3)
			color = Color.RED;
		else
			color = new Color(new Color(MathUtils.random(.1f, 1f), MathUtils.random(.1f, 1f), MathUtils.random(.1f, 1f), 1));
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(main.getKeyFrame(_animBreakingTimer, true));
	}

	public void Update(float dt) {
		if (isDead) {
			rect.x = 10000;
			rect.y = 10000;
		} else if (fired) {
			isMain = true;
		}
		if (bulletLife > 0)
			bulletLife--;
		if (bulletLife <= 0)
			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) {
		bulletLife = BULLETLIFE;
		rect.x = _x;
		rect.y = _y;
		isDead = false;
		_animBreakingTimer = 0;
		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);
	}

}
