
using CombatLibrary.CombatLibrary.CombatCore.CustomizeTimeLogic.FxLogic;
using CombatLibrary.CombatLibrary.CombatCore.Utility;

using Fort23.Core;
using Fort23.UTool;
using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;
#if !COMBAT_SERVER
using Fort23.Mono;
using UnityEngine;
using UnityEngine.Rendering;
using Utility;
#endif

namespace Fort23.Common
{
    /// <summary>
    /// 战斗用的，其他地方不要用
    /// </summary>
    public class CombatParticleSystemPool : ParticleSystemPool
    {
        public float UpdateTime;
        public int guid;
        public SpecialDotInfo targetSpecialDotInfo;
        public bool isNotRotion;
        public int activeCount;
        public int remvoeCount;

        public float UpdateFrequency = (float)0.031;
        private float _currUpdateTime;
        public CombatParticleSystemPool()
        {
          
        }
#if !COMBAT_SERVER
        public void InitCombatParticleSystem()
        {
        }

        public SortingGroup SortingGroup
        {
            get
            {
                if (_sortingGroup == null)
                {
                    _sortingGroup = own.GetComponent<SortingGroup>();
                    if (_sortingGroup == null)
                    {
                        _sortingGroup = own.AddComponent<SortingGroup>();
                    }
                }

                return _sortingGroup;
            }
        }


        private SortingGroup _sortingGroup;


        public ParticleSystemSize ParticleSystemSize
        {
            get
            {
                if (_particleSystemSize == null)
                {
                    _particleSystemSize = own.GetComponent<ParticleSystemSize>();
                }

                return _particleSystemSize;
            }
        }

        private ParticleSystemSize _particleSystemSize;
        public Map<ParticleSystem, int> particleOrder = new Map<ParticleSystem, int>();
        public Map<RenderSetting, int> renderSettingOrder = new Map<RenderSetting, int>();

        private int _lastOder;
        private string _lastLayerName;

        public SortingGroup RootSortingGroup;

        public void SetRootSortingGroup(SortingGroup sortingGroup)
        {
            SetOder(sortingGroup.sortingOrder, sortingGroup.sortingLayerName);
            this.RootSortingGroup = sortingGroup;
        }

        public void SetOder(int oder, string layerName)
        {
            RootSortingGroup = null;

            // if (own != null)
            // {
            //     ShowLayer showLayer = own.GetComponent<ShowLayer>();
            //     if (showLayer != null)
            //     {
            //         if (showLayer.showLayerType == ShowLayer.ShowLayerType.Bottom)
            //         {
            //             oder = -3000;
            //         }
            //         else if (showLayer.showLayerType == ShowLayer.ShowLayerType.Top)
            //         {
            //             oder = 20000;
            //         }
            //     }
            // }

            _lastOder = oder;
            _lastLayerName = layerName;
            ParticleSystem[] Particles = m_particles;
            if (Particles != null)
            {
                for (int i = 0; i < Particles.Length; i++)
                {
                    Renderer renderer = Particles[i].gameObject.GetComponent<Renderer>();
                    if (renderer != null)
                    {
                        if (particleOrder.ContainsKey(Particles[i]))
                        {
                            renderer.sortingOrder = particleOrder[Particles[i]] + oder;
                        }
                        else
                        {
                            particleOrder.Add(Particles[i], renderer.sortingOrder);
                            renderer.sortingOrder = renderer.sortingOrder + oder;
                        }

                        renderer.sortingLayerName = layerName;
                    }
                }
            }

            if (RenderSetting != null)
            {
                for (int i = 0; i < RenderSetting.Length; i++)
                {
                    RenderSetting renderSetting = RenderSetting[i];
                    if (renderSettingOrder.ContainsKey(renderSetting))
                    {
                        renderSetting.SetOrder(renderSettingOrder[renderSetting] + oder, layerName);
                    }
                    else
                    {
                        renderSettingOrder.Add(renderSetting, renderSetting.order);
                        renderSetting.SetOrder(renderSetting.order + oder, layerName);
                    }
                }
            }
        }

        public void SetSize(float size)
        {
            if (ParticleSystemSize == null)
            {
                return;
            }

            own.gameObject.transform.localScale =
                Vector3.one + Vector3.one * ParticleSystemSize.cardinality * (size - 1) +
                Vector3.one * ParticleSystemSize.offValue;
        }

        protected override void ProActiveObj()
        {
            activeCount++;
            GameObjectDestroyPool.Instance.RomoveDestroyObject(own);
      
            base.ProActiveObj();
        }


      

        public override async CTask DelayHide()
        {
            if (_hideShowEndFx != null)
            {
                // TimeLineFxParticleTool.Instance.TimeLineFxParticleFactory.CreateParticle(_hideShowEndFx.hideFxName, new Vector3(_pos), null, false,
                //     null, delegate(CombatParticleSystemPool pool)
                //     {
                //         pool.transform.position = gameObject.transform.position;
                //         pool.transform.eulerAngles = gameObject.transform.eulerAngles;
                //         if (pool != null)
                //         {
                //             pool.SetOder(_lastOder, _lastLayerName);
                //         }
                //     });
            }

            await base.DelayHide();
        }

      

        protected override void ProDormancyObj()
        {
           
            base.ProDormancyObj();
          
            RootSortingGroup = null;
        }

        protected override void ProOnDestroy()
        {
         
            base.ProOnDestroy();
        }

        public int initLogicFrame { get; set; }
        public int initRenderFrame { get; set; }


        public void UnityRenderUpdate(float deltaTime)
        {
            if (transform == null)
            {
                return;
            }

            _currUpdateTime += deltaTime;
            if (_currUpdateTime < UpdateFrequency)
            {
                return;
            }


            deltaTime = _currUpdateTime;
            _currUpdateTime = 0;
            if (RootSortingGroup != null)
            {
                if (!RootSortingGroup.sortingLayerName.Equals(_lastLayerName) ||
                    RootSortingGroup.sortingOrder != _lastOder)
                {
                    SortingGroup sortingGroup = RootSortingGroup;
                    SetOder(sortingGroup.sortingOrder, sortingGroup.sortingLayerName);
                    RootSortingGroup = sortingGroup;
                }
            }

            UpdateTime += deltaTime;
            

           

     
            float speed = 1;
          

            Play(speed, deltaTime);
        }

        private void Play(float speed, float deltaTime)
        {
            if (Particles != null)
            {
                for (int i = 0; i < Particles.Length; i++)
                {
                    if (Particles[i] == null)
                    {
                        continue;
                    }

                    ParticleSystem.MainModule mainMode = Particles[i].main;
                    mainMode.simulationSpeed = speed;
                }
            }

            if (Animators != null)
            {
                for (int i = 0; i < Animators.Length; i++)
                {
                    if (Animators[i] == null)
                    {
                        continue;
                    }

                    Animators[i].speed = speed;
                }
            }

            // if (ska != null)
            // {
            //     for (int i = 0; i < ska.Length; i++)
            //     {
            //         ska[i].Update(speed * deltaTime.value);
            //     }
            // }
        }

        public void UnityRenderUpdateFinish()
        {
        }

        public void CombatUpdate(float deltaTime)
        {
            UnityRenderUpdate(deltaTime);
        }
#else
        public void CombatUpdate(float time)
        {
            UpdateTime += time;
        }

        public int initLogicFrame { get; set; }
        public int initRenderFrame { get; set; }

        public void UnityRenderUpdate(float time)
        {
            
        }

        public void UnityRenderUpdateFinish()
        {
           
        }
#endif
    }
}