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

public class Boy : MonoBehaviour {

	public Shit shitController;

	public Text e;
	public float distance;
	public float smooth;

	public bool dep;
	Timer depTimer;
	public bool blur;
	public bool jerk;
	Timer jerkTimer;
	public bool shake;
	public bool drunk;

	public static int totalPills;
	public int currentPills;
	bool carrying = false;
	GameObject carried;

	float rate = 0f;

	void Start () {
		dep = false;
		depTimer = new Timer(1f);
		jerkTimer = new Timer(2f);
		
	}

	// Update is called once per frame
	void Update() {
		Debug.DrawLine(Camera.main.transform.position, Camera.main.transform.forward, Color.black, 1);
		RaycastHit hit;
		bool there;

		Effects();

		there = Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 2);

		if (carrying) {
			Carry();
			print("carrying");
		}

		if (Input.GetMouseButtonDown(0)) {
			if (carrying) {
				Drop();
			}
			else if (there && hit.transform.GetComponent<Grabbable>() != null) {
				Pickup(hit.transform.gameObject);
			}
		}

		if (there) {
			if (hit.transform.GetComponent<Interactiable>() != null) {
				e.gameObject.SetActive(true);
				e.text = hit.transform.GetComponent<Interactiable>().shitToSay;
				if (Input.GetKeyDown("e")) {
					hit.transform.GetComponent<Interactiable>().Use();
                }
			}
			else {
				e.gameObject.SetActive(false);
			}

		}
	}

	//checks if effects if so does them
	void Effects() {
		if (totalPills / 2 > Shit.day -1) {
			blur = true;
		}
		else {
			blur = false;
		}
		if (totalPills / 2 > Shit.day) {
			drunk = true;
		}
		else {
			drunk = false;
		}
		if (totalPills / 2 < Shit.day-2) {
			jerk = true;
		}
		else {
			jerk = false;
		}
		if (totalPills / 2 < Shit.day-1) {
			shake = true;
		}
		else {
			shake = false;
		}

		if (dep) {
			//depression
			if (depTimer.Check()) {
				Camera.main.transform.eulerAngles = new Vector3(Mathf.LerpAngle(Camera.main.transform.eulerAngles.x, 60, Time.deltaTime * .25f), Camera.main.transform.eulerAngles.y, Camera.main.transform.eulerAngles.z);
				GetComponent<UnityStandardAssets.Characters.FirstPerson.RigidbodyFirstPersonController>().enabled = false;
			}
			if (Input.anyKey || Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0) {
				GetComponent<UnityStandardAssets.Characters.FirstPerson.RigidbodyFirstPersonController>().enabled = true;
				depTimer.Reset();
			}
		}
		if (blur) {
			Camera.main.GetComponent<UnityStandardAssets.ImageEffects.Blur>().enabled = true;
		}
		else {
			Camera.main.GetComponent<UnityStandardAssets.ImageEffects.Blur>().enabled = false;
		}
		if (drunk) {
			Camera.main.GetComponent<UnityStandardAssets.ImageEffects.Twirl>().enabled = true;
			Camera.main.GetComponent<UnityStandardAssets.ImageEffects.Twirl>().angle = Mathf.Sin(rate)*50;
			rate += .1f;
        }
		else {
			Camera.main.GetComponent<UnityStandardAssets.ImageEffects.Twirl>().enabled = false;
		}
		if (jerk) {
			if (jerkTimer.Check()) {
				//this is shit but it works sorry
				Camera.main.transform.eulerAngles = new Vector3(Mathf.LerpAngle(Camera.main.transform.eulerAngles.x, Random.Range(60,0), Time.deltaTime * 10), Mathf.LerpAngle(Camera.main.transform.eulerAngles.y, Random.Range(-10, 10), Time.deltaTime * 10), Camera.main.transform.eulerAngles.z);
				jerkTimer = new Timer(Random.Range(.5f, 3f));
			}
		}
		if (shake) {
			//Camera.main.transform.eulerAngles = new Vector3(Mathf.LerpAngle(Camera.main.transform.eulerAngles.x, Random.Range(60, 0), Time.deltaTime * 10),Camera.main.transform.eulerAngles.y, Camera.main.transform.eulerAngles.z);
			//really bad cant look straight shake
			Camera.main.transform.eulerAngles = new Vector3(Mathf.LerpAngle(Camera.main.transform.eulerAngles.x, Random.Range(20, 0), Time.deltaTime * 10), Mathf.LerpAngle(Camera.main.transform.eulerAngles.y, Random.Range(-10, 10), Time.deltaTime * 10), Camera.main.transform.eulerAngles.z);
		}
	}

	void Pickup(GameObject o) {
		o.GetComponent<Rigidbody>().useGravity = false;
		carried = o;
		carrying = true;
	}

	void Drop() {
		carried.GetComponent<Rigidbody>().useGravity = true;
		carried = null;
		carrying = false;
	}

	void Carry() {
		carried.transform.position = Vector3.Lerp(carried.transform.position, Camera.main.transform.position + (Camera.main.transform.forward * distance), Time.deltaTime * smooth);
	}
}
