package effects;

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.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.ld57.game.ddd;

import levelElements.Engine;

public class FireParticle extends Sprite {// this is a very simple, non animated sprite.
	private TextureRegion main;
	private Texture t1;
	public Color color;
	public Rectangle rect;
	private int _sw;// sprite width
	private int _sh;// sprite height
	private int MAXLIFE = MathUtils.random(50, 100);// was 50, 100\\\15, 40
	private int life = MAXLIFE;
	private float speed = .1f;
	private float originSpeed = .1f;
	public boolean isDead;
	private float Xspeed = MathUtils.random(-1f, 1f);

	/*
	 * @_x this is hmmm
	 */
	public FireParticle(float _x, float _y) {
		rect = new Rectangle();
		rect.x = (int) _x;
		rect.y = (int) _y;
		rect.width = 1;
		rect.height = 1;
		_sw = 1;
		_sh = 1;

		// still frames
		t1 = new Texture(Gdx.files.internal("onePixel.png"));
		main = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		color = new Color(MathUtils.random(.1f, 1f), MathUtils.random(.1f, .3f), MathUtils.random(.1f, .3f), 1);
		// color = Color.ORANGE;
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setRegion(main);
	}

	private void update(Engine engine) {
		rect.y += speed * MathUtils.random(.5f, 10f);// was 1, 40
		speed -= MathUtils.random(.01f, .04f);
		rect.x += Xspeed * MathUtils.random(.01f, .5f);// was .01f .5f

//		if (isDead) {
//			// rect.x = 9000;
//			// rect.y = 9000;
//			System.out.println("fire particle isdead: " + isDead);
//		} else {
//			if (life > 0)
//				personToFollow.y += speed;
//			if (life <= 0)
//				isDead = true;
//		}
		// System.out.println("here is the color: " + color);
		if (isDead) {
			rect.x += 0;
			rect.y += 0;
		}
		if (life > 0)
			life -= 2;
		if (life >= MAXLIFE * .8)
			color = Color.YELLOW;
		if (life < MAXLIFE * .8)
			color = Color.GOLD;
		if (life <= MAXLIFE * .6)
			color = Color.ORANGE;
		if (life <= MAXLIFE * .4)
			color = Color.RED;
		if (life <= MAXLIFE * .2)
			color = Color.GRAY;
		if (life <= 0) {
			life = MAXLIFE;
			speed = originSpeed;
			rect.x = engine.rect.x + 22;
			rect.y = engine.rect.y + 10;
			// System.out.println("out of life");
			// System.out.println("x: " + rect.x + " y: " + rect.y);
		}
	}

	public TextureRegion getTextureRegion() {
		return main;
	}

	public void render(ddd game, OrthographicCamera camera, float dt, Engine engine) {
		update(engine);
		game.batch.setColor(color);
		game.batch.draw(getTextureRegion(), rect.x, rect.y);
	}

	public void dispose() {
	}
}
