using System.Collections;
using System.Collections.Generic;
using Common.Utility.CombatTimer;
using Fort23.Core;
#if !COMBAT_SERVER
using Fort23.Core;
#endif
using Fort23.UTool;

using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;
#if !COMBAT_SERVER
using UnityEngine;
#endif

namespace UTool.CustomizeTimeLogic.FxLogic.TimeLineEventLogic
{
    public class TimeLineEventParticleLogicBasic : TimeLineEventLogicBasic
    {
        // private Timer timer;

        private List<Clock> loadTask = new List<Clock>();

        public List<ParticleSystemPool> loopFx = new List<ParticleSystemPool>();

        private CombatTimer _combatTimer;

        public TimeLinePlayFxSerializtion timeLinePlayFxSerializtion
        {
            get { return mTimeLineAssetSerialization as TimeLinePlayFxSerializtion; }
        }


        protected override void ProSetCombatInfo()
        {
        }

        protected override void ProEnter()
        {
            // ITimeLineTriggerEvent trigger = _targetEntity as ITimeLineTriggerEvent;
            // if (trigger != null)
            {
                BetterList<ILifetCycleHitPoint> timeLineEntities = GetSkillTarget();
                ExecuteFxTarget(timeLineEntities, 0);
            }
        }

        protected override void ProLeave()
        {
        }

        protected override void ProTimeUpdate()
        {
        }

        protected override void ProBreakTimeLine()
        {
            for (int i = 0; i < loadTask.Count; i++)
            {
                loadTask[i].Dispose();
            }

            CombatTimerManager.Instance.RemoveTimer(_combatTimer);
            _combatTimer = null;
            loadTask.Clear();
        }

        public void CloseLoopFx()
        {
            loopFx.Clear();
        }

        protected override void ProDispose()
        {
            for (int i = 0; i < loadTask.Count; i++)
            {
                loadTask[i].Dispose();
            }

            CombatTimerManager.Instance.RemoveTimer(_combatTimer);
            _combatTimer = null;
            loadTask.Clear();
            loopFx.Clear();
            if (currTarget != null)
            {
                currTarget.Dispose();
            }

            currTarget = null;
        }


        private void ExecuteFxTarget(BetterList<ILifetCycleHitPoint> combatTarget, int index)
        {
            if (combatTarget.Count <= index)
            {
                return;
            }

            ILifetCycleHitPoint lifeCycle = combatTarget[index];
            if (lifeCycle == null)
            {
                LogTool.Log("一个空的目标");
                index++;
                ExecuteFxTarget(combatTarget, index);
                return;
            }

            if (!string.IsNullOrEmpty(timeLinePlayFxSerializtion.fxName))
            {
                ILifetCycleHitPoint fxTarget = null;
                Vector3 startPos = Vector3.one;
                SpecialDotInfo targetSpecialDotInfo = null;
                switch (timeLinePlayFxSerializtion.locationType)
                {
                    case FXLocationType.Oneself:
                        fxTarget = _castEntity.GetMainHotPoin<ILifetCycleHitPoint>(true);
                        break;
                    case FXLocationType.Target:
                        fxTarget = lifeCycle;
                        break;
                    case FXLocationType.CustomTarget:
                        fxTarget = lifeCycle;
                        break;

                    case FXLocationType.SceneZero:
                        fxTarget = _castEntity.GetMainHotPoin<ILifetCycleHitPoint>(false);
                        startPos = TimeLineEventLogicGroup.SceneWorldPos();
                        break;

                    case FXLocationType.OneselfSpecialDot:
                        fxTarget = _castEntity.GetMainHotPoin<ILifetCycleHitPoint>(false);
                        break;
                    case FXLocationType.TargetSpecialDot:
                        fxTarget = lifeCycle;
                        break;
                }


                if (fxTarget != null)
                {
                    if (!string.IsNullOrEmpty(timeLinePlayFxSerializtion.hitPointGroupName))
                    {
                        ILifetCycleHitPoint newHitPoint =
                            fxTarget.IfLifeCycle.GetILifetCycleHitPoint<ILifetCycleHitPoint>(
                                timeLinePlayFxSerializtion.hitPointGroupName, true,true);
                        targetSpecialDotInfo =
                            newHitPoint.GetSpecialDotInfo(timeLinePlayFxSerializtion.targetSpecialDotName);
                    }
                    else
                    {
                        targetSpecialDotInfo =
                            fxTarget.GetSpecialDotInfo(timeLinePlayFxSerializtion.targetSpecialDotName);
                    }
                }

                if (timeLinePlayFxSerializtion.isActivityCustomTargetPos&&customizePos!=null&&customizePos.Length>timeLinePlayFxSerializtion.CustomTargetPosIndex)
                {
                    startPos = customizePos[timeLinePlayFxSerializtion.CustomTargetPosIndex];
                    targetSpecialDotInfo = null;
                }
                else if (targetSpecialDotInfo != null&&timeLinePlayFxSerializtion.locationType!=FXLocationType.SceneZero)
                {
                    startPos = targetSpecialDotInfo.GetWorlPos();
                }

                if (timeLinePlayFxSerializtion.isGround)
                {
                    startPos = new Vector3(startPos.x, (float) 0.7f);
                }

                LoadFx(lifeCycle, fxTarget, startPos,targetSpecialDotInfo);
            }

            index++;
            if (timeLinePlayFxSerializtion.intervalTime > 0)
            {
                _combatTimer = CombatTimerManager.Instance.AddTimer((float)timeLinePlayFxSerializtion.intervalTime*index,
                    delegate {  });
                ExecuteFxTarget(combatTarget, index);
            }
            else
            {
                ExecuteFxTarget(combatTarget, index);
            }
        }

        protected void LoadFx(ILifetCycleHitPoint effectTarget, ILifetCycleHitPoint fxTarget, Vector3 startPos,
            SpecialDotInfo targetSpecialDotInfo)
        {
           
            ITimeLineParticleFactory iTimeLineParticleFactory =  TimeLineFxParticleTool.Instance.TimeLineFxParticleFactory;
            if (iTimeLineParticleFactory != null)
            {
                Clock clock = iTimeLineParticleFactory.CreateParticle(this, effectTarget, fxTarget, startPos,targetSpecialDotInfo,
                    delegate(IGObjectPoolInterface systemPool)
                    {
                        ParticleSystemPool pool = systemPool as ParticleSystemPool;
                        if (pool != null)
                        {
// #if !COMBAT_SERVER
//                             pool.TimeLineEventLogicGroupBasic = _timeLineEventLogicGroup;
//                             pool.TimeLinePlayFxSerializtion = timeLinePlayFxSerializtion;
// #endif

                            if (timeLinePlayFxSerializtion.isLoop)
                            {
                                loopFx.Add(pool);
                            }

                            if (timeLinePlayFxSerializtion.isAttSpeed)
                            {
                                ITimeLineParticlePlaySpeed timeLineParticlePlaySpeed =
                                    _castEntity as ITimeLineParticlePlaySpeed;
#if !COMBAT_SERVER
                                pool.palySpeed = timeLineParticlePlaySpeed.GetCurrPlaySpeed(timeLinePlayFxSerializtion);
#endif
                            }
                            else
                            {
#if !COMBAT_SERVER
                                pool.palySpeed = (float) 1;
#endif
                            }

                            _timeLineEventLogicGroup.OnInitParticleSystemPoolCallBack?.Invoke(pool);
                        }
                    });
                if (clock != null)
                {
                    loadTask.Add(clock);
                }
            }
        }
    }
}