package entities;

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

import java.util.UUID;

public class Plant extends Sprite {
	public enum State {
		INGROUND, CARRIED, DYINGINGROUND, DYINGCARRIED
	};

	public State currentState;
	public State previousState;
	private Texture t1;
	private TextureRegion inGround;
	private TextureRegion carried;
	private Animation<TextureRegion> dyingInGround;
	private Animation<TextureRegion> dyingCarried;
	public Color color;
	public Rectangle rect;
	private int _sw;// sprite width
	private int _sh;// sprite height
	private boolean isAnimationOver;
	private float _stateTimer;
	public boolean isCarried;
	// sfx
	public boolean isCarriedByPlayer;
	public boolean isDead;
	public UUID uuid = UUID.randomUUID();
	Worker w = new Worker(0, 0);
	public boolean isDying;
	private float angle;

	public Plant(float _x, float _y) {
		rect = new Rectangle();
		rect.x = (int) _x;
		rect.y = (int) _y;
		rect.width = 32;
		rect.height = 32;

		_sw = 32;
		_sh = 32;

		// still frames
		t1 = new Texture(Gdx.files.internal("plant.png"));
		inGround = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		carried = new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh);
		Array<TextureRegion> frames = new Array<TextureRegion>();
		frames.add(new TextureRegion(t1, (2 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (3 * _sw), (0 * _sh), _sw, _sh));
		dyingInGround = new Animation<TextureRegion>(.2f, frames, PlayMode.LOOP);
		frames.clear();
		frames.add(new TextureRegion(t1, (4 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (5 * _sw), (0 * _sh), _sw, _sh));
		dyingCarried = new Animation<TextureRegion>(.2f, frames, PlayMode.LOOP);
		color = Color.WHITE;
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(inGround);
	}

	// for all the animations: //this isn't even updated unless i call it from the other screen oh bother. *facepalm*
	public TextureRegion getFrame(Array<Worker> workers, Player player, float dt) {
		currentState = getState();
		if (!isDead) {
			for (int i = 0; i < workers.size; i++) {
				if (workers.get(i).uuid == uuid)
					w = workers.get(i);
			}
			if (isCarried) {
				if (w.isDead)
					isCarried = false;
				rect.x = w.rect.x;
				rect.y = w.rect.y + 25;
			}
			if (isCarriedByPlayer) {
				rect.x = player.rect.x + player.swirlingObjects * MathUtils.cos(angle + MathUtils.random(.1f, .5f)) / 4;
				rect.y = player.rect.y + player.swirlingObjects + player.swirlingObjects / 4 * MathUtils.sin(angle + MathUtils.random(.1f, .5f)) * 4;
				angle += .06f;
				if (angle >= 360)
					angle = 1;
			}
		}

		if (isDying)
			isDead = true;

		TextureRegion region;
		switch (currentState) {
		case INGROUND:
			region = inGround;
			break;
		case CARRIED:
			region = carried;
			break;
		case DYINGINGROUND:
			region = dyingInGround.getKeyFrame(_stateTimer, false);
			break;
		case DYINGCARRIED:
			region = dyingCarried.getKeyFrame(_stateTimer, false);
			break;
		default:
			region = inGround;
			break;
		}
		if (currentState != previousState)
			isAnimationOver = false;

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

		return region;
	}

	public State getState() {// behavior
		// if a worker collides with this object, they can pick it up. This just says if they picked you up.
		if (isCarried && isDead)
			return State.DYINGCARRIED;
		else if (isCarried)
			return State.CARRIED;
		else if (isDead)
			return State.DYINGINGROUND;
		else
			return State.INGROUND;
	}

	public void render(Agent game, Array<Worker> workers, Player player, float dt) {// hmmm i think it just needs placed before? it gets here.
		game.batch.setColor(color);
		game.batch.draw(getFrame(workers, player, dt), rect.x, rect.y);
	}

	public void dispose() {
	}
}
