using System.Collections.Generic;
using Assets.Source.Content.Tweens;
using Assets.Source.UI;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Source.Content.UI.Components
{
    public class UIAnimPopup : UIAnimation
    {
        public UITweenDataPopup Data;
        public bool ZeroAlphaFromStart = true;
        public float OpenDelay;

        public Transform ScaleTransform;
        public CanvasGroup[] CanvasGroups;
        public Image[] Images;

        private Dictionary<CanvasGroup, float> _standardCanvasGroupAlphas = new Dictionary<CanvasGroup, float>();
        private Dictionary<Image, float> _standardImageAlphas = new Dictionary<Image, float>();
        private Sequence _seq;

        protected override void VAwake()
        {
            base.VAwake();
            foreach (var img in Images)
            {
                _standardImageAlphas.Add(img, img.color.a);
                if (ZeroAlphaFromStart)
                    img.color = img.color.SetAlpha(0);
            }
            foreach (var grp in CanvasGroups)
            {
                _standardCanvasGroupAlphas.Add(grp, grp.alpha);
                if (ZeroAlphaFromStart)
                    grp.alpha = 0;
            }
        }

        public override void EndInstantly()
        {
            _seq?.Complete();
        }

        protected override void HandleTrigger(UIAnimationTrigger trigger)
        {
            _seq?.Kill();

            switch (trigger)
            {
                case UIAnimationTrigger.Open:
                    AnimateOpen();
                    break;
                case UIAnimationTrigger.Close:
                    AnimateClose();
                    break;
            }
            _seq.onComplete = () => _seq = null;
        }

        public void AnimateOpen()
        {
            _seq = DOTween.Sequence();
            foreach (var canvasGroup in CanvasGroups)
                _seq.Join(canvasGroup.DOFade(_standardCanvasGroupAlphas[canvasGroup], Data.DurationIn).SetEase(Data.FadeInCurve));
            foreach (var image in Images)
                _seq.Join(image.DOFade(_standardImageAlphas[image], Data.DurationIn).SetEase(Data.FadeInCurve));

            _seq.Join(ScaleTransform.DOBlendableScaleBy(Vector3.right, Data.DurationIn).SetEase(Data.ScaleInCurve.X));
            _seq.Join(ScaleTransform.DOBlendableScaleBy(Vector3.up, Data.DurationIn).SetEase(Data.ScaleInCurve.Y));
            _seq.PrependInterval(OpenDelay);
        }

        public void AnimateClose()
        {
            _seq = DOTween.Sequence();
            foreach (var canvasGroup in CanvasGroups)
                _seq.Join(canvasGroup.DOFade(0, Data.DurationOut).SetEase(Data.FadeOutCurve));
            foreach (var image in Images)
                _seq.Join(image.DOFade(0, Data.DurationOut).SetEase(Data.FadeOutCurve));

            _seq.Join(ScaleTransform.DOBlendableScaleBy(Vector3.right, Data.DurationOut).SetEase(Data.ScaleOutCurve.X));
            _seq.Join(ScaleTransform.DOBlendableScaleBy(Vector3.up, Data.DurationOut).SetEase(Data.ScaleOutCurve.Y));
        }
    }
}