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

public class PlanetarUI : BaseUI
{
	[SerializeField]Button dropScannerButton = null; // Set from editor
	[SerializeField]Text dropScannerCountLabel = null; // Set from editor
	[SerializeField]Text debugResourceInfo = null; // Set from editor

	void Start()
	{
		Logic.Subscribe (Res.dropScanner, OnChangeScannerCount);
		OnChangeScannerCount (Logic.triggers [Res.dropScanner]);
		// TODO test
		Logic.triggers [Res.dropScanner] = 5; // give 5 drop scanners
		Logic.Subscribe (UpdateResourceInfo);
		UpdateResourceInfo ();
	}

	void UpdateResourceInfo()
	{
		if (debugResourceInfo != null)
		{
			string[] titles = {
				"fuel:",
				"",
				"scanner:",
				"missles:",
				"",
				"resources",
				"antimatter:",
				"rare:",
				"transuranic:",
				"bio:",
				"artifacts:",
				"info-sources:"
			};
			string[] resources = {
				Res.fuel,
				"",
				Res.dropScanner,
				Res.missles,
				"",
				"",
				Res.antimatter,
				Res.rareMaterials,
				Res.transuranicMaterials,
				Res.biologicalMaterials,
				Res.artificalMaterials,
				Res.infoSourceMaterials
			};
			string res = "";
			for(int i = 0; i < titles.Length || i < resources.Length; i++)
			{
				res += titles[i];
				if (resources[i] != "")
				{
					res += " " + Logic.triggers[resources[i]].ToString();
				}
				res += "\n";
			}
			debugResourceInfo.text = res;
		}
	}

	public void OnStarSystemClick()
	{
		Player.Save ();
		CameraController.SetNeedDistance (30);
		BaseUI.AsyncShowWithLoaderUI<StarSystemUI, LoaderUI> (0, () => {
			Game.SetGameState (1);
		});
	}

	public void OnShipDetailsClick()
	{
		Player.Save ();
		CameraController.SetNeedDistance (0.5f);
		BaseUI.AsyncShowWithLoaderUI<ShipDetailsUI, LoaderUI> (0, () => {
			Game.SetGameState (2);
		});
	}

	public void OnChangeScannerCount(int dropScannerCount)
	{
		if (dropScannerButton != null)
		{
			dropScannerButton.interactable = (dropScannerCount > 0);
		}
		if (dropScannerCountLabel != null)
		{
			dropScannerCountLabel.text = dropScannerCount.ToString();
		}
	}

	public void OnDropScannerClick()
	{	
		if (Logic.triggers [Res.dropScanner] > 0)
		{
			// TODO make normal cancel button
			PlanetarSpaceState.DropSensorMode = !PlanetarSpaceState.DropSensorMode;
		}
	}
	
	public void OnScanClick()
	{
		Debug.Log ("OnScanClick");
	}

	void Update()
	{
		if (Input.GetKeyUp (KeyCode.Space))
		{
			BaseUI.AsyncShowWithLoaderUI<DialogUI, LoaderUI> ();
		}
	}
}
