package zero1hd.polyjet.entity.ally;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;

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

public class PolyJetEntity extends Actor implements Entity {
	public float health;
	private ParticleEffect thrust;
	private Texture polyjet;
	private ParticleEffect teleportCloak;
	private Rectangle hitbox;
	
	public boolean moveLeft, moveRight, moveUp, moveDown, teleporting, accelerate;
	private float speed, accel;
	private float rate;
	public PolyJetEntity(AssetManager assets, float speed, float accel, String jet) {
		health = 100;
		this.speed = speed;
		this.accel = accel;
		setSize(1.5f, 1.5f);
		setPosition(Polyjet.GAME_AREA_WIDTH/2 - getWidth()/2, -4f);
		
		hitbox = new Rectangle(getX(), getY(), getWidth(), getHeight());
		polyjet = assets.get("polyjet-" + jet + ".png", Texture.class);
		thrust = assets.get("standard_thrust.p", ParticleEffect.class);
		thrust.start();
		
		teleportCloak = assets.get("teleport-cloak.p", ParticleEffect.class);
		
		addAction(Actions.moveTo(getX(), 4f, 0.5f));
	}
	
	@Override
	public void act(float delta) {
		hitbox.setPosition(getX(), getY());
		
		thrust.setPosition(getX()+(getWidth())/2 - 1f/16f, getY()-0.2f);
		thrust.update(delta);
		teleportCloak.setPosition(getX() +(getWidth()-1)/2, getY() + (getHeight()-1)/2);
		
		
		//Movement!
		if (accelerate) {
			rate = speed + accel;
		} else {
			rate = speed;
		}
		
		if (moveLeft && !moveRight) {
			moveBy(-(rate*delta), 0);
		}
		if (moveRight && !moveLeft) {
			moveBy(rate*delta, 0);
		}
		if (moveUp && !moveDown) {
			moveBy(0, rate*delta);
		}
		
		if (moveDown && !moveUp) {
			moveBy(0, -rate*delta);
		}

		super.act(delta);
	}
	
	@Override
	public void draw(Batch batch, float parentAlpha) {
		thrust.draw(batch);
		batch.draw(polyjet, getX(), getY(), getWidth(), getHeight());
		super.draw(batch, parentAlpha);
	}

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

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

	@Override
	public void collided(Entity entity) {
	}

	@Override
	public boolean isDead() {
		if (health <= 0) {
			return true;
		} else {
			return false;
		}
	}
}
