using UnityEngine;

public class BuildingPlaceholder : MonoBehaviour
{

	public string BuildingID;
	public int MaxGrowStep;
	public int GrowStep;

	public void Improve()
	{
		if (GrowStep >= MaxGrowStep)
		{
			AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/impossible_action"), transform.position, 4);
			return;
		}
		
		AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/improve_building"), transform.position, 4);

		ScoreManager.Instance.Money -= Random.Range(3000, 8000) * GrowStep;
		ScoreManager.Instance.Points += 200 * GrowStep;
		
		GrowStep += 1;
		
		RedrawBuilding();
	}

	public void Destroy()
	{
		AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/destroy_building"), transform.position, 4);
		
		ScoreManager.Instance.Money -= Random.Range(400, 5000) * GrowStep;
		ScoreManager.Instance.Money += Random.Range(1000, 50000) * GrowStep;
		ScoreManager.Instance.Points -= ScoreManager.Instance.Points / 8 * (GrowStep / MaxGrowStep);
		
		Destroy(gameObject);
	}

	public void GenerateBuilding(GameplayManager.Building building)
	{
		BuildingID = building.ID;
		MaxGrowStep = building.MaxSize;
		GrowStep = Random.Range(1, (MaxGrowStep / 2) + 1);
		RedrawBuilding();
	}

	public void RedrawBuilding()
	{
		GetComponent<SpriteRenderer>().sprite =
			Resources.Load<Sprite>("Sprites/Buildings/" + BuildingID + "/" + BuildingID + GrowStep);
	}
	
	private void OnMouseDown()
	{
		if (enabled)
		{
			Destroy();
		}
	}
	
	private void  OnMouseOver()
	{
		if (enabled && Input.GetMouseButtonDown(1))
		{
			Improve();
		}
	}

}
