using UnityEngine;
using UnityEngine.UI;

namespace eagle {
    /// <summary>
    /// Creates a drought effect with a hot sun overlay.
    /// </summary>
    [RequireComponent(typeof(RectTransform))]
    public class DroughtEffect : MonoBehaviour {
        [Header("Sun Settings")]
        public Sprite sunSprite;
        public float sunSize = 40f;
        public float sunVerticalOffset = 40f;
        public Color sunColor = new Color(1f, 0.9f, 0.3f, 0.9f);
        public float sunPulseSpeed = 1.5f;
        public float sunPulseAmount = 0.15f;
        public float sunWaveAmountX = 8f;   // Horizontal wave distance
        public float sunWaveAmountY = 5f;   // Vertical wave distance
        public float sunWaveSpeedX = 0.4f;  // Horizontal wave speed
        public float sunWaveSpeedY =
                0.6f;  // Vertical wave speed (slightly different for organic feel)

        private GameObject _sunObject;
        private Image _sunImage;
        private float _baseSunScale;
        private Vector2 _baseSunPosition;

        void Start() { SetupEffect(); }

        private void SetupEffect() { SetupSun(); }

        private void SetupSun() {
            _sunObject = new GameObject("Sun");
            _sunObject.transform.SetParent(transform, false);

            _sunImage = _sunObject.AddComponent<Image>();
            if (sunSprite != null) { _sunImage.sprite = sunSprite; }
            _sunImage.color = sunColor;

            var rectTransform = _sunObject.GetComponent<RectTransform>();
            rectTransform.sizeDelta = new Vector2(sunSize, sunSize);
            _baseSunPosition = new Vector2(0f, sunVerticalOffset);
            rectTransform.anchoredPosition = _baseSunPosition;

            _baseSunScale = 1f;
        }

        void Update() {
            if (_sunObject != null) {
                // Pulse the sun size
                float pulse = 1f + Mathf.Sin(Time.time * sunPulseSpeed) * sunPulseAmount;
                _sunObject.transform.localScale = Vector3.one * (_baseSunScale * pulse);

                // Wave the sun position slowly
                var rectTransform = _sunObject.GetComponent<RectTransform>();
                float waveX = Mathf.Sin(Time.time * sunWaveSpeedX) * sunWaveAmountX;
                float waveY = Mathf.Sin(Time.time * sunWaveSpeedY) * sunWaveAmountY;
                rectTransform.anchoredPosition = _baseSunPosition + new Vector2(waveX, waveY);
            }
        }
    }
}
