using UnityEngine;
using System.Collections;

public class Plant : MonoBehaviour {

	public GameObject waterParticle;
	
	public GameObject[] stages;
	
	public bool wet = false;
	public int plantStage = 1;
	public int saplingPrice = 3;
	public int sellingPrice;
	public bool auto = false;
	public AudioClip water;

	private States states;
	private Bed bed;

	// Use this for initialization
	void Start () {
		bed = GameObject.Find ("Bed").GetComponent<Bed>();
		states = GameObject.Find("Player").GetComponent<States>();
	}
	
	void Use ()
	{
		if (states.water > 0)
		{
			if (plantStage == 0)
			{
				if (states.playerMoney >= saplingPrice)
				{
					states.playerMoney -= saplingPrice;
					transform.tag = "Untagged";
					StartCoroutine(Water ());
					transform.tag = "Untagged";
					wet = true;
					states.plantWaterCount++;
				}
			} else {
				transform.tag = "Untagged";
				StartCoroutine(Water ());
				wet = true;
				states.plantWaterCount++;
			}
		}
	}
	
	IEnumerator Water ()
	{
		states.water--;
		waterParticle.SetActive(true);
		AudioSource.PlayClipAtPoint(water, transform.position);
		yield return new WaitForSeconds(2f);
		waterParticle.SetActive(false);
		transform.tag = "Interactable";
	}
	
	// Update is called once per frame
	void Update ()
	{
		Debug.Log (stages.Length);
		if (auto)
		{
			if (plantStage > 0)
				transform.tag = "Untagged";
			else
				transform.tag = "Interactable";
			wet = true;
		}
		if (plantStage == 0)
			GetComponent<InteractText>().interactText = "Plant sapling : $" + saplingPrice;
		else
			GetComponent<InteractText>().interactText = "Water plant (requries 1 water)";
		Debug.Log(bed.sleeping);
		if (plantStage >= stages.Length)
			plantStage = stages.Length - 1;
		for (int i = 0;i < stages.Length;i++)
		{
			if (i == plantStage)
			{
				stages[i].SetActive(true);
			} else {
				stages[i].SetActive(false);
			}
		}
	}
	
	void Sleep ()
	{
		transform.tag = "Interactable";
		Debug.Log (states.selling + " " + plantStage + " " + (stages.Length - 1));
		if (states.selling && plantStage == stages.Length - 1)
		{
			Debug.Log ("HELLO");
			plantStage = 0;
			states.tableMoney += sellingPrice;
		}
		if (wet && plantStage != stages.Length)
		{
			plantStage++;
			wet = false;
		}
	}
}
