using System;
using System.Collections;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class InteractionWheel : MonoBehaviour
{
    private Image interactionWheel;
    
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        interactionWheel = GetComponent<Image>();
    }

    private float _startFill = 0f;
    private float _endFill = 0f;
    private float _time = 0f;
    private float _elapsedTime = 0f;
    private bool _lerping = false;
    private GameObject _present;
    private AudioSource _collect;
    
    private float startTime;

    [HideInInspector]
    public void FillWheelOverTimeToAmount(float time, float amount, GameObject present, AudioSource collect)
    {
        _startFill = interactionWheel.fillAmount;
        _endFill = amount;
        _time = time;
        _lerping = true;
        _present = present;
        startTime = Time.time;
        _collect = collect;
    }
    
    // Update is called once per frame
    void Update()
    {
        if (_lerping)
        {
            _elapsedTime += Time.deltaTime;
            float progress = Mathf.Clamp01(_elapsedTime / _time);
            interactionWheel.fillAmount = Mathf.Lerp(_startFill, _endFill, progress);
            if (progress >= 1f && Math.Abs(interactionWheel.fillAmount - 1f) < 0.01f)
            {
                _collect.Play();
                interactionWheel.fillAmount = 1f;
                _present.GetComponent<SphereCollider>().enabled = false;
                StartCoroutine(Hide());
                StartCoroutine(_present.GetComponent<Present>().collected(0.5f, 40f));
                _elapsedTime = 0f;
            }
        }
    }

    public IEnumerator Hide()
    {
        _lerping = false;
        _elapsedTime = 0f;
        yield return new WaitForSeconds(0.5f);
        interactionWheel.fillAmount = 0f;
    }
    
}
