using DG.Tweening;
using System;
using TMPro;
using UnityEngine;
using XGame.Framework.Map;
using XGame.Framework.ObjectCollection;
using XGame.Framework.Utils;

namespace FL.Map
{
    public class HpAddEntityView : EntityView<HpAddEntityVM>
    {
        protected override void OnEnable(object intent)
        {
        }
        protected override void OnDisable()
        {
        }
        protected override void OnDispose()
        {
            VM.Tr.DOKill();
            VM.Root.DOKill();
        }

        public void ShowValue(int value, Vector3 position, Action callback)
        {
            VM.HpText.text = value.ToString();
            VM.HpText.color = value > 0 ? Color.green : Color.red;
            Position = position;
            var endPosition = position + new Vector3(2, 4, 0);// new Vector3(UnityEngine.Random.Range(0, 2), UnityEngine.Random.Range(4, 5), 0);
            var timeScale = UnityEngine.Random.Range(0.2f, 0.8f);
            var linePoint = Vector3.Lerp(position, endPosition, timeScale);
            var dir = Vector3.Cross(endPosition - position, Vector3.forward).normalized; //运动方向的法线
            var disOffset = UnityEngine.Random.Range(-4f, -1f);
            var center = linePoint + dir * disOffset;
            var totalTime = 3f;
            var timeTween = DOTween.To(() => 0, (t) =>
            {
                var tNormal = t / totalTime;
                //var pos = Vector3.Lerp(position, endPosition, tNormal);
                //Position = pos + dir * disOffset * Mathf.Sin(180 * tNormal * Mathf.Deg2Rad);
                Position = position.BezierPoint(center, endPosition, tNormal);
            }, totalTime, totalTime);
            timeTween.SetEase(Ease.OutQuad);
            timeTween.onComplete = () =>
            {
                callback?.Invoke();
            };
            //var jumpTween = VM.Tr.DOJump(endPosition, 2, 1, 3);
            //jumpTween.onComplete = () =>
            //{
            //    callback?.Invoke();
            //};
        }
    }
    public class HpAddEntityVM : EntityViewModel
    {
        public TextMeshPro HpText { get; private set; }
        public Transform Root { get; private set; }
        protected override void OnInit(IObjectCollector collector)
        {
            HpText = collector.GetComponent<TextMeshPro>("HpText");
            Root = collector.GetGameObject("Root").transform;

        }
    }
}
