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

//Manages the 'grid' of modules
using UnityEngine.UI;


public abstract class GridManagement {

	public static Transform allModulesUI;
	public static List<GameObject> modulesPanels; //where to clone the modules' UI, depending on their tab

	public static void init() {
		modulesPanels = new List<GameObject>();
		allModulesUI = GameObject.Find("Templates/ModulesUI").transform;

		modulesPanels.Add(GameObject.Find("Canvas/GUI/ModulesStates/Weapons"));
		modulesPanels.Add(GameObject.Find("Canvas/GUI/ModulesStates/Sensors"));
		modulesPanels.Add(GameObject.Find("Canvas/GUI/ModulesStates/Ship&Prop"));
		modulesPanels.Add(GameObject.Find("Canvas/GUI/ModulesStates/Command"));
	}

	public static void constructGrid(DeployedUnit selectedUnit) {
		Class unitClass = selectedUnit.getClass();

		float width = modulesPanels[0].GetComponent<RectTransform>().rect.width;
		float height = modulesPanels[0].GetComponent<RectTransform>().rect.height;

		foreach(GameObject tab in modulesPanels) {
			GameController.deleteAllChildren(tab.transform);
		}
		foreach (ModuleUI moduleUI in unitClass.getModulesUI()) {
			GameObject clone = GameObject.Instantiate(allModulesUI.FindChild(moduleUI.getName()).gameObject, modulesPanels[moduleUI.getTab()].transform);

			RectTransform transform = clone.GetComponent<RectTransform>();

			transform.localPosition = new Vector3(moduleUI.getX() * width / Constants.MODULE_GRID_WIDTH + Constants.MODULE_BORDER, moduleUI.getY() * height / Constants.MODULE_GRID_HEIGHT - Constants.MODULE_BORDER, 0);
			transform.sizeDelta = new Vector2(width * (-Constants.MODULE_GRID_WIDTH + moduleUI.getWidth()) / Constants.MODULE_GRID_WIDTH - 2 * Constants.MODULE_BORDER,
				height * (-Constants.MODULE_GRID_HEIGHT + moduleUI.getHeight()) / Constants.MODULE_GRID_HEIGHT - 2 * Constants.MODULE_BORDER);

			clone.name = moduleUI.getName();
			GridElement gridElement = clone.AddComponent<GridElement>();
			gridElement.moduleDatas = getModuleDatas(selectedUnit, moduleUI);
		}
	}

	private static List<ModuleData> getModuleDatas(DeployedUnit selectedUnit, ModuleUI moduleUI) {
		List<ModuleData> datas = new List<ModuleData>();

		foreach(string moduleName in moduleUI.getMonitoredModules()) {
			datas.AddRange(selectedUnit.getModuleDatas(moduleName));
		}

		return datas;
	}

	public static void updateModules(DeployedUnit selectedUnit) {
		foreach(GameObject tab in modulesPanels) {
			if (tab.activeSelf) { //no need to update invisible tabs
				foreach (Transform child in tab.transform) {
					string name = child.gameObject.name;
					GridElement element = child.gameObject.GetComponent<GridElement>();

					updateHealth(child.gameObject, element);

					if (name == "ArleighMk-41") {
						updateModuleArleighMk_41(child.gameObject, element);
					}
				}
			}
		}
	}

	private static void updateHealth(GameObject gameObject, GridElement element) {
		float health = 0.0f;
		float total = 0.0f;

		foreach(ModuleData data in element.moduleDatas) {
			health += data.getHealth();
			total += data.getTemplate().getHealth();
		}

		gameObject.GetComponent<Image>().color = Util.getColorFromValue(health / total);
	}

	private static void updateModuleArleighMk_41(GameObject gameObject, GridElement element) {
		LauncherModuleData fore = (LauncherModuleData)element.moduleDatas[0];
		LauncherModuleData aft = (LauncherModuleData)element.moduleDatas[0];

		Transform foreList = gameObject.transform.Find("ForeList");
		Transform aftList = gameObject.transform.Find("AftList");
		GameObject weaponText = gameObject.transform.Find("ForeList/Weapon").gameObject;

		GameController.deleteAllChildren(foreList, true);
		foreach(Tuple<Class, int> tuple in fore.getReadyToLaunch()) {
			GameObject clone = GameObject.Instantiate(weaponText, foreList);

			clone.SetActive(true);
			clone.transform.Find("Name").gameObject.GetComponent<Text>().text = tuple._1.getName();
			clone.transform.Find("Num").gameObject.GetComponent<Text>().text = tuple._2 + "";
		}

		GameController.deleteAllChildren(aftList, true);
		foreach(Tuple<Class, int> tuple in aft.getReadyToLaunch()) {
			GameObject clone = GameObject.Instantiate(weaponText, aftList);

			clone.SetActive(true);
			clone.transform.Find("Name").gameObject.GetComponent<Text>().text = tuple._1.getName();
			clone.transform.Find("Num").gameObject.GetComponent<Text>().text = tuple._2 + "";
		}
	}
}
