using UnityEngine;
using System.Collections;

namespace Pix.Samples.TopDown
{
	public class Fader : MonoBehaviour
	{
		public bool FadeOnStartup = true;
        public float StartDelay = 0;
        public float Duration = 1;
		private float remaining = 0;
		private bool isFading = false;

        private IEnumerator Start()
		{
			remaining = Duration;
            if (FadeOnStartup)
            {
                if (StartDelay > 0)
                    yield return new WaitForSeconds(StartDelay);
                Fade();
            }
		}
		
		public virtual void Fade()
		{
			isFading = true;
		}

		private void Update()
		{
			if (!isFading)
				return;
			
			remaining -= Time.deltaTime;
			if (remaining <= 0)
				Destroy(gameObject);
			else
				HandleFade(remaining / Duration);
		}

        protected virtual void HandleFade(float progress)
        {
        }
	}
}