package coolbot;

import javax.media.opengl.GL;

import object.Model;

import devUtil.Vector3;

import util.WAV;
import world.GameObject;

@SuppressWarnings("unused")
public class Bullet extends GameObject {
	
	private Model bullet;
	
	private double xPos;
	private double yPos;
	private double zPos;
	
	private double xVelocity;
	private double yVelocity;
	private double zVelocity;
	
	private double time;
	
	private double height;
	
	private String alliance;
	
	public Bullet(double xPos, double yPos, double zPos,
			double xVelocity, double yVelocity, double zVelocity) {
		
		this.xPos = xPos;
		this.yPos = yPos;
		this.zPos = zPos;
		
		this.xVelocity = xVelocity;
		this.yVelocity = yVelocity;
		this.zVelocity = zVelocity;
		
		bullet = new Model();
		bullet.parseObjectFile(ClassLoader.getSystemResourceAsStream("visual/models/Bullet.obj"));
		
		bullet.setSize(-5f, .5f, .5f);
		
		alliance = "";
	}
	
	float xRotation = Float.MIN_VALUE;
	float yRotation = Float.MIN_VALUE;
	float zRotation = Float.MIN_VALUE;
	
	public void update() {
		
		xPos += xVelocity;
		yPos += yVelocity;
		zPos += zVelocity;
		
		time++;
		
		if(time == 1)
			new WAV("/audio/FX/Gunshot.wav").play(1);
		
		if(time == 200)
			destroy();
		
//		if(yPos < height)
//			destroy();
		
		if(xPos < -4850)
			destroy();
		
		if(zPos < -4850)
			destroy();
		
		if(xPos > 4850)
			destroy();
		
		if(zPos > 4850)
			destroy();
		
//		if(xRotation == Float.MIN_VALUE) {
//			
//			Vector3 trajectory = new Vector3(
//					xVelocity,
//					yVelocity,
//					zVelocity);
//			
//			trajectory = trajectory.getUnitVector();
//			
//			xRotation = (float) Math.toDegrees(Math.acos(trajectory.getY() / 1));
//			
//			if(camera.getYView() >= 0)
//				xRotation = -xRotation - 180;
//		}
		
		if(yRotation == Float.MIN_VALUE) {
			
			Vector3 trajectory = new Vector3(
					xVelocity,
					yVelocity,
					zVelocity);
			
			trajectory = trajectory.getUnitVector();
			
			yRotation = (float) Math.toDegrees(Math.asin(trajectory.getZ() / 1));
			
			if(trajectory.getX() >= 0)
				yRotation = -yRotation - 180;
		}
		
//		if(zRotation == Float.MIN_VALUE) {
//			
//			Vector3 trajectory = new Vector3(
//					xVelocity,
//					yVelocity,
//					zVelocity);
//			
//			trajectory = trajectory.getUnitVector();
//			
//			zRotation = (float) Math.toDegrees(Math.asin(trajectory.getY() / 1));
//			
//			if(camera.getYView() >= 0)
//				zRotation = -zRotation - 180;
//		}
		
		bullet.setRotation(xRotation, yRotation, zRotation);
	}
	
	public void setAlliance(String alliance) {
		this.alliance = alliance;
	}
	
	public String getAlliance() {
		return alliance;
	}
	
	public void render(GL gl) {
		
		bullet.setPosition((float) xPos, (float) yPos, (float) zPos);
		bullet.render(gl);
	}
	
	public Vector3 getPosition() {
		return new Vector3(xPos, yPos, zPos);
	}
	
	public void interact(GameObject object) {
		
		if(object instanceof Map) {

			height = ((Map) object).getHeightAt(
					xPos, zPos);
		}
	}
}