using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using DG.Tweening;

public class UIController : MonoBehaviour {

    public static UIController Instance;

    [SerializeField] private Color _correct;
    [SerializeField] private Color _fail;

    [SerializeField] private Image _satisfaction;
    [SerializeField] private TextMeshProUGUI _scoreText;
    private Color _baseCol;

    private int _score = 0;

    private float _satisfactionWidth = 0;
    void Awake()
    {
        Instance = this;
        _satisfactionWidth = _satisfaction.rectTransform.sizeDelta.x;
        _baseCol = _satisfaction.color;

        _scoreText.text = _score.ToString();
    }

	public void SetSatisfaction(float progress)
    {
        _satisfaction.rectTransform.sizeDelta = new Vector2(_satisfactionWidth * progress, _satisfaction.rectTransform.sizeDelta.y);

        float basePos = -_satisfactionWidth * 0.5f;

        _satisfaction.rectTransform.anchoredPosition = new Vector2(basePos + _satisfaction.rectTransform.sizeDelta.x * 0.5f, _satisfaction.rectTransform.anchoredPosition.y);

      //  -0.75
    }


    public void Correct()
    {
        _score++;
        _scoreText.text = _score.ToString();


        StopAllCoroutines();
        StartCoroutine(ReactionRoutine(_correct));
    }

    public void Fail()
    {
        StopAllCoroutines();
        StartCoroutine(ReactionRoutine(_fail));
    }

    IEnumerator ReactionRoutine(Color value)
    {
        float progress = 0;
        while(progress < 1)
        {
            progress += Time.deltaTime;
            progress = Mathf.Clamp01(progress);

            _satisfaction.color = Color.Lerp(_baseCol, value, Mathf.Sin(progress * 5f * Mathf.PI));

            yield return null;
        }
    }

   
}
