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;

public class Halo 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();
	public boolean isCarried;
	public boolean isCarriedByPlayer;
	public boolean isDead;

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

		_sw = 100;
		_sh = 100;
		System.out.println("The halo is for showing you where this is. I hope you can see it. 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("halo.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));
		on = new Animation<TextureRegion>(MathUtils.random(.3f, .4f), frames, PlayMode.LOOP);
		frames.clear();
		color = Color.WHITE;
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(main);
	}

	public TextureRegion getFrame(float dt, OrthographicCamera camera) {
		currentState = getState();
		update();
		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 TextureRegion getFrame(float dt, OrthographicCamera camera, Bread bread) {
		currentState = getState();
		update(bread);
		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() {
		if (isCarriedByPlayer) {
			rect.x = Global.P.x;
			rect.y = Global.P.y - 20;
		}

	}

	private void update(Bread bread) {
		if (isCarriedByPlayer) {
			rect.x = Global.P.x;
			rect.y = Global.P.y - 20;
		} else {
			rect.x = bread.rect.x - 20;
			rect.y = bread.rect.y;
		}

	}

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

	public void render(rcr game, OrthographicCamera camera, float dt, Bread bread) {
		game.batch.setColor(color);
		game.batch.draw(getFrame(dt, camera, bread), rect.x, rect.y);
	}

	public void dispose() {
	}
}
