using DG.Tweening;
using FL.Nodes.GameMain;
using UnityEngine;
using XGame.Framework.Map;
using XGame.Framework.Utils;

namespace FL.Map
{
    /// <summary>
    /// 战斗信息显示
    /// </summary>
    public class TestMainMapPopupCtrl : EntityController<TestMainMapVM>
    {
        //private HashSet<HpAddEntityView> _playingHpAdd;
        //private HashSet<Transform> _playingTransform;
        public override void OnEnable(object intent)
        {
            EventSingle.Instance.AddListener(EventDefine.GameMainMapHpAdd, OnHpAdd);
            EventSingle.Instance.AddListener(EventDefine.GameMainMapSkillEffect, OnSkillEffect);
        }

        public override void OnDisable()
        {
            EventSingle.Instance.RemoveListener(EventDefine.GameMainMapHpAdd, OnHpAdd);
            EventSingle.Instance.RemoveListener(EventDefine.GameMainMapSkillEffect, OnSkillEffect);
        }
        private void OnHpAdd(int eventId, object args)
        {
            PlayHpAdd((HpAddDto)args);
        }
        private void PlayHpAdd(HpAddDto dto)
        {
            var viewAsync = Context.Asset.LoadEntity<HpAddEntityView>("hpadd", VM.HpRoot);
            viewAsync.On(_ =>
            {
                var view = viewAsync.Result;
                view?.ShowValue(dto.hpValue, dto.position, () =>
                {
                    Context.Asset.Recycle(view);
                });
            });
        }
        private void OnSkillEffect(int eventId, object args)
        {
            var dto = (SkillEffectDto)args;
            if (!string.IsNullOrEmpty(dto.skillEffect))
            {
                var skillAsync = Context.Asset.LoadAsync<GameObject>(dto.skillEffect);
                skillAsync.On(_ =>
                {
                    var skillEffect = skillAsync.GetResult();
                    if (skillEffect == null)
                    {
                        return;
                    }
                    var skillTr = skillEffect.transform;
                    skillTr.SetParent(VM.HpRoot, false);
                    skillTr.position = dto.from;
                    skillEffect.gameObject.SetActive(true);

                    var timer = Context.Time.AddDelayTimer(dto.skillTime, () =>
                    {
                        PlayBulletEffect(dto);
                        Context.Asset.Recycle(skillEffect);
                    });
                });
            }
            else
            {
                PlayBulletEffect(dto);
            }
        }
        private void PlayBulletEffect(SkillEffectDto dto)
        {
            if (!string.IsNullOrEmpty(dto.bulletEffect))
            {
                var bulletAsync = Context.Asset.LoadAsync<GameObject>(dto.bulletEffect);
                bulletAsync.On(_ =>
                {
                    var bulletEffect = bulletAsync.GetResult();
                    if (bulletEffect == null)
                    {
                        return;
                    }
                    var skillTr = bulletEffect.transform;
                    skillTr.SetParent(VM.HpRoot, false);
                    skillTr.position = dto.from;
                    bulletEffect.gameObject.SetActive(true);

                    var timeScale = Random.Range(0.2f, 0.8f);
                    var linePoint = Vector3.Lerp(dto.from, dto.to, timeScale);
                    var dir = Vector3.Cross(dto.to - dto.from, Vector3.forward).normalized; //运动方向的法线
                    var disOffset = Random.Range(-3f, 3f);
                    var center = linePoint + dir * disOffset;
                    var totalTime = 0.5f;
                    var timeTween = DOTween.To(() => 0, (t) =>
                    {
                        var tNormal = t / totalTime;
                        skillTr.position = dto.from.BezierPoint(center, dto.to, tNormal);
                    }, totalTime, totalTime);
                    timeTween.SetEase(Ease.OutQuad);
                    timeTween.onComplete = () =>
                    {
                        //播放受击
                        PlayHitEffect(dto);
                        PlayHpAdd(dto.hpAdd);
                        Context.Asset.Recycle(bulletEffect);
                    };
                });
            }
            else
            {
                //播放受击
                PlayHitEffect(dto);
                PlayHpAdd(dto.hpAdd);
            }
        }
        private void PlayHitEffect(SkillEffectDto dto)
        {
            var hitAsync = Context.Asset.LoadAsync<GameObject>(dto.hitEffect);
            hitAsync.On(_ =>
            {
                var hitEffect = hitAsync.GetResult();
                if (hitEffect == null)
                {
                    return;
                }
                var hitTr = hitEffect.transform;
                hitTr.SetParent(VM.HpRoot, false);
                hitTr.position = dto.to;
                hitTr.gameObject.SetActive(true);
                Context.Time.AddDelayTimer(2000, () =>
                { // 2秒后删除
                    Context.Asset.Recycle(hitEffect);
                });
            });
        }
    }
}
