using UnityEngine;
using System.Collections;

public class HintController : MonoBehaviour {

    public float hinterval;

    public TextMesh meshPrefab;
    public float growTime;
    public int sizeIncrease;

    private IEnumerator coroutine;

    public void StopHints() {
        StopCoroutine(coroutine);
    }

    public void StartHints(string word) {
        coroutine = GiveHints(word);
        StartCoroutine(coroutine);
    }

    IEnumerator GiveHints(string word) {
        foreach (char letter in word) {
            yield return new WaitForSeconds(hinterval);

            StartCoroutine(GrowFadeLetter(letter));
        }
    }

    IEnumerator GrowFadeLetter(char letter) {
        float time = 0f;

        Vector3 position = transform.position;
        Quaternion rotation = transform.rotation;

        TextMesh textMesh = Object.Instantiate(meshPrefab, position, rotation) as TextMesh;
        textMesh.transform.parent = transform;
        textMesh.text = "" + letter;

        Color color = textMesh.color;
        float size = textMesh.fontSize;

        while (time < growTime) {
            textMesh.fontSize = (int) Mathf.Lerp(size, size + sizeIncrease, time / growTime);

            float newAlpha = Mathf.Lerp(color.a, 0f, time / growTime);
            textMesh.color = new Color(color.r, color.g, color.b, newAlpha);

            time += Time.deltaTime;

            yield return null;
        }

        Destroy(textMesh.gameObject);
    }
}
