﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

namespace Nostalgia.Example
{
	[AddComponentMenu("Nostalgia/Example/MessageBoard")]
	public sealed class MessageBoard : MonoBehaviour, ISubmitHandler, IPointerClickHandler
	{
		static MessageBoard _Instance;

		public GameObject window;
		public Image _Background;
		public Image cursor;
		public Text text;

		private float _Wait = 0.0f;
		private bool _IsOpen = false;
		private bool _CompleteShowText = false;
		private Vector2 _CursorPosition;

		void Awake()
		{
			_Instance = this;

			_Background.enabled = false;

			_CursorPosition = cursor.rectTransform.anchoredPosition;
		}

		public static void Open(string message, float wait)
		{
			_Instance.OpenInternal(message, wait);
		}

		IEnumerator WaitUnscaledSeconds(float seconds)
		{
			float time = Time.unscaledTime;
			while (Time.unscaledTime - time < seconds)
			{
				yield return null;
			}
		}

		IEnumerator ShowText(string message, float wait)
		{
			_CompleteShowText = false;
			_Wait = wait;

			text.text = "";
			int messageIndex = 0;
			while (messageIndex < message.Length)
			{
				if (_Wait > 0.0f)
				{
					yield return StartCoroutine(CoroutineUtility.WaitUnscaledSeconds(_Wait));
				}
				text.text += message[messageIndex];
				messageIndex++;
			}

			_CompleteShowText = true;
		}

		IEnumerator MoveCursor()
		{
			cursor.rectTransform.anchoredPosition = _CursorPosition;
			float moveDuration = 0.2f;

			float time = Time.unscaledTime;
			while (_IsOpen)
			{
				float t = (Time.unscaledTime - time) / moveDuration;
				t = (Mathf.Sin(t) + 1.0f) / 2.0f;
				cursor.rectTransform.anchoredPosition = Vector2.Lerp(_CursorPosition, _CursorPosition + new Vector2(0, -5), t);
				yield return null;
			}
		}

		void OpenInternal(string message, float wait)
		{
			if (!_IsOpen)
			{
				text.text = message;
				RectTransform rectTransform = window.GetComponent<RectTransform>();
				Vector2 sizeDelta = rectTransform.sizeDelta;
				sizeDelta.x = text.preferredWidth + (text.rectTransform.offsetMin.x - text.rectTransform.offsetMax.x) + 2;
				sizeDelta.y = text.preferredHeight + (text.rectTransform.offsetMin.y - text.rectTransform.offsetMax.y) + 2;
				rectTransform.sizeDelta = sizeDelta;

				text.text = "";
				Time.timeScale = 0.0f;
				_Background.enabled = true;
				window.SetActive(true);

				_IsOpen = true;

				StartCoroutine(ShowText(message, wait));
				StartCoroutine(MoveCursor());

				EventSystem.current.SetSelectedGameObject(gameObject);
			}
		}

		IEnumerator Close()
		{
			yield return null;

			window.SetActive(false);
			_Background.enabled = false;
			Time.timeScale = 1.0f;

			_IsOpen = false;

			EventSystem.current.SetSelectedGameObject(null);
		}

		public void OnSubmit()
		{
			if (_IsOpen)
			{
				if (!_CompleteShowText)
				{
					_Wait = 0.0f;
				}
				else
				{
					StartCoroutine(Close());
				}
			}
		}

		public void OnSubmit(BaseEventData data)
		{
			OnSubmit();
		}

		public void OnPointerClick(PointerEventData eventData)
		{
			OnSubmit();
		}
	}
}
