using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{
	public float SpeedLimit = 15;
	public GameObject camera;
	public float forceAmount =5;
	public float CameraTilt = 15;
	public bool isDead = false;
	public bool paused;
	public float CurrentDistance 
	{
		get{return transform.position.y;}
	}
	public float CurrentX
	{
		get{return transform.position.x;}
	}
	public int CurrentDepth =0;
	public float tick = 1;
	public float timer=0;
	public float increasingForce=-.001f;
	public int HighScore
	{
		set
		{
			tick-=.001f;
			increasingForce-=.001f;
			UpdateScore(100);
			//GameObject.Find("Manager").GetComponent<Manager>().UpdateScore(value);
		}
	}
	public static int score = 0;

	
	// Update is called once per frame
	void Update () 
	{
		if(timer > tick)
		{
			timer =0;
			HighScore=1;
		}
		else
		{
			timer += Time.deltaTime;
		}
		rigidbody.AddRelativeForce(0,increasingForce,0,ForceMode.VelocityChange);
		if(Input.GetKey(KeyCode.D))
		{
			MoveLeft();
			var newRotation = Quaternion.LookRotation(camera.transform.position - transform.position).eulerAngles;
			newRotation.x = 0;
			newRotation.y = CameraTilt;
			newRotation.z = 0;
			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(newRotation), Time.deltaTime);	
		}
		else if(Input.GetKey(KeyCode.A))
		{
			MoveRight();
			var newRotation = Quaternion.LookRotation(camera.transform.position - transform.position).eulerAngles;
			newRotation.x = 0;
			newRotation.y = -CameraTilt;
			newRotation.z = 0;
			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(newRotation), Time.deltaTime);
		}
		else
		{

			var newRotation = Quaternion.LookRotation(camera.transform.position - transform.position).eulerAngles;
			newRotation.x = 0;
			newRotation.y = 0;
			newRotation.z = 0;
			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(newRotation), Time.deltaTime);
		}

		if(Input.GetKeyDown(KeyCode.Escape))
		{
			if(Time.timeScale == 0)
			{
				Time.timeScale = 1;
				paused = false;
			}
			else
			{
				paused = true;
				Time.timeScale = 0;
			}
		}

	}

	public void MoveRight()
	{

		rigidbody.AddRelativeForce(Vector3.right*forceAmount,ForceMode.VelocityChange);
	}
	public void MoveLeft()
	{

		rigidbody.AddRelativeForce(Vector3.left*forceAmount,ForceMode.VelocityChange);
	}
	public void OnGUI() {

		GUI.Label(new Rect(Screen.width/2-50,20,100,100),"SCORE: " + score);
		if(paused)
		{
			GUI.Label(new Rect(100,200,100,100),"PAUSED");
		}

	}

	public void UpdateScore(int amount)
	{
		score+=amount;
	}

}
