using UnityEngine;
using UnityEngine.UI;

public class UILoadingManager : MonoBehaviour
{
    public static UILoadingManager Instance { get; private set; }

    public GameObject Overlay = null;


    public Image[] Dots = null;

    private const float COLOR_ANIMATION_SPEED = 12.0f;

    private static readonly Color BrightDotColor = new Color(0.27f, 0.47f, 0.64f, 1.0f);

    private static readonly Color DarkDotColor = new Color(0.27f, 0.47f, 0.64f, 0.18f);

    void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(this);
            return;
        }

        Instance = this;
        Overlay.transform.SetAsLastSibling();
        Overlay.SetActive(false);
    }

    void Update()
    {
        if (!Overlay.activeSelf || Dots == null || Dots.Length == 0)
        {
            return;
        }

        float offset = Time.unscaledTime * COLOR_ANIMATION_SPEED;
        int dotCount = Dots.Length;

        for (int i = 0; i < dotCount; i++)
        {
            float t = Mathf.Repeat((i + offset) / dotCount, 1.0f);
            Dots[i].color = Color.Lerp(BrightDotColor, DarkDotColor, t);
        }
    }

    public void Open()
    {
        Overlay.transform.SetAsLastSibling();
        Overlay.SetActive(true);
    }

    public void Close()
    {
        Overlay.SetActive(false);
    }
}
