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

public class BearAwareness : MonoBehaviour
{
    private int awareness;
    public Slider awarenessSlider;

    public Image SilderColor;
    
    
    public static event Action<float> HasWokenUp;

    public float timeToWakeUp = 2f;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        awareness = 0;
        awarenessSlider.value = 0;
        Noice.OnMakeNoice += IncreaseAwareness;
        StartCoroutine(Decrease(10));
        
    }

    private void Update()
    {
        ChangeSliderColor();
    }

    void IncreaseAwareness(int noice)
    {
        awareness += noice;
        awarenessSlider.value = awareness;

        if (awareness >= awarenessSlider.maxValue)
        {
            Debug.Log("Bear is awake now!");
            HasWokenUp.Invoke(timeToWakeUp);
        }
    }

    IEnumerator Decrease(float delay)
    {
        
        while (true)
        {
            yield return new WaitForSeconds(5);

            awarenessSlider.value -= 5;
            awareness -= 5;

        }
    }

    void ChangeSliderColor()
    {
        if (awareness >= (awarenessSlider.maxValue - 10))
        {
            SilderColor.color = Color.red;
        }
        else if (awareness >= (awarenessSlider.maxValue / 2))
        {
            SilderColor.color = Color.orange;
        }
        else if (awareness <= (awarenessSlider.maxValue / 2) )
        {
            SilderColor.color = Color.darkGreen;
        }
    }
}
