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

public class GameOver_Manager : MonoBehaviour {
    [SerializeField]
    GameObject obj;
    [SerializeField]
    TextMesh clear;
    [SerializeField]
    SpriteRenderer BackGround;
    [SerializeField]
    Sprite DeathImage;
    // Use this for initialization
    void Start() {
        clear.text = "";
        if (ClearFlag == false) {
            BackGround.sprite = DeathImage;
            obj.GetComponent<ColorFade>().Fadein();
            clear.text = "Game Over";
            clear.color = Color.red;
        }
    }

    int Switch = 0;
    float timer = 0.0f;
    public static bool ClearFlag = true;
    // Update is called once per frame
    void Update() {
        if (ClearFlag) {
            // ゲームクリア
            switch (Switch) {
                case 0:
                    timer += Time.deltaTime;
                    if (timer > 1) {
                        clear.text = "Game Clear";
                        GetComponent<AudioSource>().Play();
                        Switch++;
                    }
                    break;
                case 1:
                    if (Input.anyKeyDown) {
                        obj.GetComponent<ColorFade>().Fadein(2.0f);
                        GetComponent<await>().start(2.5f);
                        Switch++;
                    }
                    break;
                case 2:
                    if (GetComponent<await>().await_finish()) {
                        SceneManager.LoadScene("Title");
                        Debug.Log("終了");
                        Switch++;
                    }
                    break;
            }
        }else {
            // ゲームオーバー
            switch (Switch) {
                case 0:
                    obj.GetComponent<ColorFade>().Fadeout(1.0f);
                    GetComponent<await>().start(1.0f);
                    Switch++;
                    break;
                case 1:
                    if (GetComponent<await>().await_finish()) {
                        Switch++;
                    }
                    break;
                case 2:
                    if (Input.anyKeyDown) {
                        obj.GetComponent<ColorFade>().Fadein(2.0f);
                        GetComponent<await>().start(2.5f);
                        Switch++;
                    }
                    break;
                case 3:
                    if (GetComponent<await>().await_finish()) {
                        SceneManager.LoadScene("Title");
                        Debug.Log("終了");
                        Switch++;
                    }
                    break;
            }
        }
    }
}