package levelElements;

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.utils.Array;
import com.ld57.game.ddd;

import Global.Global;
import effects.HealthBar;

public class Drill extends Sprite {// this is a very simple, non animated sprite. Is this the lightest thing I have?
	public enum State {
		MAIN, BROKEN, ON
	};

	public State currentState;
	public State previousState;
	private Animation<TextureRegion> on;
	private float _stateTimer;
	private TextureRegion main;
	private TextureRegion broken;
	private Texture t1;
	public Color color;
	public Rectangle rect;
	public boolean isBroken;
	private int _sw;// sprite width
	private int _sh;// sprite height
	private boolean isAnimationOver;
	public HealthBar healthBar;

	public Drill(float _x, float _y) {
		rect = new Rectangle();
		rect.x = (int) _x;
		rect.y = (int) _y;
		rect.width = 25;
		rect.height = 25;
		_sw = 25;
		_sh = 25;
		healthBar = new HealthBar(_x, _y, _sw, 2);
		// still frames
		t1 = new Texture(Gdx.files.internal("drill.png"));
		main = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		broken = new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh);
		// animated frames
		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));
		frames.add(new TextureRegion(t1, (2 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (3 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (4 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (5 * _sw), (0 * _sh), _sw, _sh));
		on = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		frames.clear();
		color = Color.WHITE;
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(main);
	}

	private void update() {
		rect.x = Global.P.x - 70;
		rect.y = Global.P.y;
	}

	public TextureRegion getFrame(OrthographicCamera camera, float dt) {
		update();
		currentState = getState();

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

		if (!isAnimationOver && currentState == previousState) {
			_stateTimer += dt;
		} else
			_stateTimer = 0;
		// System.out.println("statetimer: " + _stateTimer);
		previousState = currentState;

		return region;
	}

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

	public TextureRegion getTextureRegion() {
		if (isBroken)
			return broken;
		else
			return main;
	}

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