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

public class DogOutputPanel : MonoBehaviour
{
	public Text text = null;
	public float tickRate = 0.05f;

	private IEnumerator slowTextCoroutine = null;

	private void OnEnable()
	{
		DogManager.OnSpeak += OutputDogSpeech;
	}

	private void OnDisable()
	{
		DogManager.OnSpeak -= OutputDogSpeech;
	}

	private void OutputDogSpeech(string text)
	{
		if (slowTextCoroutine != null)
		{
			StopCoroutine(slowTextCoroutine);
		}

		slowTextCoroutine = SlowText(text);
		StartCoroutine(slowTextCoroutine);
	}

	private IEnumerator SlowText(string slowText)
	{
		for (int i=0; i<=slowText.Length; i++)
		{
			text.text = slowText.Substring(0, i);
			yield return new WaitForSeconds(tickRate);
		}
	}
}
