using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameSceneManager : MonoBehaviour
{
    //AsyncOperation async = null;

    //public Image ProgressImage;
    //public RectTransform LoadingUI;
    //public Text txtLoading;

    // public float FadeSpeed = 5;

    //  Animator animator;

    void Awake()
    {
        //animator = GetComponentInChildren<Animator>();
        //var splashLoader = LoadingUI.GetComponent<SplashLoader>();
        //splashLoader.OnFadeInComplete = SplashFadeIn;
        //splashLoader.OnFadeOutComplete = SplashFadeOut;
        DontDestroyOnLoad(this.gameObject);
        LoadTitle();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
            Application.Quit();
#endif
        }

        //if (async != null && ProgressImage != null && LoadingUI != null)
        //{
        //    if (!LoadingUI.gameObject.activeSelf)
        //        LoadingUI.gameObject.SetActive(true);

        //    ProgressImage.fillAmount = async.progress;

        //    if (txtLoading != null)
        //        txtLoading.text = string.Format("Loading...{0}%", Mathf.RoundToInt(async.progress * 100.0f));

        //    if (async.progress > 0.0f)
        //    {
        //        animator.SetFloat("Fade", 0);
        //    }
        //}
    }

    //void SplashFadeIn()
    //{ }
    //void SplashFadeOut() { }

    //private IEnumerator FadeOut()
    //{

    //    //    yield return new WaitForSeconds(0.2f);
    //    //    var canvasGroup = LoadingUI.GetComponent<CanvasGroup>();
    //    //    while (canvasGroup.alpha > 0f)
    //    //    {
    //    //        canvasGroup.alpha -= Time.deltaTime * FadeSpeed;
    //    //        yield return new WaitForEndOfFrame();
    //    //    }
    //    //    LoadingUI.gameObject.SetActive(false);
    //    //    async = null;
    //    //    ProgressImage.fillAmount = 0;
    //    //    if (txtLoading != null)
    //    //        txtLoading.text = string.Format("Loading...{0}%", 0);

    //}

    //private IEnumerator LoadLevel(string levelName)
    //{
    //    if (!LoadingUI.gameObject.activeSelf)
    //        LoadingUI.gameObject.SetActive(true);

    //    animator.SetFloat("Fade", 1);

    //    var canvasGroup = LoadingUI.GetComponent<CanvasGroup>();
    //    while (canvasGroup.alpha < 1.0f)
    //    {
    //        canvasGroup.alpha += Time.deltaTime * FadeSpeed;
    //        yield return new WaitForEndOfFrame();
    //    }

    //    yield return new WaitForSeconds(0.2f);

    //    async = SceneManager.LoadSceneAsync(levelName);
    //    yield return async;

    //}


    public void LoadGameScene()
    {
        SceneManager.LoadSceneAsync("Game");
        //StartCoroutine(LoadLevel("Game"));
    }

    public void LoadTitle()
    {
        //StartCoroutine(LoadLevel("Title"));
        SceneManager.LoadSceneAsync("Title");
    }

    public void LoadScene(string sceneName)
    {
        //StartCoroutine(LoadLevel(sceneName));
        SceneManager.LoadSceneAsync(sceneName);
    }
}
