package levelElements;

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 Global.Global;
import entities.Student;

public class Bread extends Sprite {// simple animated sprite
	public enum State {
		MAIN, ON
	};

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

	/*
	 * @_x this is simple
	 */
	public Bread(float _x, float _y) {
		rect = new Rectangle();
		rect.x = (int) _x;
		rect.y = (int) _y;
		rect.width = 50;
		rect.height = 50;

		_sw = 50;
		_sh = 50;
		System.out.println("The bread still lives--> x: " + _x + "y: " + _y);
		// this is for the animations
		currentState = State.MAIN;
		previousState = State.ON;
		_stateTimer = 0f;
		// still frames
		t1 = new Texture(Gdx.files.internal("bread.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, (3 * _sw), (0 * _sh), _sw, _sh));
		frames.add(new TextureRegion(t1, (4 * _sw), (0 * _sh), _sw, _sh));
		on = new Animation<TextureRegion>(MathUtils.random(.3f, .4f), frames, PlayMode.LOOP);
		frames.clear();
		color = new Color(MathUtils.random(.8f, .9f), MathUtils.random(.7f, .8f), MathUtils.random(.5f, .6f), 1);
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(main);
	}

	public TextureRegion getFrame(float dt, OrthographicCamera camera, Array<Student> students) {
		currentState = getState();
		update(students, dt);
		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;
	}

	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);
			}
			// System.out.println("is carried" + isCarried);
			if (isCarried) {
				if (st.isDead) {
					isCarried = false;
				}
				rect.x = st.rect.x + 10;
				rect.y = st.rect.y + 50;
			}
			if (isCarriedByPlayer) {
				rect.x = Global.P.x;
				rect.y = Global.P.y - 20;
			}
		}
	}

	public State getState() {
		if (isDead)
			return State.MAIN;
		else
			return State.ON;
	}

	public void render(rcr game, OrthographicCamera camera, float dt, Array<Student> students) {
		game.batch.setColor(color);
		game.batch.draw(getFrame(dt, camera, students), rect.x, rect.y);
	}

	public void dispose() {
	}
}
