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

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

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

		panels = new List<GameObject>();

		panels.Add(GameObject.Find("Canvas/GUI/Fire/SAM"));
		panels.Add(GameObject.Find("Canvas/GUI/Fire/Strike"));
		panels.Add(GameObject.Find("Canvas/GUI/Fire/CIWS"));

		GameController.GUI.SetActive(false);
	}

	public static void constructFire(DeployedUnit selectedUnit) {
		GameController.deleteAllChildren(panels[0].transform);
		GameObject SAM = allMissiles.transform.Find("SAM").gameObject;
		int count = 0;
		foreach(Class antiMissile in selectedUnit.getClass().getMissileAutoDef()) {
			GameObject clone = GameObject.Instantiate(SAM, panels[0].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
		GameController.deleteAllChildren(panels[1].transform);
		GameObject strike = allMissiles.transform.Find("Strike").gameObject;
		count = 0;
		foreach(Class missile in selectedUnit.getClass().getMissilesStrike()) {
			GameObject clone = GameObject.Instantiate(strike, panels[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
		GameController.deleteAllChildren(panels[2].transform);
		GameObject ciws = allMissiles.transform.Find("CIWS").gameObject;
		count = 0;
		foreach(Tuple<Module, ModuleData> tuple in selectedUnit.getModules()) {
			if (tuple._1 is CIWSModule) {
				GameObject clone = GameObject.Instantiate(ciws, panels[2].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++;
			}
		}
	}

	public static void updateFire(DeployedUnit selectedUnit) {
		//SAM
		if (panels[0].activeSelf) {
			foreach(Transform child in panels[0].transform) {
				SAMBehaviour behaviour = child.gameObject.GetComponent<SAMBehaviour>();
				child.Find("Firing").gameObject.SetActive(selectedUnit.isFiring(behaviour.missile) && (Time.frameCount % 30) < 15);
			}
		}
		//Strike
		else if (panels[1].activeSelf) {
			foreach(Transform child in panels[1].transform) {
				MissileStrikeBehaviour behaviour = child.gameObject.GetComponent<MissileStrikeBehaviour>();

				int left = selectedUnit.getNumberStored(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);

				if (selectedUnit.getTarget() == null) {
					canFireMessage = "NO TARGET";
				}
				else if (!behaviour.missile.canHit(selectedUnit.getTarget().getClass().getClassType())) {
					canFireMessage = "CANNOT HIT TARGET";
				}
				else if (selectedUnit.getTarget().getClass().getCountry().getRelation(selectedUnit.getClass().getCountry()) == Relation.Player) {
					canFireMessage = "NO FRIENDLY FIRE";
				}
				else if (behaviour.currentNumber == 0) {
					canFireMessage = "NO MORE AMMO";
				}

				//TODO : check if missile can be launched

				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 (panels[2].activeSelf) {
			foreach(Transform child in panels[2].transform) {
				CIWSBehaviour behaviour = child.gameObject.GetComponent<CIWSBehaviour>();

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