using System;
using System.Collections.Generic;
using FairyGUI;

namespace NBC
{
    public class UIComponentTween : Dictionary<string, string>
    {
    }

    /// <summary>
    /// 组件动效配置包。与 UIComponentLanguagePack 结构类似，根据 resourceURL 映射到每个组件的子节点动效名称。
    /// </summary>
    public abstract class UIComponentTweenPack : Dictionary<string, UIComponentTween>
    {
        public bool Has(string url)
        {
            return ContainsKey(url);
        }

        public void TryPlayComponentTween(GComponent component)
        {
            if (component == null || component.packageItem == null) return;
            if (!Has(component.resourceURL)) return;
            PlayComponentTween(component);
        }

        /// <summary>
        /// 面板调用，不判断是否存在，根节点如果配置了 __root__ 会播放。
        /// </summary>
        public void TryPlayPanelTween(GComponent component)
        {
            if (component == null) return;
            PlayComponentTween(component);
        }

        public string GetRootTween(string url)
        {
            if (TryGetValue(url, out var cfg) && cfg != null && cfg.TryGetValue("__root__", out var tween))
                return tween;
            return null;
        }

        public void PlayComponentTween(GComponent component)
        {
            if (component == null) return;
            bool comHasTweenConfig = false;
            UIComponentTween tweenCfg = null;
            if (component.packageItem != null && TryGetValue(component.resourceURL, out tweenCfg))
            {
                comHasTweenConfig = true;
            }

            var count = component.numChildren;
            for (var i = 0; i < count; i++)
            {
                var child = component.GetChildAt(i);
                if (child.packageItem != null && child is GComponent childCom)
                {
                    PlayComponentTween(childCom); // 先递归子组件
                }

                if (comHasTweenConfig)
                {
                    var id = child.id;
                    if (tweenCfg.TryGetValue(id, out var tweenName))
                    {
                        PlayTween(child, tweenName);
                    }
                }
            }

            // 顶层UIPanel根节点动效改由 UIManager 设置 ShowAnim，不在这里直接播放
            if (component.parent == GRoot.inst) return;
            // if (comHasTweenConfig && tweenCfg.TryGetValue("__root__", out var rootTween))
            // {
            //     PlayTween(component, rootTween);
            // }
        }

        private static readonly Dictionary<string, Action<GObject>> TweenMap =
            new()
            {
                ["Fade"] = o => o.TweenFadeShow(),
                ["Scale"] = o => o.TweenScaleShow(),
                ["Pop"] = o => o.TweenPopShow(),
                ["SlideInL"] = o => o.TweenSlideInLeft(),
                ["SlideInR"] = o => o.TweenSlideInRight(),
                ["SlideInT"] = o => o.TweenSlideInTop(),
                ["SlideInB"] = o => o.TweenSlideInBottom(),
                ["Bounce"] = o => o.TweenBounceShow(),
                ["Rotate"] = o => o.TweenRotateShow(),
                ["Shake"] = o => o.TweenShake()
            };

        private void PlayTween(GObject target, string tweenName)
        {
            if (target == null || string.IsNullOrEmpty(tweenName)) return;
            if (TweenMap.TryGetValue(tweenName, out var act))
                act(target);
        }
    }
}