using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class GameManager : MonoBehaviour {

	public static event System.EventHandler<EventArgs<GameState>> OnGameStateChanged;
	public static event System.EventHandler<EventArgs<GameState>> OnBeforeGameStateChanged;
	public enum GameState {
		UI_STATE,
		PLAY_STATE
	}

	public GameState LastState {
		get {
			return lastState;
		}
	}
	private GameState lastState;
	public GameState CurrentState {
		get {
			return currentState;
		}
	}
	private GameState currentState;
	public static GameManager Instance;
	public UIScreen StartScreen;


	public void SetState(GameState newState) {

		if(OnBeforeGameStateChanged != null) {
			OnBeforeGameStateChanged(this,new EventArgs<GameState>(currentState));
		}


		lastState = currentState;

		currentState = newState;
		if(newState != GameState.UI_STATE) {
			UIScreen.GetScreen<GameHUDScreen>().Show();
		}

		if(OnGameStateChanged != null) {
			OnGameStateChanged(this,new EventArgs<GameState>(currentState));
		}

		
	}

	public void GameOver() {
		EnemyManager.instance.Reset();

		Shot[] s = FindObjectsOfType<Shot>();
		foreach(Shot sh in s) {
			Destroy(sh.gameObject);
		}

		UIScreen.GetScreen<GameOverScreen>().Show();
		PlayerShipController.instance.Reset();
	}

	public void Reset() {
		WaveManager.instance.Reset();
		EnemyManager.instance.Reset();
		

		UIScreen.GetScreen<MainMenuScreen>().Show();
	}

	// Use this for initialization
	void Awake () {
		if(GameManager.Instance == null) {
			GameManager.Instance = this;
		}
	}

	void Start() {
		lastState = GameState.UI_STATE;
		currentState = GameState.UI_STATE;
		//Ini Game
		StartScreen.Show();
	}

	
	
	
}
