using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour {

	static public int runs = 0;

	public float speed;
	public float turn;
	public float speedOfTime;
	public float time;
	public float infMagnitude; // Magnitude of change in speed
	private float fuel;
	public Vector3 infDirection; // Direction being changed to
	public Vector3 mv;
	public float currentRotation;
	public int counter = 0;

	public float gravFactor = 0.075f;

	public int objType = 0; //Identifies this as a player object

	public float MAX_ROTATE = 45;
	public float ROTATION_DELTA = 90;

	public float MAX_SPEED = 100;
	public float MAX_TURN = 5;

	public float MAX_SPEED_BOOST = 150f;
	public float MAX_TURN_BOOST = 5;

	public float speedBoost;
	public float turnBoost;

	public int waveBoost;

	public Text speedText;
	public Text fuelText;

	public AudioSource[] audio;
	public Vector3 waveOffset;

	public float Fuel
	{
		get
		{
			return fuel;
		}

		set
		{
			if (fuel + value > 100) fuel = 100;
			else if (fuel + value < 0) fuel = 0;
			else fuel = value;
			fuelText.text = "Fuel: " + fuel;
		}
	}

	// Use this for initialization
	void Start() {
		audio = GetComponents<AudioSource>();
		//Debug.Log(audio[0].clip.ToString());
		//Debug.Log(audio[1].clip.ToString());
		//Debug.Log(audio[2].clip.ToString());
		waveBoost = 0;
		speedBoost = 0;
		turnBoost = 0;
		speed = 100;
		turn = 10;
		infMagnitude = 0;
		infDirection = new Vector3(0, 0, 0);
		if (runs < 1) {
			Destroy(GameObject.Find("Skimmer"));
		}
		if (runs < 2) {
			fuelText.enabled = false;
		}
		waveOffset = Vector3.zero;
	}

	// Update is called once per frame
	void Update() {

		if (waveBoost > 0) {
			waveBoost--;
			audio[0].loop = true;
		}

		float x = Input.GetAxis("Horizontal");
		float z;


		if (runs > 1 && Input.GetAxis("Fire3") != 0) {
			Debug.Log("Shift pressed!");
			if (Fuel >= 50) {
				waveBoost += (int)(500f * (Fuel / 100));
				Fuel = 0;
				audio[1].Play();
				audio[2].Play(2);
			}
		}

		z = (x * ROTATION_DELTA) * Time.deltaTime;
		currentRotation = z + currentRotation;
		if (currentRotation > MAX_ROTATE) {
			z = 0; currentRotation = MAX_ROTATE;
		}
		if (currentRotation < (MAX_ROTATE * -1)) {
			z = 0; currentRotation = -MAX_ROTATE;
		}

		//infDirection.Set(z,x,y);

		if (x == 0) {
			if (currentRotation == 0) { }
			else if ((currentRotation > -1) && (currentRotation < 1)) {
				currentRotation = 0; z = -z;
			}
			//else if (currentRotation > 0) {
			//	currentRotation = currentRotation - (ROTATION_DELTA *
			//		Time.deltaTime);
			//	z = currentRotation;
			//}
			//else {
			//	currentRotation = currentRotation + (ROTATION_DELTA *
			//		Time.deltaTime);
			//	z = currentRotation;
			//}
			else {
				z = -(currentRotation * Time.deltaTime);
				currentRotation = currentRotation + z;
			}
		}

		speed = (speed + infMagnitude);

		mv.Set(infDirection[0], 0, 0);

		infMagnitude = 0;
		infDirection = Vector3.zero;

		transform.Rotate(transform.forward, -z);
		//transform.Rotate(transform.up, x); //Don't need this anyway! :)

		transform.position = transform.position + (Vector3.right * x * (MAX_TURN + turnBoost));
		float speedSet = MAX_SPEED + speedBoost + waveBoost;

		speedText.text = "Speed: " + speedSet;
		transform.Translate(0, 0, speedSet * Time.deltaTime);
		//transform.Translate(waveOffset * Time.deltaTime);
		transform.position.Set(transform.position[0], 0, transform.position[2]);
		/*
				transform.position = transform.position + (Vector3.right * x * MAX_TURN * 
					Time.deltaTime) + (mv[0] * Vector3.right);
				Debug.Log(speed);
				transform.Translate(0,0,(MAX_SPEED+speed)*Time.deltaTime);
				transform.position.Set(transform.position[0],0,transform.position[2]);
				*/
	}

	void OnCollisionEnter(Collision col) {
		GameObject obj = col.gameObject;
		if (obj.tag == "Planet") {
			//Debug.Log(infMagnitude);
			//Debug.Log(infDirection);
			CrashAndBurn();
		}

	}

	void CrashAndBurn() {
		SceneManager.LoadScene("GameOver");
	}

	void OnTriggerStay(Collider obj) {
		if (obj.tag == "Grav") {
			if (audio[0].isPlaying == false) {
				audio[0].loop = true;
				audio[0].Play();
			}
			RotationScript rot = obj.transform.parent.GetComponent<RotationScript>();
			float maxDistance = rot.gravityRadius - rot.planetRadius;
			float myDistanceToCenter = Vector3.Distance(transform.position, obj.transform.parent.position);
			float ratioTowardsOutside = (rot.gravityRadius - myDistanceToCenter) / maxDistance;
			float ratioTowardsInside = ((myDistanceToCenter - rot.planetRadius) / maxDistance) * 2f;

			speedBoost = ratioTowardsOutside * MAX_SPEED_BOOST;
			turnBoost = ratioTowardsInside * MAX_TURN_BOOST;






			//counter++;
			//Debug.Log(counter);
			//Debug.Log(gravFactor, gameObject);
			/*Vector3 oPos = obj.transform.position;
			Vector3 sPos = gameObject.transform.position;
			Vector3 scalar = sPos - oPos;
			Vector3 dir = scalar.normalized;

			float push = (((scalar.magnitude /
				obj.transform.parent.GetComponent<RotationScript>().gravityRadius) * obj.transform.parent.GetComponent<RotationScript>().planetRadius)
				* dir[2]) * gravFactor;


			float pull = ((((scalar.magnitude /
				obj.transform.parent.GetComponent<RotationScript>().gravityRadius) * obj.transform.parent.GetComponent<RotationScript>().planetRadius)
				* dir[0]) * gravFactor) * 0.01f;

			infMagnitude = infMagnitude + push;
			infDirection[2] = infDirection[2] + infMagnitude;
			infDirection[0] = (infDirection[0] + pull) * -1;
			//Debug.Log("The value of push is: ");
			//Debug.Log(push);
			//Debug.Log(pull);
			//Debug.Log(oPos);
			//Debug.Log(sPos);
			//Debug.Log(dir);
			//Debug.Log(scalar);*/
		}
		else { audio[0].loop = false; }
	}

	void OnTriggerExit(Collider col) {
		if (col.tag == "Grav") {
			speedBoost = 0;
			turnBoost = 0;
		}
	}

	void OnDestroy() {
		runs++;
	}

}
