using UnityEngine;
using System.Collections;

public enum GameStatus{
	prepare,
	ingame,
	gameover
};

public class GameManager : MonoBehaviour {

	public static GameManager instance;
	public GameObject Bullets;
	public Transform BulletOutlet;
	public GameObject[] astroids;
	public GameObject generateAreas;
	public AutoGenerateCtrl rightGenerateArea;
	public float scaleMin = 0.5f;
	public float scaleMax = 1.2f;

	Vector3 cameraArea;
	GameObject astroidList;
	GameObject bulletList;
	GameStatus gameStatus;
	float score=0;
	ArrayList randomMode;
	float gameOverWait = 1.5f;

	void Awake(){
		instance = this;
		cameraArea = new Vector3 ();
		Vector3 p1 = Camera.main.ViewportToWorldPoint(new Vector3(1,1,Camera.main.nearClipPlane));
		Vector3 p0 = Camera.main.ViewportToWorldPoint(new Vector3(0,0,Camera.main.nearClipPlane));
		cameraArea = p1 - p0;
		astroidList = new GameObject ("AstroidList");
		bulletList = new GameObject ("BulletList");
		gameStatus = GameStatus.prepare;
		randomMode = new ArrayList ();
	}

	// Use this for initialization
	void Start () {
		//StartGameMode (0);
		gameStatus = GameStatus.prepare;
		UICtrl.instance.DisplayPrepareUI();
	}
	
	// Update is called once per frame
	void Update () {
		switch(gameStatus){
		case GameStatus.prepare:
			if(Input.GetMouseButtonDown(0)){
				UICtrl.instance.PrepareActive();
			}
			break;
		case GameStatus.ingame:
			score += Time.deltaTime;
			break;
		case GameStatus.gameover:
			if(gameOverWait>0){
				gameOverWait-= Time.deltaTime;
			}else{
				if(Input.GetMouseButtonDown(0)){
					UICtrl.instance.CloseGameOverUI();
					gameStatus = GameStatus.prepare;
					UICtrl.instance.DisplayPrepareUI();
					generateAreas.SetActive (false);
				}
			}
			break;
		}
	}

	public GameObject GenerateAstroids(int index, Vector3 position){
		if(index<0||index>=astroids.Length){
			index = Random.Range(0, astroids.Length);
		}
		GameObject newAstroid = Instantiate (astroids [index], position, Quaternion.identity) as GameObject;
		newAstroid.transform.localScale *= Random.Range (scaleMin, scaleMax);
		newAstroid.transform.parent = astroidList.transform;
		return newAstroid;
	}

	public Vector3 GetCameraArea(){
		return cameraArea;
	}

	public void GameOver(){
		PlayerController.instance.GameOver ();
		UICtrl.instance.EnableGameOverUI((int)score);
		gameStatus = GameStatus.gameover;
		SoundFXCtrl.instance.PlaySound (9, 0.5f);
		gameOverWait = 1.5f;
	}

	public void StartGameMode(int index){
		ClearAstroids ();
		ClearBullets ();
		PlayerController.instance.GameStart (index);
		generateAreas.SetActive (true);
		rightGenerateArea.safeTime = 8;
		score = 0;
		gameStatus = GameStatus.ingame;
		UICtrl.instance.EnableInGameUI (index);
	}

	void ClearAstroids(){
		foreach(Transform child in astroidList.transform){
			Destroy(child.gameObject);
		}
	}

	void ClearBullets(){
		foreach(Transform child in bulletList.transform){
			Destroy(child.gameObject);
		}
	}

	public GameStatus currentGameStatus(){
		return gameStatus;
	}

	public int currentScore(){
		return (int)score;
	}

	public void ShootBullets(Vector3 direction){
		GameObject bullet = Instantiate (Bullets, BulletOutlet.position, Quaternion.identity) as GameObject;
		bullet.transform.right = direction;
		bullet.transform.parent = bulletList.transform;
	}

	public int GetRandomMode(){
		if(randomMode.Count==0){
			for(int i = 0;i<3;i++){
				randomMode.Add(i);
			}
		}
		int randomIndex = Random.Range (0, randomMode.Count);
		int result = (int)randomMode[randomIndex];
		randomMode.RemoveAt (randomIndex);
		return result;
	}

}
