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

public class Building : MonoBehaviour
{
	public ResourceManager.BuildingResources currentResource;
	public bool isOnFire;

	void Start()
	{
		isOnFire = false;
	}

	public void BuildingClicked()
	{
		if (isOnFire) { return; }
		BuildingManager.instance.setChosenBuilding(this.gameObject);
		GameManager.instance.changeState(GameManager.GameState.AnimatingToSacrifice);
        AudioController.instance.playSFX(AudioController.audioClips.runLoop);
    }

	void Update()
	{
		Image fireIcon = transform.FindChild("FireIcon").GetComponent<Image>();
		fireIcon.enabled = isOnFire;
	}

	public void assignResource(ResourceManager.BuildingResources resourceToUse)
	{
		currentResource = resourceToUse;
		Image iconImg = transform.GetComponentInChildren<Button>().transform.FindChild("Image").GetComponent<Image>();
		iconImg.sprite = ResourceIconHelper.instance.BuildingResourceIcons[(int)resourceToUse];

		//Use appropriate building image
		int imgIndex;
		if (resourceToUse == ResourceManager.BuildingResources.Llama) { imgIndex = 5; }
		else if (resourceToUse == ResourceManager.BuildingResources.Wine || resourceToUse == ResourceManager.BuildingResources.Beer) { imgIndex = 6; }
		else { imgIndex = Random.Range(0,4); }
		Image img = transform.GetComponent<Image>();
		img.sprite = BuildingManager.instance.BuildingResourcesSprites[imgIndex];
		img.SetNativeSize();
		RectTransform rt = GetComponent<RectTransform>();
		float buildingDownscale = 0.2f;	//TODO: This needs to change depending on resolution. It's literally the worst. GAME JAM YEAH!
		rt.sizeDelta *= buildingDownscale;
	}
}
