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;

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

	public State currentState;
	public State previousState;
	private Texture t1;
	private TextureRegion paused;
	private Animation<TextureRegion> main;
	public Color color;
	public Rectangle rect;
	private int _sw;// sprite width
	private int _sh;// sprite height
	private boolean isAnimationOver;
	private float _stateTimer;
	// sfx
	private boolean isDead;
	public Vector2 dir;
	public int speed;
	public boolean isMoving;

	public Harvester(float _x, float _y) {
		rect = new Rectangle();
		rect.x = (int) _x;
		rect.y = (int) _y;
		rect.width = 400;
		rect.height = 300;

		_sw = 400;
		_sh = 300;

		// still frames
		t1 = new Texture(Gdx.files.internal("harvester.png"));
		paused = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		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>(.2f, frames, PlayMode.LOOP);
		color = Color.WHITE;
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(main.getKeyFrame(_stateTimer, true));
	}

	// for all the animations: //this isn't even updated unless i call it from the other screen oh bother. *facepalm*
	public TextureRegion getFrame(OrthographicCamera camera, float dt) {
		currentState = getState();
		if (isDead) {
			// rect.x = 9000;
			// rect.y = 9000;
			// System.out.println("isdead: " + isDead);
		} else {
			if (isMoving) {
				System.out.println("dasfasdfasdfead: " + isDead);
				rect.x += dir.x * speed * dt;
				rect.y += dir.y * speed * dt;
			}
		}

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

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

		return region;
	}

	public State getState() {
		if (!isDead)
			return State.MAIN;
		else
			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() {
	}
}
