using UnityEngine;
using System.Collections;

public class Bed : MonoBehaviour {

	public float overTime;
	
	public GameObject bedCam;
	public GameObject[] plants;
	
	public bool sleeping;
	
	public AudioClip scuffle;
	
	public Transform cameraPos;
	
	private GameObject player;
	private States states;

	// Use this for initialization
	void Start () {
		player = GameObject.Find("Player");
		states = player.GetComponent<States>();
	}
	
	void Use () {
		StartCoroutine(GoToBed ());
	}
	
	IEnumerator GoToBed ()
	{
		//Going to bed
		player.GetComponent<ExtraControls>().inCutscene = true;
		player.GetComponent<HeadsUpDisplay>().inCutscene = true;
		StartCoroutine(GameObject.Find("Fader").GetComponent<SceneFader>().FadeToBlack());
		StartCoroutine(GameObject.Find("Fader").GetComponent<SceneFader>().Intro());
		states.controllerFlag = false;
		states.mouselookFlag = false;
		AudioSource.PlayClipAtPoint(scuffle, transform.position);
		StartCoroutine(ToBedCamMove ());
		StartCoroutine(ToBedCamRotate ());
		yield return new WaitForSeconds(overTime);
		
		//Meta stuff
		for (int i = 0;i < plants.Length;i++)
		{
			plants[i].SendMessage("Sleep");
		}
		states.sleepCount++;
		states.water = states.dailyWater;
		sleeping = true;
		if (!states.medicated)
		{
			states.missedPills++;
		} else {
			states.missedPills--;
			states.medicated = false;
		}
		yield return null;
		sleeping = false;
		
		//Waking up
		yield return new WaitForSeconds(2f);
		StartCoroutine(GameObject.Find("Fader").GetComponent<SceneFader>().FadeToClear());
		AudioSource.PlayClipAtPoint(scuffle, transform.position);
		StartCoroutine(FromBedCamMove ());
		StartCoroutine(FromBedCamRotate ());
		yield return new WaitForSeconds(overTime);
		player.GetComponent<ExtraControls>().inCutscene = false;
		player.GetComponent<HeadsUpDisplay>().inCutscene = false;
		states.controllerFlag = true;
		states.mouselookFlag = true;
		states.selling = false;
		
	}
	
	IEnumerator ToBedCamMove ()
	{
		float startTime = Time.time;
		while(Time.time < startTime + overTime)
		{
			Camera.main.transform.position = Vector3.Lerp(cameraPos.position, bedCam.transform.position, (Time.time - startTime) / overTime);
			yield return null;
		}
		Camera.main.transform.position = bedCam.transform.position;
		yield return null;
	}
	
	IEnumerator ToBedCamRotate ()
	{
		float startTime = Time.time;
		while(Time.time < startTime + overTime)
		{
			Camera.main.transform.rotation = Quaternion.Lerp(cameraPos.rotation, bedCam.transform.rotation, (Time.time - startTime) / overTime);
			yield return null;
		}
		Camera.main.transform.rotation = bedCam.transform.rotation;
	}
	
	IEnumerator FromBedCamMove ()
	{
		float startTime = Time.time;
		while(Time.time < startTime + overTime)
		{
			Camera.main.transform.position = Vector3.Lerp(bedCam.transform.position, cameraPos.position, (Time.time - startTime) / overTime);
			yield return null;
		}
		Camera.main.transform.position = cameraPos.position;
		yield return null;
	}
	
	IEnumerator FromBedCamRotate ()
	{
		float startTime = Time.time;
		while(Time.time < startTime + overTime)
		{
			Camera.main.transform.rotation = Quaternion.Lerp(bedCam.transform.rotation, cameraPos.rotation, (Time.time - startTime) / overTime);
			yield return null;
		}
		Camera.main.transform.rotation = cameraPos.rotation;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
