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.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 Shard extends Actor implements Entity, Poolable {
	private Sprite sprite;
	private Rectangle hitbox;
	private Vector2 angle;
	private Vector2 center;
	private int hp;
	private int maxHp;
	private float rate;
	
	public Shard(Texture shardTexture) {
		hitbox = new Rectangle();
		angle = new Vector2();
		sprite = new Sprite(shardTexture);
		sprite.setSize(2f, 3f);
		center = new Vector2();
	}
	
	public void init(float x, float y, float angle, float rate, int hp) {
		this.rate = rate;
		this.hp = hp;
		maxHp = hp;
		this.angle.set(MathUtils.sinDeg(angle), MathUtils.cosDeg(angle));
		sprite.setRotation(-angle);
		setSize(2f, 2f);
		hitbox.setSize(getWidth(), getHeight());
		center.set(getWidth()/2f, getHeight()/2f);
		setPosition(x-center.x, y-center.y);
		sprite.setOrigin(sprite.getWidth()/2f, sprite.getHeight()/2f);
	}
	
	@Override
	public void reset() {
		hp = 0;
		maxHp = 0;
		setPosition(0, 0);
		angle.set(0, 0);
		center.set(0, 0);
		setSize(0, 0);
		hitbox.set(0, 0, 0, 0);
		setSize(0, 0);
		sprite.setRotation(0);
	}
	
	@Override
	public void act(float delta) {
		moveBy(angle.x*delta*rate, angle.y*rate*delta);
		hitbox.setPosition(getX(), getY());
		
		if (getX() > Polyjet.GAME_AREA_WIDTH || getY() > Polyjet.GAME_AREA_HEIGHT || getX() < 0-getWidth() || getY() < 0-getHeight()) {
			hp = 0;
		}
		super.act(delta);
	}
	
	@Override
	public void draw(Batch batch, float parentAlpha) {
		sprite.setCenter(getX()+center.x, getY()+center.y);
		sprite.setColor(((float)hp/(float)maxHp), ((float)hp/(float)maxHp), ((float)hp/(float)maxHp), 0.5f);
		sprite.draw(batch);
		batch.setColor(Color.WHITE);
		super.draw(batch, parentAlpha);
	}
	
	@Override
	public void collided(Entity entity) {
		switch (entity.getEntityType()) {
		case LASER:
			hp--;
			break;
		default:
			break;
		}
	}

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

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

	@Override
	public boolean isDead() {
		if (hp <= 0) {
			return true;
		}
		return false;
	}
	
	public void setRate(float rate) {
		this.rate = rate;
	}
	
	public Vector2 getCenter() {
		return center;
	}

}
