import DBase;	//Importing Database Functionality
import Level;	//Importing Level Class

public var tennis:Material;
public var beach:Material;
public var softball:Material;

private var clicked:boolean;	//TRUE if it is currently being clicked and FALSE if released
private var mClickPos:Vector3;	//Will contain the mouse position of the click
private var mReleaPos:Vector3;	//Will contain the mouse position of the release
private var released:boolean;	//Will determine whether the ball has been thrown or not.
public var basketSound: AudioClip;	//Used to make the bouncing sound of the ball

private var move:boolean;		//TRUE when the ball is moving and FALSE otherwise
private var moveDirection:boolean;	//The direction of ball
private var speed;				//The speed of the ball

function Start () {
	move = true;	//At the beginning the ball will be going up and down
	moveDirection = true;	//Set a direction
	
	//Getting the level information for the ball attributes
	var db = new DBase();
	var lvl = db.getLevelById(PlayerPrefs.GetInt("Level"));
	speed = lvl.getSpeed();	//Assigning the speed of the ball
	
	//Powerup type.
	if(GameObject.FindGameObjectWithTag("powerup").renderer.material.color == Color.red){
		GameObject.FindGameObjectWithTag("powerup").renderer.material.color = Color.blue;
	}else{
		GameObject.FindGameObjectWithTag("powerup").renderer.material.color = Color.red;
	}
	
	var balls = GameObject.FindGameObjectsWithTag("basketball");
	if(PlayerPrefs.GetInt("Level") == 1){
	
	}else if(PlayerPrefs.GetInt("Level") == 2){
		
		for(var ball : GameObject in balls){
			ball.renderer.material = tennis;
		}	
	}else if(PlayerPrefs.GetInt("Level") == 3){
		for(var ball : GameObject in balls){
			ball.renderer.material = beach;
		}	
	}else if(PlayerPrefs.GetInt("Level") == 4){
		for(var ball : GameObject in balls){
			ball.renderer.material = softball;
		}	
	}
}

function Update () {
	getClick();
	stopMovement();
	verticalMovement();
}

//This method will be recording the drag of the throw
private function getClick(){
	//Check whether the user is clicking the left click and whether the throw has not yet been done
	if(Input.GetMouseButtonDown(0)){
		//If the user has not released the ball and the ball has been stoped [space key clicked]...
		if(released == false && move == false){	
			//Get the area covered by the ball - The area which is clickable	
			var ballLeft 	= transform.position.x - 0.5;
			var ballRight 	= transform.position.x + 0.5;
			var ballTop 	= transform.position.y - 0.5;
			var ballBot 	= transform.position.y + 0.5;
			
			//Get the mouse position of the click
			var mClickPos	= getMousePosition(); //The position of the first click so that we can identify the angle and power of the throw	
			
			//If the click is within the clickable area of the ball we need to start recording the power
			//and angle of the shot.
			if(mClickPos.x > ballLeft && mClickPos.x < ballRight && mClickPos.y > ballTop && mClickPos.y < ballBot){
				clicked = true;
			}
			
			GameObject.FindGameObjectWithTag("basketball").renderer.material.color = Color.green;
		}
	}
	
	//When a user would like to cancel the shot, all he has to do is to simply click the right click
	if(Input.GetMouseButtonDown(1)){
		clicked = false;
	}
	
	//This condition will check whether the user has clicked and has released the left button
	if(Input.GetMouseButtonUp(0) && clicked == true){
		released = true;	//Eliminate the chance for a second shot
		//We need to add gravity factor and force
		transform.gameObject.GetComponent(Rigidbody).useGravity = true;
		mReleaPos 	= getMousePosition();	//Getting the mouse position of the release
		//Adding force accordingly
		transform.gameObject.GetComponent(Rigidbody).AddRelativeForce (Vector3.up * getPower(mClickPos,mReleaPos,false) * 400);
		transform.gameObject.GetComponent(Rigidbody).AddRelativeForce (Vector3.right * getPower(mClickPos,mReleaPos,true) * 400);
		
		GameObject.FindGameObjectWithTag("basketball").renderer.material.color = Color.white;
	}
}

//This method will be used to get the position of the mouse click
private function getMousePosition():Vector3{
	var mouseCoord	= Input.mousePosition;
	mouseCoord.z 	= 4;	//Including the z value so that we can work in 3D
	var mc 			= Camera.mainCamera.ScreenToWorldPoint(mouseCoord);	//Getting the world point where the mouse is being clicked.
	//Returning the mouse click position.
	return mc;
}

//With this method we will be able to determine the distance (thus power)
//in the X-Axis of the first click position and the position when the user released the click.
private function getPower(mousePosA:Vector3, mousePosB:Vector3, xAxis:boolean){
	if(xAxis){return mousePosA.x - mousePosB.x;}else{return mousePosA.y - mousePosB.y;}
}

function OnCollisionEnter(obj:Collision){
	//Getting the total life of the player
	var life = PlayerPrefs.GetInt("PlayerLife");
	
	//If the user scores
	if(obj.gameObject.tag == "goal"){
		//Delete the ball
		Destroy(transform.gameObject);
		Camera.mainCamera.GetComponent(GameScript).addScore();	//Add Score
		Camera.mainCamera.GetComponent(GameScript).addLife();	//Add a new life if possible
		GameObject.FindGameObjectWithTag("hoopNet").renderer.material.color = Color.gray;	//Sett the hoop back to normal color.
	}
	
	//If the ball hits the ground or the wall
	if(obj.gameObject.tag == "floor" || obj.gameObject.tag == "wall"){
		Destroy(transform.gameObject);	//Delete the ball
		Camera.mainCamera.GetComponent(GameScript).generateBall();	//Generate a new ball
		Camera.mainCamera.GetComponent(GameScript).pushComment("Ohh.. Try again.");	//Add a commentary comment
		Camera.mainCamera.GetComponent(GameScript).subLife();	//Subtract a life
		GameObject.FindGameObjectWithTag("hoopNet").renderer.material.color = Color.gray;	//Reset the hoop back to the normal color
	}
	
	//If the ball hits the hoop
	if(obj.gameObject.tag == "hoop"){
		Camera.mainCamera.GetComponent(GameScript).pushComment("CLOOOOOSE!!!");	//Add a commentary comment
		GameObject.FindGameObjectWithTag("hoopNet").renderer.material.color = Color.red;	//Set the hoop to red
		GameObject.FindGameObjectWithTag("basketball").renderer.material.color = Color.red;	//Set the ball to red
	}
	
	//If the user hits the plate
	if(obj.gameObject.tag == "baskitPlate"){
		Camera.mainCamera.GetComponent(GameScript).pushComment("On the plate!!");	//Add a commentary comment
	}
	
	audio.PlayOneShot(basketSound);	//For every collision the ball does, make a bouncing sund
}

//This methid will be used to move the ball up and down
private function verticalMovement(){
	//If move is true
	if(move){
		//If the y position is less bigger then one set invert direction
		if(transform.position.y > 1){
			moveDirection = false;
		}else if(transform.position.y < -1){
			//Else if the y is small then -1 invert direction aswell
			moveDirection = true;
		}
		
		//Moving the ball
		if(moveDirection){
			transform.Translate(Vector3.up * speed * Time.deltaTime);
		}else{
			transform.Translate(Vector3.up * (speed) * -1 * Time.deltaTime);
		}
	}
}

//This method will stop the movement of the ball
private function stopMovement(){
	//When the user clicks space, the ball will stop and the user will be able to shoot the ball
	if(move && Input.GetKey(KeyCode.Space)){
		move = false;
	}
}