using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ChatManager : MonoBehaviour {

	public float DelayBeforeStart;
	public GameObject EndOfMessageGameObject;
	public GameObject WaitDialoguePrefab;
	public List<GameObject> DialoguePrefabs;
	[Tooltip("A reference to the player chat bubble spawn position")]
	public GameObject PlayerChatBubblePosition;
	[Tooltip("A reference to the player chat bubble spawn position")]
	public GameObject ReplyChatBubblePosition;

	[Tooltip("A random delay between x and y before display dialogue")]
	public Vector2 DelayBeforeDialogue;
	public GameObject SendButton;

	public delegate void OnDialogueSpawn();
	public OnDialogueSpawn onDialogueSpawn;

	//Reference to the current dialogue displayed/to display
	private int currentDialogue;
	//A list of all the spawned chat bubbles
	private List<GameObject> spawnedDialogues;
	private bool isPlayerChat;

	// Use this for initialization
	void Start () {
		spawnedDialogues = new List<GameObject>();
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.X))
			PlayDialogue();
		if(currentDialogue % 2 == 1)
		{
			SendButton.GetComponent<Button>().interactable = false;
			isPlayerChat = false;
		}
		else
		{
			SendButton.GetComponent<Button>().interactable = true;
			isPlayerChat = true;
		}

		if (currentDialogue == DialoguePrefabs.Count && !EndOfMessageGameObject.activeSelf) {
			StartCoroutine (StartEndOfLine ());
		}
	}

	public void PlayDialogue()
	{
		if (currentDialogue < DialoguePrefabs.Count) {
			if (spawnedDialogues.Count > 0) {
				if (spawnedDialogues.Count == 4) {
					GameObject dialogue = spawnedDialogues [0];
					spawnedDialogues.Remove (dialogue);
					Destroy (dialogue);
				}
				foreach (GameObject chatBubble in spawnedDialogues) {
					chatBubble.transform.localPosition += new Vector3 (0, 0.065f, 0);
				}
			}
			//if current dialogue index is even, spawn player else reply
			StartCoroutine (SpawnDialogue ());
		}
	}

	IEnumerator SpawnDialogue()
	{
		currentDialogue++;

		Transform spawnPos = (!isPlayerChat) ? ReplyChatBubblePosition.transform : PlayerChatBubblePosition.transform;
		GameObject waitDialogue = Instantiate (WaitDialoguePrefab, spawnPos.parent);

		waitDialogue.transform.position = spawnPos.position;

		yield return new WaitForSeconds (Random.Range (DelayBeforeDialogue.x, DelayBeforeDialogue.y));

		if (currentDialogue <= 10) {

			waitDialogue.transform.position = spawnPos.position;
			Destroy (waitDialogue);
			GameObject newChatBubble = Instantiate (DialoguePrefabs [currentDialogue - 1], spawnPos.parent);
			//newChatBubble.transform.position = spawnPos.transform.position;
			spawnedDialogues.Add (newChatBubble);

			yield return new WaitForSeconds (DelayBeforeStart);
			if (!isPlayerChat) {
				if (onDialogueSpawn != null) {
					onDialogueSpawn.Invoke ();
				}
			}
		} else if (currentDialogue > 10) {
			waitDialogue.transform.position = PlayerChatBubblePosition.transform.position;
			yield return new WaitForSeconds (Random.Range (DelayBeforeDialogue.x, DelayBeforeDialogue.y));
			Destroy (waitDialogue);
			GameObject newChatBubble = Instantiate (DialoguePrefabs [currentDialogue - 1], PlayerChatBubblePosition.transform.parent);
			//newChatBubble.transform.position = spawnPos.transform.position;
			spawnedDialogues.Add (newChatBubble);

			if (currentDialogue < DialoguePrefabs.Count && currentDialogue > 10) {
				PlayDialogue ();
			}
		} 
	}


	IEnumerator StartEndOfLine()
	{
		yield return new WaitForSeconds (2.5f);
		EndOfMessageGameObject.SetActive (true);
		yield return new WaitForSeconds (1.0f);
		GetComponent<Fade> ().startFade = true;
		yield return new WaitForSeconds (2.5f);
		UnityEngine.SceneManagement.SceneManager.LoadScene (0);
	}
}
