package coolbot;

import javax.media.opengl.GL;

import devUtil.Vector3;

import object.Model;
import util.MP3;
import world.GameObject;

public class Coolbot extends GameObject {

	private Model bot;

	private float height;
	private float seaLevel;
	
	private int coolDownTime;
	
	private float speed;
	private float personalSpace;
	
	public Coolbot() {

		bot = new Model();

		bot.parseObjectFile(
				ClassLoader.getSystemResourceAsStream(
						"visual/models/Poly Bot.obj"));

		bot.setSize(15, 65, 15);

		bot.setColor(.25f, .3f, .3f);
		bot.setPosition(0, 0, 0);
		
		coolDownTime = (int) (Math.random() * 100 + 100);
		
		speed = (float) (Math.random() * 2 + 1);
		personalSpace = (float) (Math.random() * 150 + 75);
	}

	public Coolbot(float xPos, float zPos) {

		bot = new Model();

		bot.parseObjectFile(
				ClassLoader.getSystemResourceAsStream(
						"visual/models/Poly Bot.obj"));

		bot.setSize(15, 65, 15);

		bot.setColor(.25f, .3f, .3f);
		bot.setPosition(xPos, 0, zPos);
		
		coolDownTime = (int) (Math.random() * 100 + 100);
		
		speed = (float) (Math.random() * 2 + 1);
		personalSpace = (float) (Math.random() * 150 + 75);
	}

	public void render(GL gl) {
		bot.render(gl);
	}

	private int time;

	public void update() {

		time++;

		float xSpace = (float) (bot.getXPosition() - camera.getXPosition());
		float ySpace = (float) (bot.getYPosition() + 30 - camera.getYPosition());
		float zSpace = (float) (bot.getZPosition() - camera.getZPosition());

		float space = (float) Math.sqrt((xSpace * xSpace) + (zSpace * zSpace));

		faceTarget(xSpace, zSpace, space);
		chaseTarget(xSpace, zSpace, space);

		if(time % coolDownTime == 0 && space < 300)
			shootTarget(-xSpace, -ySpace, -zSpace);

		if(height + 80 < seaLevel && seaLevel != 0) {
			
			Explosion explosion = new Explosion();
			explosion.setExplosion(bot.getPosition(), bot.getXSize(), 75);
			
			spawn.add(explosion);
			
			destroy();
		}
	}

	private float rotation;

	private void faceTarget(float xSpace, float zSpace, float space) {

		float targetRotation = (float) Math.toDegrees(Math.asin(zSpace / space));

		if(rotation < targetRotation) {

			rotation += 1;

			if(rotation > targetRotation)
				rotation = targetRotation;
		}

		if(rotation > targetRotation) {

			rotation -= 1;

			if(rotation < targetRotation)
				rotation = targetRotation;
		}

		if(xSpace <= 0)
			bot.setRotation(0, rotation + 90, 0);

		else
			bot.setRotation(0, -rotation - 90, 0);
	}

	private void chaseTarget(float xSpace, float zSpace, float space) {

		if(space > 125) {

			bot.setPosition(
					bot.getXPosition() - (xSpace / space) * speed,
					bot.getYPosition(),
					bot.getZPosition() - (zSpace / space) * speed
					);
		}

		bot.setPosition(bot.getXPosition(), height + 45, bot.getZPosition());
	}

	private void shootTarget(float xSpace, float ySpace, float zSpace) {

		Vector3 trajectory = new Vector3(xSpace, ySpace, zSpace);
		trajectory = trajectory.getUnitVector();

		Bullet bullet = new Bullet(
				bot.getXPosition(), bot.getYPosition() + 30, bot.getZPosition(),
				trajectory.getX() * 25, trajectory.getY() * 25, trajectory.getZ() * 25);

		bullet.setAlliance("Coolbot");

		spawn.add(bullet);
	}
	
	public void move(Vector3 move) {
		
		bot.setPosition(
				(float) (bot.getXPosition() + move.getX()),
				(float) (bot.getYPosition() + move.getY()),
				(float) (bot.getZPosition() + move.getZ()));
	}
	
	public Vector3 getPosition() {
		return bot.getPosition();
	}

	private int health = 5;

	public void interact(GameObject object) {

		if(object instanceof Map) {

			height = (float) ((Map) object).getHeightAt(
					bot.getXPosition(), bot.getZPosition()) - 45;
			
			seaLevel = (float) ((Map) object).getSeaLevel();
		}

		if(object instanceof Bullet)

			if(!((Bullet) object).getAlliance().equals("Coolbot")){

				Vector3 vector = new Vector3(bot.getXPosition(), bot.getYPosition() + 30, bot.getZPosition());
				vector.subtract(((Bullet) object).getPosition());

				if(vector.getMagnitude() < 50) {

					health--;

					new MP3("/audio/FX/Drop Sword.mp3").play(1);
					object.destroy();
				}

				if(health == 0) {
					
					Explosion explosion = new Explosion();
					explosion.setExplosion(bot.getPosition(), bot.getXSize(), 75);
					
					spawn.add(explosion);
					
					destroy();
				}

				else
					bot.setColor(1, 1 - ((5 - health) * .2f), 1 - ((5 - health) * .2f));
			}
		
		if(object instanceof Coolbot) {
			
			Vector3 space = getPosition();
			space.subtract(((Coolbot) object).getPosition());
			
			if(space.getMagnitude() < personalSpace) {
				move(new Vector3((space.getX() / 50), (space.getY() / 50), (space.getZ() / 50)));
				//((Coolbot) object).move(new Vector3((space.getX() / 50), (space.getY() / 50), (space.getZ() / 50)));
			}
		}
	}
}