package unearthables;

import java.util.UUID;

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.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;
import com.ld58.game.rcr;

import entities.Student;

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

	public State currentState;
	public State previousState;
	private Animation<TextureRegion> on;
	private float _stateTimer;
	private boolean isAnimationOver;
	private TextureRegion main;
	private Texture t1;
	public Color color;
	public Rectangle rect;
	private int _sw;// sprite width
	private int _sh;// sprite height
	public boolean isDead;
	public boolean isCarried;
	public Student st = new Student(0, 0);
	public UUID uuid = UUID.randomUUID();

	public Topaz(float _x, float _y) {
		rect = new Rectangle();
		rect.x = (int) _x;
		rect.y = (int) _y;
		rect.width = 15;
		rect.height = 15;
		_sw = 15;
		_sh = 15;
		// still frames
		t1 = new Texture(Gdx.files.internal("topaz.png"));
		main = new TextureRegion(t1, (0 * _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, (1 * _sw), (0 * _sh), _sw, _sh));
		on = new Animation<TextureRegion>(MathUtils.random(.1f, .4f), frames, PlayMode.LOOP);
		frames.clear();
		color = Color.WHITE;
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(main);
	}

	private void Update(Array<Student> students, float dt) {
		if (!isDead) {
			for (int i = 0; i < students.size; i++) {
				if (students.get(i).uuid == uuid)
					st = students.get(i);
			}
			if (isCarried) {
				if (st.isDead)
					isCarried = false;
				rect.x = st.rect.x + 20;
				rect.y = st.rect.y + 50;
			}
//			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;
//			}
		}
	}

	public TextureRegion getFrame(OrthographicCamera camera, float dt, Array<Student> students) {
		Update(students, dt);
		currentState = getState();

		TextureRegion region;
		switch (currentState) {
		case ON:
			region = on.getKeyFrame(_stateTimer, true);
			break;
		case MAIN:
			region = main;
			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 void render(rcr game, OrthographicCamera camera, float dt, Array<Student> students) {// hmmm i think it just needs placed before? it gets here.
		game.batch.setColor(color);
		game.batch.draw(getFrame(camera, dt, students), rect.x, rect.y);
	}

	public void dispose() {
	}
}
