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

public abstract class FireManagement {
	private static Transform allMissiles;
	private static List<Tuple<GameObject, GameObject>> panelsAndButtons; //where to clone the missiles' UI, depending on their tab

	public static void init() {
		allMissiles = GameObject.Find("Templates/Fire").transform;

		panelsAndButtons = new List<Tuple<GameObject, GameObject>>();
		panelsAndButtons.Add(getPanel("SAM"));
		panelsAndButtons.Add(getPanel("Strike"));
		panelsAndButtons.Add(getPanel("CIWS"));
	}

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

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

		GameObject SAM = allMissiles.transform.Find("SAM").gameObject;
		int count = 0;
		foreach(Class antiMissile in selectedUnit.getClass().getMissileAutoDef()) {
			panelsAndButtons[0]._2.SetActive(true);

			GameObject clone = GameObject.Instantiate(SAM, 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 = antiMissile.getName();
			//TODO : change image

			clone.GetComponent<SAMBehaviour>().missile = antiMissile;

			int num = selectedUnit.getAntimissileSalvo(antiMissile);
			foreach(Transform child in clone.transform.Find("Numbers")) {
				num --;

				child.gameObject.GetComponent<OnOffButton>().setState(num == 0);
			}

			count++;
		}

		//Strike
		GameObject strike = allMissiles.transform.Find("Strike").gameObject;
		count = 0;
		foreach(Class missile in selectedUnit.getClass().getMissilesStrike()) {
			panelsAndButtons[1]._2.SetActive(true);

			GameObject clone = GameObject.Instantiate(strike, panelsAndButtons[1]._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 = missile.getName();
			//TODO : change image

			clone.GetComponent<MissileStrikeBehaviour>().missile = missile;
			clone.GetComponent<MissileStrikeBehaviour>().currentNumber = 1;

			count++;
		}

		//CIWS
		GameObject ciws = allMissiles.transform.Find("CIWS").gameObject;
		count = 0;
		foreach(Tuple<Module, ModuleData> tuple in selectedUnit.getModules()) {
			if (tuple._1 is CIWSModule) {
				panelsAndButtons[2]._2.SetActive(true);

				GameObject clone = GameObject.Instantiate(ciws, panelsAndButtons[2]._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 = tuple._1.getName();
				//TODO : change image

				clone.GetComponent<CIWSBehaviour>().data = (CIWSModuleData)tuple._2;
				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 updateFire(DeployedUnit selectedUnit) {
		//SAM
		if (panelsAndButtons[0]._1.activeSelf) {
			foreach(Transform child in panelsAndButtons[0]._1.transform) {
				SAMBehaviour behaviour = child.gameObject.GetComponent<SAMBehaviour>();
				child.Find("Firing").gameObject.SetActive(selectedUnit.isFiring(behaviour.missile) && (Time.frameCount % 30) < 15);
			}
		}
		//Strike
		else if (panelsAndButtons[1]._1.activeSelf) {
			foreach(Transform child in panelsAndButtons[1]._1.transform) {
				MissileStrikeBehaviour behaviour = child.gameObject.GetComponent<MissileStrikeBehaviour>();

				int left = selectedUnit.getNumberReadyToLaunch(behaviour.missile);

				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 canFireMessage = selectedUnit.canFireMessage(behaviour.missile, behaviour.currentNumber, selectedUnit.getTarget());

				child.Find("FireButton").GetComponent<Button>().interactable = (canFireMessage == "");
				child.Find("FireButton").GetComponent<ToolTip>().text = canFireMessage;

				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("Firing").gameObject.SetActive(selectedUnit.isFiring(behaviour.missile) && (Time.frameCount % 30) < 15);
			}
		}
		//CIWS
		else if (panelsAndButtons[2]._1.activeSelf) {
			foreach(Transform child in panelsAndButtons[2]._1.transform) {
				CIWSBehaviour behaviour = child.gameObject.GetComponent<CIWSBehaviour>();

				child.Find("Firing").gameObject.SetActive(behaviour.data.isFiring() && (Time.frameCount % 30) < 15);
			}
		}
	}
}
