package levelElements;

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;

public class DirtChunk extends Sprite {// this is a very simple, non animated sprite. Is this the lightest thing I have?
	private TextureRegion main;
	private TextureRegion main2;
	private TextureRegion main3;
	private TextureRegion main4;
	private TextureRegion main5;
	private TextureRegion main6;
	private TextureRegion main7;
	private TextureRegion main8;
	private TextureRegion main9;
	private TextureRegion main10;
	private TextureRegion texture;
	private Texture t1;
	public Color color;
	public Rectangle rect;
	private int _sw;// sprite width
	private int _sh;// sprite height
	public boolean isDead;
	private float timer = .02f;
	private float angle = 1f;
	private float angleSpeed = .01f;
	private float widthX;
	private float heightY;
	private float speed;
	private float originX;
	private float originY;
	private float originSpeed;
	private float ovalIncreaserX;
	private float originOvalIncreaserX;
	private float ovalIncreaserY;
	private float originOvalIncreaserY;

	public DirtChunk(float _x, float _y) {
		rect = new Rectangle();
		rect.x = (int) _x;
		rect.y = (int) _y;
		rect.width = 25;
		rect.height = 25;
		_sw = 50;
		_sh = 50;
		// still frames
		t1 = new Texture(Gdx.files.internal("dirt.png"));
		main = new TextureRegion(t1, (0 * _sw), (0 * _sh), _sw, _sh);
		main2 = new TextureRegion(t1, (1 * _sw), (0 * _sh), _sw, _sh);
		main3 = new TextureRegion(t1, (2 * _sw), (0 * _sh), _sw, _sh);
		main4 = new TextureRegion(t1, (3 * _sw), (0 * _sh), _sw, _sh);
		main5 = new TextureRegion(t1, (0 * _sw), (1 * _sh), _sw, _sh);
		main6 = new TextureRegion(t1, (1 * _sw), (1 * _sh), _sw, _sh);
		main7 = new TextureRegion(t1, (2 * _sw), (1 * _sh), _sw, _sh);
		main8 = new TextureRegion(t1, (3 * _sw), (1 * _sh), _sw, _sh);
		main9 = new TextureRegion(t1, (0 * _sw), (2 * _sh), _sw, _sh);
		main10 = new TextureRegion(t1, (1 * _sw), (2 * _sh), _sw, _sh);
		color = new Color(MathUtils.random(.1f, .2f), MathUtils.random(.05f, .1f), MathUtils.random(.001f, .002f), 1);
		setBounds(0, 0, _sw, _sh);
		setOriginCenter();
		setTheRegion();// picks a random region!
		originX = widthX = MathUtils.random(300f, 350f);
		originY = heightY = MathUtils.random(300f, 340f);
		originSpeed = speed = MathUtils.random(.1f, 4f);
		angle = MathUtils.random(1f, 360f);
		originOvalIncreaserX = ovalIncreaserX = MathUtils.random(1.2f, 1.5f);
		originOvalIncreaserY = ovalIncreaserY = MathUtils.random(1.2f, 1.5f);
	}

	private void update() {
		rect.x = timer + widthX + 250 * MathUtils.cos(angle) * ovalIncreaserX;
		rect.y = timer + heightY + 200 * MathUtils.sin(angle) * ovalIncreaserY;
		angle += angleSpeed;
		if (angle >= 360)
			angle = 1;
		timer += speed;
		if (angleSpeed < .01f)
			angleSpeed += .02f;
		if (ovalIncreaserX < 5.0f)
			ovalIncreaserX += .01f;
		if (ovalIncreaserY < 5.0f)
			ovalIncreaserY += .04f;

		if (rect.x < -206 | rect.x > ddd.MW | rect.y < -206 | rect.y > ddd.MH) {
			widthX = originX;
			heightY = originY;
			speed = originSpeed;
			ovalIncreaserX = originOvalIncreaserX;
			ovalIncreaserY = originOvalIncreaserY;
			timer = 0;
			angleSpeed = .01f;
		}

	}

	private void setTheRegion() {
		int randomTexture = MathUtils.random(1, 10);
		if (randomTexture == 1)
			texture = main;
		else if (randomTexture == 2)
			texture = main2;
		else if (randomTexture == 3)
			texture = main3;
		else if (randomTexture == 4)
			texture = main4;
		else if (randomTexture == 5)
			texture = main5;
		else if (randomTexture == 6)
			texture = main6;
		else if (randomTexture == 7)
			texture = main7;
		else if (randomTexture == 8)
			texture = main8;
		else if (randomTexture == 9)
			texture = main9;
		else if (randomTexture == 10)
			texture = main10;
		else
			texture = main;

	}

	public TextureRegion getTextureRegion() {
		return texture;
	}

	public void render(ddd game, OrthographicCamera camera, float dt) {// hmmm i think it just needs placed before? it gets here.
		update();
		game.batch.setColor(color);
		game.batch.draw(getTextureRegion(), rect.x, rect.y);
	}

	public void dispose() {
	}
}
