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

public class GameManager : MonoBehaviour
{
    public GameObject OriginalNPC;
    public Text txtTimer;
    public Text txtWaves;
    public Text txtFinal;
    public Text txtInstructions;
    public GameObject PlayerOBJ;
    public float TimeToEnd;

	// Use this for initialization
	void Start ()
    {
        TimeToEnd = 3 * 60f;
        PlayerOBJ = GameObject.FindGameObjectWithTag("Player");
        for (int i = 0; i < 100; i++)
        {
            bool Created = false;
            Vector3 PosNPC = new Vector3();
            while (!Created)
            {
                PosNPC = new Vector3(Random.Range(-75f, 75f), 4f, Random.Range(-75f, 75f));
                if (Physics.OverlapBox(PosNPC,Vector3.one * 3f).Length == 0)
                {
                    Created = true;
                }
            }
            GameObject NewNPC = Instantiate(OriginalNPC,PosNPC, OriginalNPC.transform.rotation) as GameObject;
            NewNPC.name = "NPC";
        }
	}

    // Update is called once per frame
    void Update()
    {
        if (Time.timeSinceLevelLoad > TimeToEnd)
        {
            if (txtWaves.enabled)
                txtWaves.enabled = false;

            if (txtTimer.enabled)
                txtTimer.enabled = false;

            if (!txtInstructions.enabled)
                txtInstructions.enabled = true;

            if (txtFinal.text == "")
                txtFinal.text = "YOU SAID HI TO:\n" + PlayerOBJ.GetComponent<MovePlayer>().Waved.ToString();

            if (Input.GetKeyDown(KeyCode.Return))
                SceneManager.LoadScene("Q");

            if (Input.GetKeyDown(KeyCode.Escape))
                Application.Quit();

            if (PlayerOBJ.GetComponent<MovePlayer>() != null)
                Destroy(PlayerOBJ.GetComponent<MovePlayer>());

            if (!txtInstructions.enabled)
                txtInstructions.enabled = true;
        }
        else
        {
            txtWaves.text = "Waves: " + PlayerOBJ.GetComponent<MovePlayer>().Waved.ToString();
            float fTimeLeft = TimeToEnd - Time.timeSinceLevelLoad;
            int TimeLeft = (int)fTimeLeft + 1;
            txtTimer.text = (TimeLeft / 60).ToString() + ":";
            if ((TimeLeft % 60) < 10)
                txtTimer.text += "0" + (TimeLeft % 60).ToString();
            else
                txtTimer.text += (TimeLeft % 60).ToString();
        }
    }
}
