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

public class SatisfactionBarUI : MonoBehaviour {

    public Image satisfactionBarImage;
	public Animator gameBGimage;

    private SatisfactionRateHandler thisHandler;

    private bool audioHasPlayed = false;

    void Awake()
    {
        thisHandler = GetComponent<SatisfactionRateHandler>();
        satisfactionBarImage = GetComponent<Image>();
    }

    void Update()
    {
        satisfactionBarImage.fillAmount = thisHandler.satisfactionRate;


		if(thisHandler.satisfactionRate >= 0.7f)
		{
			satisfactionBarImage.color = Color.green;
			gameBGimage.Play("BG_Normal");
		}
		else if(thisHandler.satisfactionRate >= 0.3f)
		{
			satisfactionBarImage.color = Color.yellow;
			gameBGimage.Play("BG_Normal");
		}
		else if(thisHandler.satisfactionRate >= 0.1f)
		{
            if (!audioHasPlayed) 
            {
                EventManager.TriggerEvent(Constants.Warning);
                audioHasPlayed = true;
            }
			satisfactionBarImage.color = Color.red;
			gameBGimage.Play("BG_Danger");
		}
		else if(thisHandler.satisfactionRate >= 0f)
		{
            audioHasPlayed = false;
			gameBGimage.Play("BG_Shutdown");
		}
    }

}
