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

public abstract class LaunchManagement {
	private static Transform allTemplates;
	private static List<Tuple<GameObject, GameObject>> panelsAndButtons; //where to clone the new UI, depending on their tab

	public static void init() {
		allTemplates = GameObject.Find("Templates/Launch&Command").transform;

		panelsAndButtons = new List<Tuple<GameObject, GameObject>>();
		panelsAndButtons.Add(getPanel("Helo"));

		GameController.GUI.SetActive(false);
	}

	private static Tuple<GameObject, GameObject> getPanel(string name) {
		return new Tuple<GameObject, GameObject>(GameObject.Find("Canvas/GUI/Launch&Command/" + name), GameObject.Find("Canvas/GUI/Launch&Command/Tabs/" + name));
	}

	public static void constructLaunchAndCommand(DeployedUnit selectedUnit) {
		foreach(Tuple<GameObject, GameObject> tab in panelsAndButtons) {
			GameController.deleteAllChildren(tab._1.transform);
			tab._2.SetActive(false);
		}

		//Helo
		GameController.deleteAllChildren(panelsAndButtons[0]._1.transform);
		GameObject helo = allTemplates.transform.Find("Helo").gameObject;
		int count = 0;
		foreach(Class launched in selectedUnit.getClass().getLaunchedClasses()) {
			panelsAndButtons[0]._2.SetActive(true);

			GameObject clone = GameObject.Instantiate(helo, panelsAndButtons[0]._1.transform);

			clone.GetComponent<RectTransform>().sizeDelta = new Vector2(0, 0);
			clone.transform.localPosition = new Vector3(0, -count * clone.GetComponent<RectTransform>().rect.height);

			clone.transform.Find("Name").GetComponent<Text>().text =  launched.getName();
			//TODO : change image

			clone.GetComponent<LaunchBehaviour>().launchedClass = launched;
			clone.GetComponent<LaunchBehaviour>().loadout = launched.getLoadout("Attack"); //TEMPORARY

			count++;
		}

		bool needToChangeTab = false;
		foreach(Tuple<GameObject, GameObject> tab in panelsAndButtons) {
			if (!tab._2.activeSelf) {
				OnOffButton button = tab._2.GetComponent<OnOffButton>();

				if (button.state) {
					needToChangeTab = true;
					break;
				}
			}
		}

		if (needToChangeTab) {
			foreach(Tuple<GameObject, GameObject> tab in panelsAndButtons) {
				if (tab._2.activeSelf) {
					OnOffButton button = tab._2.GetComponent<OnOffButton>();
					button.click();
				}
			}
		}
	}

	public static void updateLaunchAndCommand(DeployedUnit selectedUnit) {
		//Helo
		if (panelsAndButtons[0]._1.activeSelf) {
			foreach(Transform child in panelsAndButtons[0]._1.transform) {
				LaunchBehaviour behaviour = child.gameObject.GetComponent<LaunchBehaviour>();

				int left = selectedUnit.getNumberIdleStored(behaviour.launchedClass);

				child.Find("Left/Text").gameObject.GetComponent<Text>().text = "LEFT : " + left;
				behaviour.max = left;
				behaviour.currentNumber = Mathf.Max(Mathf.Min(behaviour.currentNumber, behaviour.max), 0);

				string canLaunchedMessage = selectedUnit.canLaunchMessage(behaviour.launchedClass, behaviour.currentNumber, behaviour.loadout);

				child.Find("LaunchButton").GetComponent<Button>().interactable = (canLaunchedMessage == "");
				child.Find("LaunchButton").GetComponent<ToolTip>().text = canLaunchedMessage;

				child.Find("Number/Text").GetComponent<Text>().text = behaviour.currentNumber + "";
				//child.Find("Number/-").GetComponent<Button>().interactable = (behaviour.currentNumber > 1);
				//child.Find("Number/+").GetComponent<Button>().interactable = (behaviour.currentNumber < left);

				child.Find("Launching").gameObject.SetActive(selectedUnit.isLaunching(behaviour.launchedClass) && (Time.frameCount % 30) < 15);
				child.Find("Preparing").gameObject.SetActive(selectedUnit.isPreparing(behaviour.launchedClass) && (Time.frameCount % 30) >= 15);
			}
		}
	}
}
