using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public class DialogueManager : MonoBehaviour {

	public Texture2D background;
	public Texture2D topCorner;
	public Texture2D bottomCorner;
	public Texture2D arrow;
	public Font font;
	public Color textColor;
	
	public string text;
	
	public bool showingDlg = false;
	
	private GUIStyle textStyle;
	
	public int lineIndex = 0;
	public List<Dialog> lines;
	private float displayTimer = 0;
	
	private int oldIndex = -1;
	private bool mouseIsOverButton = false;
	
//	private float introTimer = 0;
//	private bool introHasPlayed = false;
	
	private Theatre theatre;
	
	void Awake ()
	{		
		textStyle = new GUIStyle();
		UpdateTextStyle();
		theatre = FindObjectOfType(typeof(Theatre)) as Theatre; // GetComponent<Theatre>();
		theatre.curtainsOpen = 0;
		theatre.tabsOpen = 0;
		theatre.UpdateCurtainsAndTabs();
	}
	
	void Update ()
	{
//		if (!introHasPlayed)
//		{
////			introTimer += Time.deltaTime;
//			text = "\nA Game\nwith\nLife & Death\n\nby\nMatt Stockham\nfor Ludum Dare 31\n\nBased on a one-act play by\nLouise Bryant\n\nwith music by\nMoishe's Bagel\n\n\n\n";
//			if ((mouseIsOverButton && Input.GetMouseButtonDown(0))) // || introTimer > 30)
//			{
//				theatre.OpenCurtains();
//				theatre.OpenTabs();
//				lineIndex = 0;
//				showingDlg = true;
//				introHasPlayed = true;
//				mouseIsOverButton = false;
//			}
//		}
//		
		if (!showingDlg)
//			if (lineIndex < lines.Count || lineIndex == 0)
				return;
		
		if (mouseIsOverButton && Input.GetMouseButtonDown(0))
		{
			displayTimer = 0;
			lineIndex++;
		}	
		
		/// Next dialogue triggered
		if (lineIndex != oldIndex && lineIndex < lines.Count)
		{
			oldIndex = lineIndex;
			if (lineIndex < lines.Count && lines[lineIndex].animTriggers.Length > 0)
			{
				for (int a = 0; a < lines[lineIndex].animTriggers.Length; a++)
				{
					if (lines[lineIndex].animTriggers[a].actor != null)
					{
						lines[lineIndex].animTriggers[a].actor.setCurrentAnim(lines[lineIndex].animTriggers[a].animName);
						if (lines[lineIndex].animTriggers[a].move)
							lines[lineIndex].animTriggers[a].actor.MoveTo(lines[lineIndex].animTriggers[a].moveTarget,
																					lines[lineIndex].animTriggers[a].moveTime,
																					lines[lineIndex].animTriggers[a].turn,
																					lines[lineIndex].animTriggers[a].callBackAnim);
					}
					
					if (lines[lineIndex].animTriggers[a].changeTabs > -1)
					{
						if (theatre.tabsOpen == 0)
							theatre.OpenTabs(lines[lineIndex].animTriggers[a].changeTabs);
						if (theatre.tabsOpen == 1)
							theatre.CloseTabs(lines[lineIndex].animTriggers[a].changeTabs);
					}
					
					if (lines[lineIndex].animTriggers[a].changeCurtains > -1)
					{
						if (theatre.curtainsOpen == 0)
							theatre.OpenCurtains(lines[lineIndex].animTriggers[a].changeCurtains);
						if (theatre.curtainsOpen == 1)
							theatre.CloseCurtains(lines[lineIndex].animTriggers[a].changeCurtains);
					}
					
					
					if (lines[lineIndex].animTriggers[a].lightsIn.Length > 0)
					{
						for (int l = 0; l < lines[lineIndex].animTriggers[a].lightsIn.Length; l++)
						{
							lines[lineIndex].animTriggers[a].lightsIn[l].gameObject.active = true;
						}
					}
					
					if (lines[lineIndex].animTriggers[a].lightsOut.Length > 0)
					{
						for (int l = 0; l < lines[lineIndex].animTriggers[a].lightsOut.Length; l++)
						{
							lines[lineIndex].animTriggers[a].lightsOut[l].gameObject.active = false;
						}
					}
				}
			}
		}
		
		UpdateTextStyle();
		
		if (lineIndex >= lines.Count)
		{
			theatre.NextDlg();
//			showingDlg = false;
//			text = "To be continued...\n\n...tomorrow\n\nI missed the deadline for the full contest\nby just a few hours - so this will be\nentered in the 72-hour jam instead\n\nTime spent: 32 hours (approx)";
//			if (theatre.tabsOpen == 1)
//				theatre.CloseTabs();
//			if (theatre.tabsOpen == 0 && theatre.curtainsOpen == 1)
//				theatre.CloseCurtains();
//			if (theatre.curtainsOpen == 0)
//				showingDlg = true;
		}
		else
		{
			text = lines[lineIndex].dialogue;
			if (lines[lineIndex].displayTime > 0)
			{
				displayTimer += Time.deltaTime;
				if (displayTimer > lines[lineIndex].displayTime)
				{
					displayTimer = 0;
					lineIndex++;
				}
			}
		}
	}
	
	void OnGUI ()
	{
		if (!showingDlg)
//			if (introHasPlayed) 
				return;
		
		if (text == "") return;
		
		
		
		Vector2 textSize = textStyle.CalcSize(new GUIContent(text));
		
		Rect dlgBox = new Rect((Screen.width - textSize.x - 230) * 0.5f, 20, textSize.x + 230, textSize.y + 80);
		if (lineIndex >= lines.Count)
			dlgBox.y += 120;
		GUI.DrawTexture(dlgBox, background);
		GUI.Label(dlgBox, text, textStyle);
		
		Rect cornerBox = dlgBox;
		float cbRatio;
		if (dlgBox.width > dlgBox.height)
		{
			cbRatio = dlgBox.height / topCorner.height;
			if (cornerBox.width > topCorner.width * cbRatio)
				cornerBox.width = topCorner.width * cbRatio;
		}
		else
		{
			cbRatio = dlgBox.width / topCorner.width;
			if (cornerBox.height > topCorner.height * cbRatio)
				cornerBox.height = topCorner.height * cbRatio;
		}
		GUI.DrawTexture(cornerBox, topCorner);
		
		if (dlgBox.width > dlgBox.height)
			cornerBox.x = dlgBox.x + dlgBox.width - cornerBox.width;
		else
			cornerBox.y = dlgBox.y + dlgBox.height - cornerBox.height;
		
		GUI.DrawTexture(cornerBox, bottomCorner);
		
		if (lineIndex < lines.Count)
		{
			Rect arrBox = dlgBox;
			arrBox.width = arrow.width * 0.5f;
			arrBox.height = arrow.height * 0.5f;
			arrBox.y = dlgBox.y + dlgBox.height - arrow.height;
//			if (lines[lineIndex].Label == "Introduction")
				arrBox.x = dlgBox.x + dlgBox.width - arrow.width - 20;
//			else
//			{
//				arrBox.x = dlgBox.x + (dlgBox.width * 0.5f) - (arrow.width * 0.5f) + 20;
//				arrBox.y -= 30;
//			}
			GUI.DrawTexture(arrBox, arrow);
			if (arrBox.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y)))
				mouseIsOverButton = true;
			else
				mouseIsOverButton = false;
		}
	}
	
	
	void UpdateTextStyle ()
	{
		if (textStyle.font != font)
			textStyle.font = font;
		
		if (textStyle.normal.textColor != textColor)
			textStyle.normal.textColor = textColor;
		
		if (textStyle.alignment != TextAnchor.MiddleCenter)
			textStyle.alignment = TextAnchor.MiddleCenter;
		
		textStyle.border = new RectOffset(75, 75, 1200, 600);
		
	}
	
}

[System.Serializable]
public class Dialog
{
	public string Label;
	public Actor actor;
	public string text;
	
	public float displayTime = 10;
	
	public AnimTrigger[] animTriggers;
	
	public string dialogue
	{
		get
		{
			if (text == "")
				return text;
			else if (actor != null)
				return actor.Name.ToUpper() + ":\n" + text + "\n";
			else
				return text + "\n";
		}
	}
	
}

[System.Serializable]
public class AnimTrigger
{
	public string Label;
	public Actor actor;
	public string animName;
	public bool move = false;
	public Transform moveTarget;
	public float moveTime;
	public bool turn = false;
	public string callBackAnim;
	
	public float changeTabs = -1;
	public float changeCurtains = -1;
	
	public Light[] lightsIn;
	public Light[] lightsOut;
}

