package zero1hd.polyjet.entity.enemies;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Pool.Poolable;

import zero1hd.polyjet.entity.Entities;
import zero1hd.polyjet.entity.Entity;

public class VoidCircle extends Actor implements Entity, Poolable {
	private float timer;
	private float endRadius;
	private float currentRadius;
	private Rectangle hitBox;
	private Sprite voidCircleTexture;
	private float growthRate;
	private boolean done;
	private boolean begin;
	private Vector2 center;
	
public VoidCircle(Texture voidTexture) {
		hitBox = new Rectangle();
		center = new Vector2();
		voidCircleTexture = new Sprite(voidTexture);
	}
	
	public void init(float endRadius, float x, float y, float growthRate, float warningTime) {
		timer = warningTime;
		this.endRadius = endRadius;
		setSize(2f*endRadius, 2f*endRadius);
		center.set(getWidth()/2f, getHeight()/2f);
		setPosition(x-center.x, y-center.y);
		this.growthRate = growthRate;
	}
	
	@Override
	public void act(float delta) {
		toFront();
		if (begin) {
			voidCircleTexture.setSize(2*currentRadius, 2*currentRadius);
			voidCircleTexture.setColor(0f,0f,0f,1f);
		} else {
			voidCircleTexture.setSize(2*endRadius, 2*endRadius);
			voidCircleTexture.setColor(1f,1f,1f,0.1f);
		}		hitBox.setCenter(getX()+center.x, getY()+center.y);
		
		if (timer > 0) {
			timer -= delta;
		} else {
			begin = true;
			if (currentRadius < endRadius) {
				growCurrentRadius(delta*growthRate);
			} else {
				endRadius = -1f;
				if (currentRadius > 0) {
					growCurrentRadius(delta*-3*growthRate);
				} else {
					done = true;
				}
			}
		}
		voidCircleTexture.setCenter(getX()+center.x, getY()+center.y);
		super.act(delta);
	}
	
	
	@Override
	public void draw(Batch batch, float parentAlpha) {
		voidCircleTexture.draw(batch);
		batch.setColor(Color.WHITE);
		super.draw(batch, parentAlpha);
	}
	
	@Override
	public void reset() {
		hitBox.set(0, 0, 0, 0);
		currentRadius = 0;
		growthRate = 0;
		timer = 0;
		endRadius = 0;
		done = false;
		begin = false;
		center.set(0, 0);
		voidCircleTexture.setPosition(0, 0);
		setSize(0, 0);
		setPosition(0, 0);
	}
	
	public void growCurrentRadius(float radius) {
		currentRadius += radius;
		float length = (float) Math.sqrt(2*(currentRadius*currentRadius));
		hitBox.setSize(length, length);
	}
	
	@Override
	public Rectangle getHitZone() {
		return hitBox;
	}

	@Override
	public Entities getEntityType() {
		return Entities.VOID_CIRCLE;
	}

	@Override
	public void collided(Entity entity) {
	}

	@Override
	public boolean isDead() {
		return done;
	}
}
