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.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 Person extends Sprite {
	public enum State {
		ALIVE, DEAD
	};

	public State currentState;
	public State previousState;
	private Texture t1;
	private TextureRegion alive;
	private TextureRegion dead;
	public Color color;
	public Rectangle rect;
	private int _sw;// sprite width
	private int _sh;// sprite height
	public boolean isDead;
	public boolean isCarried;
	public boolean isCarriedByPlayer;
	public UUID uuid = UUID.randomUUID();
	Worker w = new Worker(0, 0);
	public boolean isDying;
	private float angle;

	public Person(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("person.png"));
		alive = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		dead = new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh);
		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(alive);
	}

	// 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 + 10;
				rect.y = w.rect.y - 4;
			}
			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;
			}
		}

		TextureRegion region;
		switch (currentState) {
		case ALIVE:
			region = alive;
			break;
		case DEAD:
			region = dead;
			break;
		default:
			region = alive;
			break;
		}

		return region;
	}

	public State getState() {
		if (isDead)
			return State.DEAD;
		else
			return State.ALIVE;
	}

	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() {
	}
}
