using Core.Triiger;
using Core.Utility;
using Fort23.Core;
using Fort23.UTool;
using GameLogic.Combat.Hero;
using GameLogic.Combat.Hero.State;
using UnityEngine;
using Utility.CTween;
namespace GameLogic.Combat.CombatTool
{
    public class MagicWeaponCollisionInfo : CObject, ITriggerEntity
    {
        public CombatMagicWeaponEntity a;
        public CombatMagicWeaponEntity b;
        public BesselPath _besselPathA;
        public BesselPath _besselPathB;
        public int id;
        protected float _currTime;
        /// 
        /// 状态 0=等待状态 1=飞行状态 2=碰撞状态
        /// 
        public int State;
        private IUnRegister _unRegister;
        protected bool _isOne;
        protected ParticleSystemPool ParticleSystemPool;
        public void Init(CombatMagicWeaponEntity a, CombatMagicWeaponEntity b, int id)
        {
            this.id = id;
            if (a.IsEnemy)
            {
                (a, b) = (b, a);
            }
            this.a = a;
            this.b = b;
            a.MagicWeaponCollisionId = id;
            b.MagicWeaponCollisionId = id;
            State = 0;
        }
        private void OnTriggerEnter(Collider collider, ITriggerEntity triggerEntity)
        {
            HeroEntityMono heroEntityMono = collider.GetComponent();
            if (heroEntityMono == null || (heroEntityMono.combatHeroEntity != b))
            {
                return;
            }
            if (State != 1)
            {
                return;
            }
            if ((a.HpBl <= 30 || b.HpBl < 20))
            {
                MagicWeaponPingDouState.MagicWeaponPingDouData rollingStateData =
                    CObjectPool.Instance.Fetch();
                rollingStateData.target = b;
                a.CombatAIBasic.ChangeState(CombatHeroStateType.MagicWeaponPingDou,
                    rollingStateData);
                MagicWeaponPingDouState.MagicWeaponPingDouData rollingStateData2 =
                    CObjectPool.Instance.Fetch();
                rollingStateData.target = a;
                b.CombatAIBasic.ChangeState(CombatHeroStateType.MagicWeaponPingDou,
                    rollingStateData2);
                State = 2;
                _currTime = 0;
                CombatController.currActiveCombat.GameTimeLineParticleFactory.CreateParticle("fx_fb_duipin_dian",
                    a.dotPos, null, false, null, delegate(ParticleSystemPool particleSystemPool)
                    {
                        particleSystemPool.transform.rotation =
                            a.GameObject.transform.rotation;
                        ParticleSystemPool = particleSystemPool;
                    });
            }
            else
            {
                CombatController.currActiveCombat.GameTimeLineParticleFactory.CreateParticle("fx_fb_duipin_hit",
                    collider.transform.position, null, false, null, delegate(ParticleSystemPool particleSystemPool)
                    {
                        particleSystemPool.transform.rotation =
                            a.GameObject.transform.rotation;
                    });
                a.ReduceHp(40);
                // _trigger = true;
                // _triggerTime = 0.5f;
            }
        }
        protected void SetBesselA()
        {
            _besselPathA = new BesselPath();
            _besselPathA.controlPoints.Add(a.dotPos);
            _besselPathA.controlPoints.Add(a.GameObject.transform.TransformPoint(new Vector3(-9.87f, 4, 2)));
            _besselPathA.controlPoints.Add(b.GameObject.transform.TransformPoint(new Vector3(12.66f, 1, -5)));
            _besselPathA.controlPoints.Add(b.dotPos);
            GameObject G = new GameObject("BesselPathMono11");
            BesselPathMono besselPathMono = G.AddComponent();
            besselPathMono.isRun = true;
            besselPathMono.BesselPath = _besselPathA;
            _besselPathB = new BesselPath();
            _besselPathB.controlPoints.Add(b.dotPos);
            _besselPathB.controlPoints.Add(
                b.GameObject.transform.TransformPoint(new Vector3(-11.89f, -0.29f, 3.28f)));
            _besselPathB.controlPoints.Add(
                a.GameObject.transform.TransformPoint(new Vector3(12.67f, -3.03f, -3.5f)));
            _besselPathB.controlPoints.Add(a.dotPos);
            GameObject G2 = new GameObject("BesselPathMono22");
            BesselPathMono besselPathMono2 = G2.AddComponent();
            besselPathMono2.isRun = true;
            besselPathMono2.BesselPath = _besselPathB;
        }
        protected void SetBesselB()
        {
            _besselPathA = new BesselPath();
            _besselPathA.controlPoints.Add(a.dotPos);
            _besselPathA.controlPoints.Add(a.GameObject.transform.TransformPoint(new Vector3(-3.41f, 4, -7.23f)));
            _besselPathA.controlPoints.Add(b.GameObject.transform.TransformPoint(new Vector3(2.64f, 1, -5)));
            _besselPathA.controlPoints.Add(b.dotPos);
            GameObject G = new GameObject("BesselPathMono11");
            BesselPathMono besselPathMono = G.AddComponent();
            besselPathMono.isRun = true;
            besselPathMono.BesselPath = _besselPathA;
            _besselPathB = new BesselPath();
            _besselPathB.controlPoints.Add(b.dotPos);
            _besselPathB.controlPoints.Add(
                b.GameObject.transform.TransformPoint(new Vector3(-8.5f, -3.93f, 9.58f)));
            _besselPathB.controlPoints.Add(
                a.GameObject.transform.TransformPoint(new Vector3(1.45f, -3.03f, 4.6f)));
            _besselPathB.controlPoints.Add(a.dotPos);
            GameObject G2 = new GameObject("BesselPathMono22");
            BesselPathMono besselPathMono2 = G2.AddComponent();
            besselPathMono2.isRun = true;
            besselPathMono2.BesselPath = _besselPathB;
        }
        public void Update(float t)
        {
            if (State == 0)
            {
                if (a.CombatAIBasic.stateControl.CurrStateName.Equals(CombatHeroStateType.idle) &&
                    b.CombatAIBasic.stateControl.CurrStateName.Equals(CombatHeroStateType.idle))
                {
                    State = 1;
                    int odds = Random.Range(0, 100);
                    if (odds < 50)
                    {
                        SetBesselA();
                    }
                    else
                    {
                        SetBesselB();
                    }
                    _unRegister = a.GameObject.OnTriggerEnterEvent(this, OnTriggerEnter);
                    _isOne = false;
                    _currTime = 0;
                }
            }
            if (State == 1)
            {
                _currTime += t * 0.7f;
                float v = CustomTweenManager.AnimationCurveLibrary.fabaoDuiPing.Evaluate(_currTime);
                BesselPath besselPath = _isOne ? _besselPathB : _besselPathA;
                Vector3 a1 = besselPath.CalculatePoint(v);
                Vector3 a2 = besselPath.CalculatePoint(v - 0.01f);
                Vector3 b1 = besselPath.CalculatePoint(1 - v);
                Vector3 b2 = besselPath.CalculatePoint(1 - v + 0.01f);
                a.combatHeroGameObject.SetPosition(a1);
                a.GameObject.transform.rotation = Quaternion.LookRotation((a1 - a2).normalized);
                b.combatHeroGameObject.SetPosition(b1);
                b.GameObject.transform.rotation = Quaternion.LookRotation((b1 - b2).normalized);
                if (_currTime >= 1)
                {
                    _currTime = 0;
                    _isOne = !_isOne;
                }
            }
            else if (State == 2)
            {
                _currTime += t;
                if (_currTime > 0.1f)
                {
                    _currTime = 0;
                    if (a != null)
                    {
                        a.ReduceHp(1);
                    }
                    if (b != null)
                    {
                        b.ReduceHp(1);
                    }
                }
            }
        }
        public void SetState(int state)
        {
            State = state;
        }
        public override void ActiveObj()
        {
        }
        public override void DormancyObj()
        {
            a = null;
            b = null;
        }
        public void Finish()
        {
            LogTool.Log("碰撞完成");
            if (a.HpBl > 0)
            {
                a.CombatAIBasic.ChangeState(CombatHeroStateType.att);
                a.MagicWeaponCollisionId = -1;
                b.MagicWeaponCollisionId = -1;
            }
            if (b.HpBl > 0)
            {
                b.CombatAIBasic.ChangeState(CombatHeroStateType.att);
                a.MagicWeaponCollisionId = -1;
                b.MagicWeaponCollisionId = -1;
            }
            GObjectPool.Instance.Recycle(ParticleSystemPool);
            ParticleSystemPool = null;
        }
        public string tag { get; }
    }
}