using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class BarController : MonoBehaviour
{
    public static BarController Instance;

    public Slider HealthSlider;
    public Slider AlcoholSlider;

    public Camera laughingCamera;

    public Slider FoodSlider;

    public float CurrentMoral = 99f;
    public float CurrentAlchol = 99f;
    public float CurrentFood = 99f;

    private EventControls _eventControls;

    private void Awake()
    {

        _eventControls = new EventControls(laughingCamera, gameObject);

        CurrentMoral = 60f;
        CurrentAlchol = 60f;
        CurrentFood = 60f;


        Instance = this;
    }

    private void Start()
    {
        StartCoroutine(DecreaseSlidersOverTime());
    }

    private void Update()
    {
        HealthSlider.value = CurrentMoral / 100;
        AlcoholSlider.value = CurrentAlchol / 100;
        FoodSlider.value = CurrentFood / 100;
    }

    private IEnumerator DecreaseSlidersOverTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.2f);

            CurrentMoral -= .25f;
            CurrentAlchol -= .25f;
            CurrentFood -= .25f;


            HealthSlider.value = CurrentMoral / 100f;
            AlcoholSlider.value = CurrentAlchol / 100f;
            FoodSlider.value = CurrentFood / 100f;

            if (CurrentMoral <= 0f || CurrentAlchol <= 0f || CurrentFood <= 0f)
            {
                Debug.Log("Artık bülbül ötmüyor");
                break;
            }
        }
    }

    public async void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Food"))
        {
            CurrentFood += 20f;
            other.gameObject.transform.parent = transform;
            other.gameObject.transform.localPosition = new Vector3(0, -10, 0);
            await Task.Delay(3000);
            await _eventControls.Laugh();
            Destroy(other.gameObject);
        }
        else if (other.gameObject.CompareTag("Alchol"))
        {
            CurrentAlchol += 20f;
            other.gameObject.GetComponentInParent<hobocontroller>()?.StopPath();
            other.gameObject.transform.parent = transform;
            other.gameObject.transform.localPosition = new Vector3(0, -11.5f, 0);
            other.gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);
            await Task.Delay(3000);
            await _eventControls.Laugh();
            Destroy(other.gameObject);
        }
        else if (other.gameObject.CompareTag("Stone"))
        {
            DeadAction(6);
        }
    }

    private void OnCollisionEnter(Collision other)
    {
        gameObject.GetComponent<MartiController>().moveDirection = Vector3.zero;
    }

    private void DeadAction(int mode)
    {
        switch (mode)
        {
            case 0:
                PlayerPrefs.SetString("Deaths", "Alkolün bitti, üzütüden öldün");
                Debug.Log("Alkolün bitti, üzütüden öldün");
                break;
            case 1:
                PlayerPrefs.SetString("Deaths", "Moralin bitti, üzütüden öldün");
                Debug.Log("Moralin bitti, üzütüden öldün");
                break;
            case 2:
                PlayerPrefs.SetString("Deaths", "Yemeğin bitti, açlıktan öldün");
                Debug.Log("Yemeğin bitti, açlıktan öldün");
                break;
            case 3:
                PlayerPrefs.SetString("Deaths", "Çok içtin, öldün");
                Debug.Log("Çok içtin, öldün");
                break;
            case 4:
                PlayerPrefs.SetString("Deaths", "Çok güldün, patladın öldün");
                Debug.Log("Çok güldün, patladın öldün");
                break;
            case 5:
                PlayerPrefs.SetString("Deaths", "Çok yedin, öldün");
                Debug.Log("Çok yedin, öldün");
                break;
            case 6:
                PlayerPrefs.SetString("Deaths", "Vuruldun, öldün");
                Debug.Log("Vuruldun, öldün");
                break;
            default:
                Debug.Log("Öldün");
                break;
        }

        SceneManager.LoadScene("DeadScene");
    }


}
