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 GameLogic.Combat.CombatTool;
using UnityEngine;
using UnityEngine.Rendering;
using Utility;
#endif
namespace Fort23.Common
{
    /// 
    /// 战斗用的,其他地方不要用
    /// 
    public class CombatParticleSystemPool : ParticleSystemPool
    {
        public CombatHeroEntity HeroEntity;
        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 ParticleSystemSize ParticleSystemSize
        {
            get
            {
                if (_particleSystemSize == null)
                {
                    _particleSystemSize = own.GetComponent();
                }
                return _particleSystemSize;
            }
        }
        private ParticleSystemSize _particleSystemSize;
        public Map particleOrder = new Map();
        public Map renderSettingOrder = new Map();
        private int _lastOder;
        private string _lastLayerName;
        // public SortingGroup RootSortingGroup;
        // public void SetRootSortingGroup(SortingGroup sortingGroup)
        // {
        //     SetOder(sortingGroup.sortingOrder, sortingGroup.sortingLayerName);
        //     this.RootSortingGroup = sortingGroup;
        // }
        public override void DormancyObj()
        {
            CombatController.currActiveCombat.GameTimeLineParticleFactory.RemoveCombatParticleSystemPool(this);
            if (HeroEntity != null)
            {
                HeroEntity.heroLoopParticle.Remove(this);
            }
            HeroEntity = null;
            base.DormancyObj();
        }
        public void SetOder(int oder, string layerName)
        {
            // RootSortingGroup = null;
            // if (own != null)
            // {
            //     ShowLayer showLayer = own.GetComponent();
            //     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();
                    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;
            }
            if (targetSpecialDotInfo != null)
            {
                transform.position = targetSpecialDotInfo.targetTran.position;
                transform.eulerAngles = targetSpecialDotInfo.targetTran.eulerAngles;
            }
            deltaTime = _currUpdateTime;
            _currUpdateTime = 0;
           
            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
    }
}