using System;
using Fort23.Core;
using Fort23.Mono;
#if !COMBAT_SERVER
using CombatLibrary.CombatLibrary.CombatCore.GPool;
using UnityEngine;
#endif
namespace Fort23.UTool
{
    public class ParticleSystemPool : GObjectPoolBasic
    {
#if !COMBAT_SERVER
        //public string endPlayerPartic;
        protected ParticleSystem[] m_particles;
        protected RenderSetting[] m_renderSetting;
      
        protected DelayShow[] delayShows;
        public float delayTime;
        protected float currDelayTime;
        public string endFx;
        private bool isShow;
  
        /// 
        /// 播放速度
        /// 
        public float palySpeed = (float) 1;
  
        private BetterList _lastLoopFx = new BetterList();
        private LoopDelayHide _loopDelayHide;
        protected HideShowEndFx _hideShowEndFx;
        public ParticleSystem[] Particles
        {
            get
            {
                if (m_particles == null&&transform!=null)
                {
                    m_particles = transform.GetComponentsInChildren(false);
                }
                return m_particles;
            }
        }
        public Animator[] Animators
        {
            get
            {
                if (m_animators == null&&transform!=null)
                {
                    m_animators = transform.GetComponentsInChildren(false);
                }
                return m_animators;
            }
        }
        
        private Animator[] m_animators;
        public RenderSetting[] RenderSetting
        {
            get
            {
                if (m_renderSetting == null&&transform!=null)
                {
                    m_renderSetting = transform.GetComponentsInChildren(false);
                }
                return m_renderSetting;
            }
        }
        public DelayShow[] DelayShows
        {
            get
            {
                if (delayShows == null)
                {
                    delayShows = transform.GetComponentsInChildren(false);
                }
                return delayShows;
            }
        }
       
        [ContextMenu("Replay")]
        public void Replay()
        {
            ResetData();
            ActiveObj();
        }
        private float m_duration = -1f;
        protected override void ProResetData()
        {
            try
            {
                //ParticleSystem a;
                //a.SendMessageUpwards
                if (gameObject == null)
                {
                    return;
                }
                if (Particles != null)
                {
                    for (int i = 0; i < Particles.Length; i++)
                    {
                        Particles[i].Stop();
                        Particles[i].Clear();
                    }
                }
                //NsEffectManager.RunReplayEffect(gameObject, true);
            }
            catch (Exception ex)
            {
                LogTool.Warning(ex.ToString());
            }
        }
        public override async CTask DelayHide()
        {
    
            if (_loopDelayHide == null)
            {
                await base.DelayHide();
                return;
            }
            bool loopTime = false;
            if (Particles != null)
            {
                for (int i = 0; i < Particles.Length; i++)
                {
                    bool isLoop = Particles[i].main.loop;
                    if (isLoop && !_loopDelayHide.excludeObject.Contains(Particles[i].gameObject))
                    {
                        loopTime = true;
                        _lastLoopFx.Add(Particles[i]);
                        ParticleSystem.MainModule mainModule = Particles[i].main;
                        mainModule.loop = false;
                    }
                }
            }
            if (loopTime)
            {
                await TimerComponent.Instance.WaitAsync((int) (_loopDelayHide.delayTime * 1000));
            }
        }
        
        protected override void ProActiveObj()
        {
            //gameObject.SetActive(true);
            _loopDelayHide = own.GetComponent();
            _hideShowEndFx = own.GetComponent();
            for (int i = 0; i < Particles.Length; i++)
            {
                if (_lastLoopFx.Contains(Particles[i]))
                {
                    ParticleSystem.MainModule main = Particles[i].main;
                    main.loop = true;
                }
                Particles[i].Stop();
                Particles[i].Clear(true);
                Particles[i].Play(true);
            }
            if (DelayShows != null)
            {
                for (int i = 0; i < DelayShows.Length; i++)
                {
                    DelayShows[i].Init();
                }
            }
         
            currDelayTime = (float) 0;
            StaticUpdater.Instance.AddRenderUpdateCallBack(UpdatePool);
            isShow = true;
        }
        protected virtual void UpdatePool()
        {
            currDelayTime += Time.deltaTime;
            if (IsFinish())
            {
                OnDurationUp();
            }
        }
        public bool IsFinish()
        {
            if (Particles.Length <= 0&&delayTime<=0)//没有特效的表现
            {
                delayTime = 30;
            }
            if (currDelayTime < delayTime)
            {
                return false;
            }
            bool isFinish = true;
          
            for (int i = 0; i < Particles.Length; i++)
            {
                if (!Particles[i])
                    continue;
                if (Particles[i].IsAlive())
                {
                    isFinish = false;
                    break;
                }
            }
         
            return isFinish;
        }
        protected virtual void OnDurationUp()
        {
            if (this == null ||
                gameObject == null)
            {
                return;
            }
            CloseData();
            GObjectPool.Instance.Recycle(this);
        }
        private void CloseData()
        {
            StaticUpdater.Instance.RemoveRenderUpdateCallBack(UpdatePool);
        }
        protected override void ProDormancyObj()
        {
            if (isShow)
            {
                if (!string.IsNullOrEmpty(endFx))
                {
                    // LoadResourceTask fxTask = CombatController.Instance.LoadCombatFX(endFx,
                    //     null, delegate(IPoolObject pool)
                    //     {
                    //         if (pool != null)
                    //         {
                    //             pool.myTran.position = transform.position;
                    //         }
                    //     });
                }
                isShow = false;
            }
            CloseData();
            if (gameObject == null)
            {
                return;
            }
            for (int i = 0; i < Particles.Length; i++)
            {
                Particles[i].Stop();
            }
            gameObject.SetActive(false);
        }
        //private void PlayerEnd()
        //{
        //    if (endPlayerPartic != null)
        //    {
        //        CombatController.Instance.LoadCombatFX(endPlayerPartic, delegate (IPoolObject pool)
        //        {
        //        }, transform.parent, null, null);
        //    }
        //}
        protected override void ProOnDestroy()
        {
            if (!gameObject)
            {
                return;
            }
            base.ProOnDestroy();
            //if (endPlayerPartic != null)
            //{
            //}
            // Destroy(gameObject);
        }
#endif
    }
}