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


//Manages the UI of the overall status of the selected unit (leftmost part of the gui)
public abstract class StatusManagement {

	private static GameObject templateModuleIcon;
	private static GameObject templateUnitSilhouette;
	private static List<GameObject> statuseTemplates;

	public static void init() {
		GameController.GUI.SetActive(true);
		statuseTemplates = new List<GameObject>();
		foreach(Transform status in GameObject.Find("Canvas").transform.FindChild("GUI/Statuses")) {
			statuseTemplates.Add(status.gameObject);
		}

		templateModuleIcon = GameObject.Find("Templates/ModuleIcon");
		templateModuleIcon.SetActive(false);
		templateUnitSilhouette = GameObject.Find("Templates/UnitSilhouette");
	}

	private static GameObject getStatusTemplate(ClassType classType) {
		if (classType == ClassType.Ship) {
			return statuseTemplates[0];
		}
		else {
			return statuseTemplates[0];
		}
	}

	public static void constructStatus(DeployedUnit selectedUnit) {
		GameObject status = getStatusTemplate(selectedUnit.getClass().getClassType());

		status.transform.FindChild("UnitClass").GetComponent<Text>().text = selectedUnit.getClass().getName();


		//Set the right speed button
		int count = 0;
		foreach(Transform child in status.transform.FindChild("SpeedList")) {
			if (count == selectedUnit.getSpeedButtonIndex()) {
				child.gameObject.GetComponent<OnOffButton>().click();
			}

			count++;
		}

		GameController.deleteAllChildren(status.transform.FindChild("ModulesStatus"));
		foreach(Tuple<Module, ModuleData> tuple in selectedUnit.getModules()) {
			if (tuple._1.getSprite() != null) {
				GameObject clone = GameObject.Instantiate(templateModuleIcon, status.transform.FindChild("ModulesStatus").transform);
				clone.SetActive(true);

				Vector scaledPosition = tuple._2.getPosition() * selectedUnit.getClass().getSilhouetteScale() * status.transform.FindChild("ModulesStatus").GetComponent<RectTransform>().rect.width;
				clone.GetComponent<Image>().sprite = tuple._1.getSprite();
				clone.transform.localPosition = new Vector3(scaledPosition.x(), scaledPosition.z(), 0);

				clone.GetComponent<ModuleBehavior>().module = tuple;
			}
		}
	}

	public static void updateStatus(DeployedUnit selectedUnit) {
		GameObject status = getStatusTemplate(selectedUnit.getClass().getClassType());

		status.transform.FindChild("ButtonEMCON").GetComponent<OnOffButton>().setState(selectedUnit.isEMCON());
		status.transform.FindChild("ButtonDisplayRadar").GetComponent<OnOffButton>().setState(selectedUnit.isDisplayingRadar());
		status.transform.FindChild("ButtonStop").GetComponent<OnOffButton>().setState(selectedUnit.getPath() == null);

		status.transform.FindChild("SpeedIndicator/SpeedValue/SpeedValueText").GetComponent<Text>().text = (int)selectedUnit.getcurrentSpeed() + "m/s";

		FuelType type = selectedUnit.getMainFuelType();
		float consumption = selectedUnit.getConsumption(type) * 1000;

		status.transform.FindChild("RangeIndicator").gameObject.SetActive(consumption > 0.0f);
		if (consumption > 0.0f) {
			status.transform.FindChild("RangeIndicator/RangeUnit/RangeUnitText").GetComponent<Text>().text = (int)(selectedUnit.getFuelLeft(type) / consumption * selectedUnit.getcurrentSpeed()) + " km";
		}

		foreach(Transform child in status.transform.FindChild("ModulesStatus")) {
			GameObject gameObject = child.gameObject;
			Tuple<Module, ModuleData> tuple = gameObject.GetComponent<ModuleBehavior>().module;

			gameObject.GetComponent<Image>().color = Util.getColorFromValue(tuple._2.getHealth() / tuple._1.getHealth());
		}
	}
}
