using UnityEngine;
using System.Collections;

public class Medication : MonoBehaviour {
	
	
	public int cost;
	public int pillPurchaseAmount;
	public AudioClip drink;
	public AudioClip pillShake;
	private InteractText text;
	private GameObject playerCamera;
	private States states;

	// Use this for initialization
	void Start () {
		playerCamera = GameObject.Find ("Camera");
		states = GameObject.Find ("Player").GetComponent<States>();
		text = GetComponent<InteractText>();
	}
	
	// Update is called once per frame
	void Update () {
		if (states.missedPills < 0)
			states.missedPills = 0;
		if (states.pills == 0)
			text.interactText = "Purchase medication : $" + cost + "/" + pillPurchaseAmount + " pills";
		else
			text.interactText = "Take medication (requires 1 water)";
		if (states.missedPills > 7)
			states.missedPills = 7;
		if (states.missedPills < 2)
			playerCamera.GetComponent<Blur>().enabled = false;
		else if (states.missedPills >= 2)
			playerCamera.GetComponent<Blur>().enabled = true;
		if (states.missedPills == 2)
			playerCamera.GetComponent<Blur>().blurSize = 0;
		else if (states.missedPills == 3)
			playerCamera.GetComponent<Blur>().blurSize = 2;
		else if (states.missedPills == 4)
			playerCamera.GetComponent<Blur>().blurSize = 4;
		else if (states.missedPills == 5)
			playerCamera.GetComponent<Blur>().blurSize = 6;
		else if (states.missedPills == 6)
			playerCamera.GetComponent<Blur>().blurSize = 8;
		else if (states.missedPills >= 7)
			playerCamera.GetComponent<Blur>().blurSize = 10;
	}
	
	void Use ()
	{
		if (states.pills == 0 && states.playerMoney >= cost)
		{
			states.playerMoney -= cost;
			states.pills += pillPurchaseAmount;
			AudioSource.PlayClipAtPoint (pillShake, transform.position);
		} else if (states.pills > 0 && states.water > 0)
		{
			if (states.medicated == true)
			{
				Debug.Log ("Blackout");
				GameObject.Find ("Bed").SendMessage("Use");
				states.pills--;
				states.water--;
				AudioSource.PlayClipAtPoint (drink, transform.position);
			} else {
				Debug.Log ("pill'd");
				states.medicated = true;
				states.pills--;
				states.water--;
				AudioSource.PlayClipAtPoint (drink, transform.position);
			}
		}
	}
}
