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

namespace Assets.Source.Content.UI.Components
{
    public class ImageFader : MonoBehaviour
    {
        public List<Image> Images;
        public float FadeInDuration = 0.5f;
        public float FadeOutDuration = 0.5f;
        public AnimationCurve FadeInCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);
        public AnimationCurve FadeOutCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);

        private Dictionary<Image, Color> _defaultColors = new Dictionary<Image, Color>();
        private Tween _twFade;

        void Awake()
        {
            foreach (var image in Images)
                _defaultColors.Add(image, image.color);
        }

        public void Fade(bool fadeIn)
        {
            _twFade?.Kill();
            foreach (var image in Images)
            {
                if (fadeIn)
                   _twFade = image.DOFade(_defaultColors[image].a, FadeInDuration).SetEase(FadeInCurve);
                else
                   _twFade = image.DOFade(0, FadeOutDuration).SetEase(FadeOutCurve);
            }
        }

        public void FadeImmediate(bool fadeIn)
        {
            _twFade?.Kill();
            foreach (var image in Images)
            {
                if (fadeIn)
                    image.color = image.color.SetAlpha(_defaultColors[image].a);
                else
                    image.color = image.color.SetAlpha(0);
            }
        }
    }
}
