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

public class DelayThenSwitch : MonoBehaviour {
	public float duration;
	public GameObject nextObj;
	public bool doFade;
	CanvasGroup fade;
	bool fadeIn = false;
	bool display = false;
	bool fadeOut = false;
	float timer;

	// Use this for initialization
	void Start () {
		timer = 0f;
		if(doFade){
			fade = GetComponent<CanvasGroup>();
			fade.alpha = 0f;
		}
	}
	
	// Update is called once per frame
	void Update () {
		timer += Time.deltaTime;
		if(doFade){
			if(!fadeIn){
				fade.alpha = timer / duration;
				if(fade.alpha >= 1f){
					timer = 0f;
					fadeIn = true;
				}
			}else if(!display){
				if(timer >= duration){
					display = true;
					timer = 0f;
				}
			}else if(!fadeOut){
				fade.alpha = 1f - (timer / duration);
				if(fade.alpha <= 0f){
					timer = 0f;
					fadeOut = true;
				}
			}else{
				if(nextObj != null)
					nextObj.SetActive(true);
				gameObject.SetActive(false);
			}
		}else{
			if(timer >= duration){
				if(nextObj != null)
					nextObj.SetActive(true);
				gameObject.SetActive(false);
			}
		}
	}
}
