using UnityEngine;
using UnityEngine.UI;

public class HudGui : MonoBehaviour 
{
    public GameObject throwPowerPanel;
    public Slider throwPowerSlider;
    public Image throwPowerSliderFill;
    public Gradient throwPowerGradient;
    public Slider dignitySlider;
    public Image dignitySliderFill;
    public Gradient dignityGradient;
    public Text scoreText;
    public Text messageText;
    public GameObject messagePanel;
    public Image splatterImage;
    public float lastHealth;
    public float splatterTimer;

    private PlayerController playerController;

    void Awake()
    {
        playerController = FindObjectOfType<PlayerController>();
    }
	
	void Update() 
    {
        if (!playerController.gameOver)
        {

            if (playerController.fireChargeTime > 0f)
            {
                throwPowerSlider.value = playerController.fireChargeTime / playerController.maxChargeTime;
                throwPowerSliderFill.color = throwPowerGradient.Evaluate(throwPowerSlider.value);
                throwPowerPanel.SetActive(true);
            }
            else
            {
                throwPowerPanel.SetActive(false);
            }

            dignitySlider.value = playerController.dignity;
            dignitySliderFill.color = dignityGradient.Evaluate(dignitySlider.value / dignitySlider.maxValue);
            scoreText.text = playerController.score.ToString("#,##0");

            if(!string.IsNullOrEmpty(playerController.message))
            {
                messageText.text = playerController.message;
                messagePanel.SetActive(true);
            }
            else
            {
                messagePanel.SetActive(false);
            }

            splatterTimer -= Time.deltaTime;
            if(playerController.dignity < lastHealth)
            {
                splatterTimer = 1f;
            }
            lastHealth = playerController.dignity;

            splatterImage.color = new Color(1f, 0f, 0f, splatterTimer);
        }
        else
        {
            gameObject.SetActive(false);
        }
	}
}
