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 Kitten extends Sprite {
	public enum State {
		STANDLEFT, STANDRIGHT, WALKLEFT, WALKRIGHT, CARRIED, DYING
	};

	public State currentState;
	public State previousState;
	private Texture t1;
	private TextureRegion carried;
	private TextureRegion dying;
	private TextureRegion standLeft;
	private TextureRegion standRight;
	private Animation<TextureRegion> walkLeft;
	private Animation<TextureRegion> walkRight;
	public Color color;
	public Rectangle rect;
	private int _sw;// sprite width
	private int _sh;// sprite height
	private boolean isAnimationOver;
	private float _stateTimer;
	public boolean isDead;
	// sfx
	public boolean isCarried;
	private boolean l;
	private boolean r;
	private boolean u;
	private boolean d;
	private boolean lmove;
	private boolean rmove;
	private float timer;
	private float speedX;
	private float speedY;
	public UUID uuid = UUID.randomUUID();
	public boolean isCarriedByPlayer;
	private Worker w = new Worker(0, 0);
	private float angle;

	public Kitten(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("kitten.png"));
		dying = new TextureRegion(t1, (3 * _sw), (0 * _sh), _sw, _sh);
		carried = new TextureRegion(t1, (2 * _sw), (0 * _sh), _sw, _sh);
		standLeft = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		TextureRegion rr = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		rr.flip(true, false);
		standRight = rr;
		Array<TextureRegion> frames = new Array<TextureRegion>();
		TextureRegion r = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		TextureRegion r2 = new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh);
		r.flip(true, false);
		r2.flip(true, false);
		frames.add(r);
		frames.add(r2);
		walkRight = new Animation<TextureRegion>(.1f, frames, PlayMode.LOOP);
		frames.clear();
		frames.add(new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh));
		walkLeft = new Animation<TextureRegion>(.2f, frames, PlayMode.LOOP);
		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(walkLeft.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(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) {
				// System.out.println("objects swirling: " + player.swirlingObjects);
				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 (timer <= 0 && !isCarried && !isCarriedByPlayer) {
				timer = MathUtils.random(10f, 300f);
				int rand = MathUtils.random(1, 6);
				if (rand == 1) {// left
					lmove = true;
					rmove = false;
					speedX = -MathUtils.random(.5f, 1f);
					speedY = 0;
				} else if (rand == 2) {// right
					lmove = false;
					rmove = true;
					speedX = MathUtils.random(.5f, 1f);
					speedY = 0;
				} else if (rand == 3) {// up
					lmove = false;
					rmove = true;
					speedX = 0;
					speedY = -MathUtils.random(.5f, 1f);
				} else if (rand == 4) {// down
					lmove = true;
					rmove = false;
					speedX = 0;
					speedY = MathUtils.random(.5f, 1f);
				} else if (rand == 5 || rand == 6) {// random
					int r = MathUtils.random(1, 2);
					if (r == 1) {
						lmove = true;
						rmove = false;
					} else {
						lmove = true;
						rmove = false;
					}
					speedX = MathUtils.random(.1f, 1f);
					speedY = MathUtils.random(.1f, 1f);
				}
			}
			// System.out.println("rect.x: " + rect.x + " rect.y: " + rect.y + " Agent.MW: " + (Agent.MW / 2 - _sw) + " Agent.MH: " + Agent.MH);
			if (rect.x > -Agent.MW / 2 - _sw && rect.x < Agent.MW + _sw && rect.y > -Agent.MH - _sh && rect.y < Agent.MH - _sh) {
				// System.out.println("we made it INNNNNN");
				rect.x += speedX;
				rect.y += speedY;
			} else if (rect.x > -Agent.MW / 2 - _sw)
				rect.x--;
			else if (rect.x < Agent.MW + _sw)
				rect.x--;
			else if (rect.y > -Agent.MH - _sh)
				rect.y++;
			else if (rect.y < Agent.MH - _sh)
				rect.y--;
			if (timer > 0)
				timer -= MathUtils.random(.001f, 2f);
		}

		TextureRegion region;
		switch (currentState) {
		case STANDLEFT:
			region = standLeft;
			break;
		case STANDRIGHT:
			region = standRight;
			break;
		case CARRIED:
			region = carried;
			break;
		case DYING:
			region = dying;
			break;
		case WALKLEFT:
			region = walkLeft.getKeyFrame(_stateTimer, false);
			break;
		case WALKRIGHT:
			region = walkRight.getKeyFrame(_stateTimer, false);
			break;
		default:
			region = standLeft;
			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 (isDead)
			return State.DYING;
		else if (isCarried)
			return State.CARRIED;
		else if (l)
			return State.STANDLEFT;
		else if (r)
			return State.STANDRIGHT;
		else if (lmove)
			return State.WALKLEFT;
		else if (rmove)
			return State.WALKRIGHT;
		else
			return State.STANDLEFT;
	}

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