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.math.MathUtils;
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.Polyjet;
import zero1hd.polyjet.entity.Entities;
import zero1hd.polyjet.entity.Entity;

public class Pellet extends Actor implements Entity, Poolable {
	private boolean dead;
	private Texture texture;
	private Rectangle hitBox;
	private Vector2 direction;
	private float rate;
	
	public Pellet(Texture texture) {
		hitBox = new Rectangle();
		direction = new Vector2();
		this.texture = texture;
	}
	
	public void init(float x, float y, float angle, float rate) {
		setPosition(x, y);
		direction.set(MathUtils.sinDeg(angle), MathUtils.cosDeg(angle));
		setSize(0.75f, 0.75f);
		hitBox.setSize(getWidth(), getHeight());
		this.rate = rate;
	}
	
	@Override
	public void act(float delta) {
		moveBy(direction.x*delta*rate, direction.y*delta*rate);
		hitBox.setPosition(getX(), getY());
		super.act(delta);
		
		if (getX() > Polyjet.GAME_AREA_WIDTH || getY() > Polyjet.GAME_AREA_HEIGHT || getX() < 0-getWidth() || getY() < 0-getHeight()) {
			dead = true;
		}
	}
	
	@Override
	public void draw(Batch batch, float parentAlpha) {
		batch.setColor(getColor().r, getColor().g, getColor().b, getColor().a);
		batch.draw(texture, getX(), getY(), getWidth(), getHeight());
		batch.setColor(Color.WHITE);
		super.draw(batch, parentAlpha);
	}
	
	@Override
	public void collided(Entity entity) {
		switch (entity.getEntityType()) {
		default:
			dead = true;
			break;
		}
	}

	@Override
	public Rectangle getHitZone() {
		return hitBox;
	}

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

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

	@Override
	public void reset() {
		hitBox.set(0, 0, 0, 0);
		setSize(0, 0);
		setPosition(0, 0);
		direction.set(0,0);
		rate = 0;
		dead = false;
		setColor(1f, 1f, 1f, 1f);
	}

}
