using UnityEngine;
using UnityEngine.UI;

public class Retry : MonoBehaviour {

    private const float fps = 30f;

    public const float fadeDuration = 2f;

    public const float stayTime = 2f;

    private static Text text = null;

    private static bool fadeStarted = false;

    private static float alpha = 0;

	void Start () {
        text = GetComponent<Text>();
        text.enabled = false;
        alpha = 0;
        text.color = new Color(1, 1, 1, alpha);
        fadeStarted = false;
	}
	
	void Update () {
	    if (Bed.mustSleep && !fadeStarted) {
            fadeStarted = true;
            Invoke("DoFade", Sun.waitBeforeDying);
        }
	}

    void DoFade() {
        text.enabled = true;
        InvokeRepeating("FadeIn", 0, 1 / fps);
    }

    void FadeIn() {
        alpha += 1 / (fps * fadeDuration);
        text.color = new Color(1, 1, 1, alpha);
        if (alpha > 1) {
            alpha = 1;
            Invoke("DoFadeOut", stayTime);
            CancelInvoke("FadeIn");
        }
    }

    void DoFadeOut() {
        InvokeRepeating("FadeOut", 0, 1 / fps);
    }

    void FadeOut() {
        alpha -= 1 / (fps * fadeDuration);
        text.color = new Color(1, 1, 1, alpha);
        if (alpha < 0) {
            alpha = 0;
            CancelInvoke("FadeOut");
        }
    }
}