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

public class FadeIn : MonoBehaviour {

    public Image image;
    public Text wereWorld;
    public Text thanksForPlaying;
    public GameObject turtle;
    public AudioSource music;

	void Update () {
        turtle = GameObject.Find("turtle");
        Movement controls = Game.player.GetComponent<Movement>();
        controls.enabled = false;
        StartCoroutine(DropTurtle());
        StartCoroutine(FadeMusic());
        thanksForPlaying.canvasRenderer.SetAlpha(0.0f);
        wereWorld.canvasRenderer.SetAlpha(0.0f);
        image.canvasRenderer.SetAlpha(0.0f);
        image.CrossFadeAlpha(1f, 10f, true);
        StartCoroutine(DelayedText());
        this.enabled = false;
	}

    IEnumerator DropTurtle()
    {
        for (float t = 0; t < 10f; t += Time.deltaTime)
        {
            turtle.transform.Translate(0, -t * 0.1f, 0);
            yield return new WaitForEndOfFrame();
        }
    }

    IEnumerator FadeMusic()
    {
        for (float t = 1f; t > 0f; t -= Time.deltaTime)
        {
            music.volume = t;
            yield return new WaitForEndOfFrame();
        }
    }

    IEnumerator DelayedText()
    {
        yield return new WaitForSecondsRealtime(9);
        wereWorld.CrossFadeAlpha(1f, 1f, true);
        yield return new WaitForSecondsRealtime(3);
        thanksForPlaying.CrossFadeAlpha(1f, 1f, true);
    }
}
