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

public class StarBackground : MonoBehaviour //CLANKER ALERT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
    [Header("Star Frames")]
    public Sprite[] frames;

    [Header("Star Settings")]
    public int starCount = 35;
    public float minSize = 40f;
    public float maxSize = 64f;

    [Header("Animation")]
    public float minFPS = 8f;
    public float maxFPS = 16f;

    [Header("Spacing")]
    public float minDistance = 70f;

    private RectTransform canvasRect;
    private List<Vector2> occupiedPositions = new List<Vector2>();

    void Start()
    {
        Canvas rootCanvas = GetComponentInParent<Canvas>();
        canvasRect = rootCanvas != null
            ? rootCanvas.GetComponent<RectTransform>()
            : GetComponent<RectTransform>();

        for (int i = 0; i < starCount; i++)
        {
            Vector2 pos = GetRandomPosition();
            occupiedPositions.Add(pos);
            CreateStar(pos);
        }
    }

    void CreateStar(Vector2 position)
    {
        GameObject star = new GameObject("Star");
        star.transform.SetParent(this.transform, false);

        Image img = star.AddComponent<Image>();
        img.preserveAspect = true;
        if (frames != null && frames.Length > 0)
            img.sprite = frames[0];

        float size = Random.Range(minSize, maxSize);
        RectTransform rt = star.GetComponent<RectTransform>();
        rt.anchorMin        = new Vector2(0.5f, 0.5f);
        rt.anchorMax        = new Vector2(0.5f, 0.5f);
        rt.pivot            = new Vector2(0.5f, 0.5f);
        rt.sizeDelta        = new Vector2(size, size);
        rt.anchoredPosition = position;

        StartCoroutine(AnimateStar(img));
    }

    IEnumerator AnimateStar(Image img)
    {
        if (frames == null || frames.Length == 0) yield break;

        int frameIndex = Random.Range(0, frames.Length);
        float fps = Random.Range(minFPS, maxFPS);

        while (true)
        {
            frameIndex = (frameIndex + 1) % frames.Length;
            img.sprite = frames[frameIndex];
            yield return new WaitForSeconds(1f / fps);
        }
    }

    Vector2 GetRandomPosition()
    {
        float width  = canvasRect.rect.width;
        float height = canvasRect.rect.height;

        for (int attempt = 0; attempt < 100; attempt++)
        {
            float x           = Random.Range(-width  / 2f, width  / 2f);
            float y           = Random.Range(-height / 2f, height / 2f);
            Vector2 candidate = new Vector2(x, y);

            bool tooClose = false;
            foreach (Vector2 pos in occupiedPositions)
            {
                if (Vector2.Distance(candidate, pos) < minDistance)
                {
                    tooClose = true;
                    break;
                }
            }
            if (!tooClose) return candidate;
        }

        return new Vector2(
            Random.Range(-width  / 2f, width  / 2f),
            Random.Range(-height / 2f, height / 2f)
        );
    }
}
