using UnityEngine;
using System.Collections;

public class Theatre : MonoBehaviour {

	public cCurtain leftCurtain;
	public cCurtain rightCurtain;
	public cCurtain tabs;
	
	public float curtainsOpen = 0;
	public float tabsOpen;
	
	public float curtainSpeed = 2;
	public float tabsSpeed = 2;
	
	public DialogueManager[] dialogs;
	
	private int dlgIndex = -1;
	
	private float tabsDelay = 0;
	private float curtainDelay = 0;
	
	void Start ()
	{
		NextDlg();
	}
	
	public void NextDlg (int index = -1)
	{
		if (dlgIndex > -1 && dialogs[dlgIndex] != null)
			dialogs[dlgIndex].showingDlg = false;
		
		if (index == -1)
			dlgIndex++;
		else
			dlgIndex = index;
		
		if (dlgIndex > -1 && dlgIndex < dialogs.Length && dialogs[dlgIndex] != null)
			dialogs[dlgIndex].showingDlg = true;
	}
	
	public void UpdateCurtainsAndTabs ()
	{
		curtainsOpen = Mathf.Clamp01(curtainsOpen);
		tabsOpen = Mathf.Clamp01(tabsOpen);
		
		leftCurtain.SetCurtains(curtainsOpen);
		rightCurtain.SetCurtains(curtainsOpen);
		tabs.SetCurtains(tabsOpen);
	}
	
	public void OpenCurtains (float delay = 0)
	{
		curtainDelay = 0;
		StopCoroutine("toggleCurtains");
		StartCoroutine("toggleCurtains", true);
	}
	
	public void CloseCurtains (float delay = 0)
	{
		curtainDelay = 0;
		StopCoroutine("toggleCurtains");
		StartCoroutine("toggleCurtains", false);
	}
	
	IEnumerator toggleCurtains (bool open = true)
	{
		float start = curtainsOpen;
		float timeLerped = 0f;
		
		if (curtainDelay > 0)
			yield return new WaitForSeconds(curtainDelay);
		
		if (open)
		{
			while (curtainsOpen < 1f)
			{
				timeLerped += Time.deltaTime;
				curtainsOpen = Mathf.Lerp(start, 1, timeLerped / curtainSpeed);
				UpdateCurtainsAndTabs();
				yield return null;
			}
			curtainsOpen = 1;
			UpdateCurtainsAndTabs();
		}
		else
		{
			while (curtainsOpen > 0f)
			{
				timeLerped += Time.deltaTime;
				curtainsOpen = Mathf.Lerp(start, 0, timeLerped / curtainSpeed);
				UpdateCurtainsAndTabs();
				yield return null;
			}
			curtainsOpen = 0;
			UpdateCurtainsAndTabs();
		}
	}
	
	public void OpenTabs (float delay = 0)
	{
		tabsDelay = delay;
		StopCoroutine("toggleTabs");
		StartCoroutine("toggleTabs", true);
	}
	
	public void CloseTabs (float delay = 0)
	{
		tabsDelay = delay;
		StopCoroutine("toggleTabs");
		StartCoroutine("toggleTabs", false);
	}
	
	IEnumerator toggleTabs (bool open = true)
	{
		float start = tabsOpen;
		float timeLerped = 0f;
		
		if (tabsDelay > 0)
			yield return new WaitForSeconds(tabsDelay);
		
		if (open)
		{
			while (tabsOpen < 1f)
			{
				timeLerped += Time.deltaTime;
				tabsOpen = Mathf.Lerp(start, 1, timeLerped / tabsSpeed);
				UpdateCurtainsAndTabs();
				yield return null;
			}
			tabsOpen = 1;
			UpdateCurtainsAndTabs();
		}
		else
		{
			while (curtainsOpen > 0f)
			{
				timeLerped += Time.deltaTime;
				tabsOpen = Mathf.Lerp(start, 0, timeLerped / tabsSpeed);
				UpdateCurtainsAndTabs();
				yield return null;
			}
			tabsOpen = 0;
			UpdateCurtainsAndTabs();
		}
	}
	
	void Update ()
	{
		UpdateCurtainsAndTabs();
	}
	
}

[System.Serializable]
public class cCurtain
{
	public GameObject mesh;
	public Vector3 axis;
	public float openPos;
	public float closedPos;
	public float amount;
	
	public void SetCurtains (float a)
	{
		amount = a;
		Vector3 pos = mesh.transform.position;
		float operand = ((openPos - closedPos) * amount);
		
		if (axis.x == 1)
			pos.x = closedPos + operand;
		
		if (axis.y == 1)
			pos.y = closedPos + operand;
		
		if (axis.z == 1)
			pos.z = closedPos + operand;
		
		mesh.transform.position = pos;
		
	}
}
	
