using System;
using System.Collections;
using System.Collections.Generic;
using Fort23.Common;
using Fort23.Core;
using Fort23.UTool;
using UnityEngine;
using UnityEngine.Rendering;
using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;
using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventLogic;

public class GameTimeLineParticleFactory : ITimeLineParticleFactory
{
    protected List<CombatParticleSystemPool> allCombatParticleSystem =
        new List<CombatParticleSystemPool>();
    public Clock CreateParticle(TimeLineEventParticleLogicBasic timeLineEventParticleLogicBasic,
        ILifetCycleHitPoint effectTarget,
        ILifetCycleHitPoint fxTarget, Vector3 startPos, SpecialDotInfo targetSpecialDotInfo,
        Action<IGObjectPoolInterface> callBack)
    {
        Clock clock = CreateParticle(timeLineEventParticleLogicBasic.timeLinePlayFxSerializtion.fxName, startPos,
            fxTarget,
            false, targetSpecialDotInfo, delegate(ParticleSystemPool combatParticleSystemPool)
            {
                CombatParticleSystemPool pool = combatParticleSystemPool as CombatParticleSystemPool;
#if !COMBAT_SERVER
                pool.InitCombatParticleSystem();
#endif
          

                if (targetSpecialDotInfo != null)
                {
#if !COMBAT_SERVER
                    // CombatHeroEntity heroEntity = targetSpecialDotInfo.heroEntity as CombatHeroEntity;
                    // if (heroEntity.CombatHeroTransform.iGObjectPoolInterface == null)
                    // {
                    //     return;
                    // }
#endif

                    // if (!timeLineEventParticleLogicBasic.timeLinePlayFxSerializtion.isActivityCustomTargetPos)
                    // {
                    //     startPos = targetSpecialDotInfo.GetWorlPos();
                    // }
#if !COMBAT_SERVER
                    if (!timeLineEventParticleLogicBasic.timeLinePlayFxSerializtion.isNotRotate)
                    {
                        pool.transform.eulerAngles = targetSpecialDotInfo.GetEulerAngles();
                        pool.isNotRotion = false;
                    }
                    else
                    {
                        pool.isNotRotion = true;
                    }


                    // if (heroEntity != null)
                    // {
                    //     pool.transform.SetParent(heroEntity.CombatHeroTransform.iGObjectPoolInterface.own.transform);
                    // }
                    // else
                    // {
                    //     pool.transform.SetParent(targetSpecialDotInfo.targetTran);
                    // }
#endif
                }
                else
                {
                }
#if !COMBAT_SERVER
                pool.gameObject.SetActive(false);
                pool.transform.position = startPos;

                if (timeLineEventParticleLogicBasic.timeLinePlayFxSerializtion.isFollowTarget &&
                    timeLineEventParticleLogicBasic.timeLinePlayFxSerializtion.locationType !=
                    FXLocationType.SceneZero)
                {
                    pool.transform.localEulerAngles = Vector3.zero;
                    pool.targetSpecialDotInfo = targetSpecialDotInfo;
                }
                else
                {
                    // pool.transform.SetParent(null);
                    pool.targetSpecialDotInfo = null;
                }
#endif


                // if (fxAiDataBasic != null)
                // {
                //     if (CombatManager.Instance.currFrame != initFrame)
                //     {
                //         LogTool.Error(
                //             $"战斗中特效没有预加载 {CombatManager.Instance.currFrame - initFrame} {timeLineEventParticleLogicBasic.timeLinePlayFxSerializtion.fxName}");
                //     }
                //
                //     FxAILogicBasic fxAILogicBasic = CreateFxAILogicBasic(fxAiDataBasic);
                //     if (fxAILogicBasic != null)
                //     {
                //         if (!allFxAiLogicBasic.Contains(fxAILogicBasic))
                //         {
                //             allFxAiLogicBasic.Add(fxAILogicBasic);
                //         }
                //
                //         fxAILogicBasic.SetAIMonoObj(combatParticleSystemPool);
                //         fxAILogicBasic.Init(startPos, fxAiDataBasic, timeLineEventParticleLogicBasic, effectTarget,
                //             targetSpecialDotInfo);
                //     }
                // }
#if !COMBAT_SERVER
                pool.gameObject.SetActive(true);
#endif
                callBack?.Invoke(combatParticleSystemPool);
            });
        return clock;
    }

    public Clock CreateParticle(string fxName, Vector3 startPos, ILifetCycleHitPoint fxTarget, bool isRotate,
        SpecialDotInfo targetSpecialDotInfo, Action<ParticleSystemPool> callBack, bool isNotLoadFx = false)
    {
#if !COMBAT_SERVER
        Clock clock = ClockPool.GetClock();
     

     

        CTask<CombatParticleSystemPool> task = GObjectPool.Instance.FetchAsync(fxName + ".prefab",
            delegate(CombatParticleSystemPool pool)
            {
                if (pool == null)
                {
                    return;
                }

                // if (!isActive)
                // {
                //     GObjectPool.Instance.RecycleForDestroy(pool);
                //     return;
                // }

                if (!allCombatParticleSystem.Contains(pool))
                {
                    allCombatParticleSystem.Add(pool);
                }
                else
                {
                    LogTool.Error("添加重复");
                }
                //
                // if (targetSpecialDotInfo != null && targetSpecialDotInfo.rootTrnsform != null)
                // {
                //     SortingGroup sortingGroup = targetSpecialDotInfo.rootTrnsform.GetSortingGroup();
                //     if (sortingGroup != null)
                //     {
                //         pool.SetRootSortingGroup(sortingGroup);
                //     }
                //     else
                //     {
                //         pool.SetOder(0, "Hero");
                //     }
                // }
                // else
                // {
                //     pool.SetOder(10000, "Hero");
                // }

                if (isRotate && fxTarget != null)
                {
                    SpecialDotInfo specialDotInfo = fxTarget.GetSpecialDotInfo(null);

                    if (specialDotInfo != null)
                    {
                        pool.transform.eulerAngles = specialDotInfo.GetEulerAngles();
                    }
                }

                float size = 1;
                // if (fxTarget != null)
                // {
                //     CombatHeroEntity combatHeroEntity = fxTarget.IfLifeCycle.This<CombatHeroEntity>();
                //     if (combatHeroEntity != null)
                //     {
                //         HeroGameObjectConfig heroGameObjectConfig =
                //             combatHeroEntity.HeroGameObjectConfig;
                //         if (heroGameObjectConfig != null)
                //         {
                //             size = heroGameObjectConfig.size;
                //         }
                //     }
                // }

                pool.SetSize(size);
                pool.own.transform.position = startPos;


                callBack?.Invoke(pool);
            }, clock);
        return clock;
#else
            callBack?.Invoke(new CombatParticleSystemPool());
            return null;
#endif
    }
}