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

public class GameManager : MonoBehaviour {
	public static GameManager instance;

	public GameObject MainUI;
	public Surfer[] players = new Surfer[8];

	public int playersAlive = 0;

	private bool inPlay = false;
	private bool single = false;

	// Use this for initialization
	void Start () {
		instance = this;
	}

	//Called by user to reset configs
	public void RestartScene(){
		SceneManager.LoadScene ("Main");
	}

	// Update is called once per frame
	void Update () {
		if (inPlay && playersAlive == 0) {
			Restart ();
			inPlay = false;
		}
	}

	public void PlayerEliminated(){
		playersAlive--;
		Debug.Log ("Eliminated, " + playersAlive);
	}
		
	public bool Win(){
		Debug.Log ("TRYING");
		if (inPlay&&!single) {
			Invoke ("Restart", 1f);
			inPlay = false;
			return true;
		}
		return false;
	}

	private void Restart(){
		SoundManager.instance.ChangeMusic ();
		Wave.instance.Stop ();
		MainUI.SetActive (true);
	}

	public void resetPlayers(){
		playersAlive = 0;
	}

	private void Play(){
		if (inPlay)
			return;
		
		Debug.Log ("Game Started with " + playersAlive + " players");
		inPlay = true;

		if (playersAlive == 1)
			single = true;
	}
		
	public void Prepare(){
		Debug.Log ("Player registered");
		playersAlive++;
		SoundManager.instance.ChangeMusic ();

		Invoke ("Play", 1f);
	
	}

	public void Quit(){
		Application.Quit ();
	}
}
