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

public class BuildingManager : MonoBehaviour
{
	//Editor variables
	public int numOfStartingBuildings;
	public GameObject buildingPrefab;
	public Sprite[] BuildingResourcesSprites;	//Set these in the Editor to match the enum in ResourceManager

	//Working variables
	public static BuildingManager instance { get; private set; }
	public Building[] buildings { get; private set; }
	Image[] buildingSlots;
	int[] shuffledBuildingSlotOrder;

	void Reset()
	{
		numOfStartingBuildings = 3;
	}

	void Awake()
	{
		instance = this;
	}

	void Start()
	{
		toggleBuildingButtons(false);
	}

	public void refreshBuildingReferences()
	{
		buildings = transform.GetComponentsInChildren<Building>();
	}

	public void addStartingBuildings()
	{
		toggleBuildingSlots(false);

		//Remove any existing buildings
		refreshBuildingReferences();
		for(int i=0; i<buildings.Length; i++) { GameObject.Destroy(buildings[i].gameObject); }

		//Shuffle building slots
		shuffledBuildingSlotOrder = new int[buildingSlots.Length];
		for (int i=0; i<shuffledBuildingSlotOrder.Length; i++) { shuffledBuildingSlotOrder[i] = i; }

		for (int i=0; i<buildingSlots.Length; ++i)
		{
			int nElements = buildingSlots.Length - i;
			int n = (Random.Range(0, nElements-1)) + i;
			int tmp = shuffledBuildingSlotOrder[i];
			shuffledBuildingSlotOrder[i] = shuffledBuildingSlotOrder[n];
			shuffledBuildingSlotOrder[n] = tmp;
		}

		//Add starting buildings
		for (int i=0; i<numOfStartingBuildings; i++)
		{
			GameObject targetSlot = buildingSlots[shuffledBuildingSlotOrder[i]].gameObject;
			addNewBuilding(targetSlot);
		}
		refreshBuildingReferences();
	}

	public void toggleBuildingButtons(bool isVisible)
	{
		for(int i=0; i<buildings.Length; i++)
		{
			if (buildings[i].isOnFire) { continue; }

			Button button = buildings[i].transform.GetComponentInChildren<Button>();
			button.enabled = 
				button.GetComponent<Image>().enabled = 
					button.GetComponentInChildren<Image>().enabled = isVisible;
			button.transform.parent.GetComponent<Animator>().SetBool("buttonVisible", isVisible);
		}
	}

	void toggleBuildingSlots(bool isVisible)
	{
		if (buildingSlots == null) { buildingSlots = GameObject.Find("BuildingSlots").GetComponentsInChildren<Image>(); }

		for (int i=0; i<buildingSlots.Length; i++)
		{
			buildingSlots[i].enabled = isVisible;
		}
	}

	public void checkForChanceOfNewBuilding()
	{
		//Spawn a new building if a certain random chance is met
		if (Random.Range(0, 100) < 50 && buildings.Length < buildingSlots.Length)
		{
			GameObject targetSlot = buildingSlots[shuffledBuildingSlotOrder[buildings.Length]].gameObject;
			addNewBuilding(targetSlot);
		}
	}

	GameObject addNewBuilding(GameObject slotToAddIn)
	{
		int numOfExistingBuildings = buildings.Length;
		GameObject b = GameObject.Instantiate(buildingPrefab, slotToAddIn.transform.position, Quaternion.identity) as GameObject;
		b.transform.SetParent(this.transform);
		b.name = buildingPrefab.name + numOfExistingBuildings.ToString();

		//Assign a new resource to this building
		ResourceManager.BuildingResources resToUse = (ResourceManager.BuildingResources)Random.Range(0, (int)ResourceManager.BuildingResources.numOfBuildingResources);
		b.transform.GetComponent<Building>().assignResource(resToUse);

		refreshBuildingReferences();

		return b;
	}
		
	public void setChosenBuilding(GameObject building)
	{
		// Send resource to god manager
		ResourceManager.BuildingResources resource = building.GetComponent<Building>().currentResource;
		GodManager.instance.offeredSacrifice = resource;

		ResourceIconHelper.instance.transform.position = building.transform.position;
		Image img = ResourceIconHelper.instance.GetComponent<Image>();
		img.sprite = ResourceIconHelper.instance.GetComponent<ResourceIconHelper>().BuildingResourceIcons[(int)resource];
		img.enabled = true;
	}

	public Building GetRandomBuilding(bool isBurnt)
	{
		//Gather up our unburnt buildings and randomly pick one
		Building[] unburntBuildings = getAllBuildings(isBurnt);
		if (unburntBuildings == null || unburntBuildings.Length <= 0) { return null; }
		int unburntBuildingIndex = Random.Range(0, unburntBuildings.Length-1);
		return unburntBuildings[unburntBuildingIndex].GetComponent<Building>();
	}
		
	public Building[] getAllBuildings(bool isBurnt)
	{
		List<Building> targetBuildings = new List<Building>();
		for (int i=0; i<buildings.Length; i++)
		{
			if (buildings[i].isOnFire == isBurnt) { targetBuildings.Add(buildings[i]); }
		}
		return targetBuildings.ToArray();
	}

	public bool areAllBuildingsOnFire()
	{
		for (int i=0; i<buildings.Length; i++)
		{
			if (!buildings[i].isOnFire) { return false; }
		}
		return true;
	}
}
