using UnityEngine;
using System.Collections;

public class Main : Singleton<Main>
{
    public enum Scene
    {
        menu,
        game,
    }

    public Mg_Player mgPlayer;
    public Mg_Audio mgAudio;
    public GameObject gCamera;

    private ScreenOverlay overlay;

    public float timePlayedThisLevel, highscore;
    bool gameOver = false;


    void Awake()
    {
        mgPlayer = Mg_Player.Instance;
        Screen.showCursor = false;
        //mgAudio = Mg_Audio.Instance;
    }

	void Start () 
    {
        StartCoroutine(mgPlayer.Spawn_R(Vector3.zero, Vector3.up, true));
        mgAudio.Play("Background", 1, 1, true);
	}
	
	void Update () 
    {
        if (Input.GetKey(KeyCode.Escape)) Application.Quit();

        if (!gameOver)
        {
            timePlayedThisLevel += Time.deltaTime;
        }
        else
        {
            if (Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter)) Application.LoadLevel("Splash");
        }
	}

    public void GameOver(float totalTimePlayed, int highScore)
    {
        if (gameOver) return;

        overlay = gCamera.GetComponentInChildren<ScreenOverlay>();
        overlay.intensity = 2f;

        {
            GameObject theText = new GameObject("Gameover!");
            theText.AddComponent("TextMesh");
            TextMesh textMesh = theText.GetComponent<TextMesh>();
            textMesh.font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
            textMesh.renderer.sharedMaterial = textMesh.font.material;
            textMesh.fontSize = 120;
            textMesh.text = "GAME OVER!";

            float center = -textMesh.renderer.bounds.size.x / 2f;
            Vector3 test = Mg_Camera.Instance.mainCamera.ScreenToWorldPoint(new Vector3(center, Screen.height / 1.1f));
            theText.transform.position = new Vector3(center, test.y);
        }

        {
            GameObject theText = new GameObject("Highscore!");
            theText.AddComponent("TextMesh");
            TextMesh textMesh = theText.GetComponent<TextMesh>();
            textMesh.font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
            textMesh.renderer.sharedMaterial = textMesh.font.material;
            textMesh.fontSize = 90;
            textMesh.text = "Highscore: " + (int)highscore;

            float center = -textMesh.renderer.bounds.size.x / 2f;
            Vector3 test = Mg_Camera.Instance.mainCamera.ScreenToWorldPoint(new Vector3(center, Screen.height / 3f));
            theText.transform.position = new Vector3(center, test.y);
        }

        {
            GameObject theText = new GameObject("TimeElapsed");
            theText.AddComponent("TextMesh");
            TextMesh textMesh = theText.GetComponent<TextMesh>();
            textMesh.font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
            textMesh.renderer.sharedMaterial = textMesh.font.material;
            textMesh.fontSize = 90;
            textMesh.text = "Survived: " + (int)timePlayedThisLevel + "s";

            float center = -textMesh.renderer.bounds.size.x / 2f;
            Vector3 test = Mg_Camera.Instance.mainCamera.ScreenToWorldPoint(new Vector3(center, Screen.height / 4f));
            theText.transform.position = new Vector3(center, test.y);
        }

        gameOver = true;
    }
}
