using UnityEngine;
using UnityEngine.UI;
using System.Collections;

// main control script
public class Game : MonoBehaviour
{
	static Game instance = null;
	[SerializeField] SpaceStateController starmap = null; // Set from editor - star map
	[SerializeField] SpaceStateController system = null; // Set from editor - current star system
	[SerializeField] SpaceStateController localSpace = null; // Set from editor - hero ship space
	[SerializeField] SpaceStateController planetSpace = null; // Set from editor - hero ship space
	[SerializeField] SpaceStateController battleSpace = null; // Set from editor - hero ship battle space

	SpaceStateController currentStateObject = null;

	int currentState = 1;

	void Start ()
	{
		instance = this;
//		Player.info.StarId = StarMap.GetNearestStar (Vector3.zero); // in center of map
//		Player.Load ();
		starmap.SetActive (false);
		system.SetActive (false);
		localSpace.SetActive (false);
		planetSpace.SetActive (false);
		battleSpace.SetActive (false);
		if (Player.info == null)
		{
			Player.Load();
		}
		// TODO
		SetState (0);
		BaseUI.Show<StarMapUI> ();
//		BattleState.StartBattle ();
	}

	void Update ()
	{
		if (Input.GetKeyDown (KeyCode.Escape))
		{
			Application.Quit();
		}
	}

	public static void SetGameState (int state = 0)
	{
		if (instance != null)
		{
			instance.SetState(state);
		}
	}

	void SetState(int state = 0)
	{
		// set SpaceStateController
		#region set
		if (state == currentState)
		{
			return;
		}
		int previosRelativeSize = 100;
		// Hide old
		if (currentStateObject != null)
		{
			previosRelativeSize = currentStateObject.relativeSize;
			currentStateObject.SetActive(false);
		}
		switch (state)
		{
		case 0:
			currentStateObject = starmap;
			break;
		case 1:
			currentStateObject = system;
			break;
		case 2:
			currentStateObject = localSpace;
			break;
		case 3:
			currentStateObject = planetSpace;
			break;
		case 4:
			currentStateObject = battleSpace;
			break;
		}
		currentState = state;
		if (currentStateObject != null)
		{
			currentStateObject.SetActive(true);
			currentStateObject.OnStateShow(previosRelativeSize > currentStateObject.relativeSize);
		}
		#endregion
	}
}