using System;
using System.Collections;
using System.Collections.Generic;
using CombatLibrary.CombatLibrary.CombatCore;
using CombatLibrary.CombatLibrary.CombatCore.CustomizeTimeLogic.FxLogic;
using UnityEngine;
using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;


public abstract class TimeLineEventLogicBasic : IDisposable
{
    public TimeLineAssetSerialization timeLineAssetSerialization
    {
        get { return mTimeLineAssetSerialization; }
    }

    protected TimeLineAssetSerialization mTimeLineAssetSerialization;


    /// <summary>
    /// 是否进入
    /// </summary>
    protected bool IsEnter;

    protected bool IsLeave;

    public ILifeCycle castEntity
    {
        get { return _castEntity; }
    }

    public ITimeLineTriggerEntity ITimeLineTriggerEntity
    {
        get { return _timeLineTriggerEntity; }
    }

    public TriggerData extraData
    {
        get { return _extraData; }
        set { _extraData = value; }
    }

    /// <summary>
    /// 施放者（谁施放的这个效果 (EntityBasic)）
    /// </summary>
    protected ILifeCycle _castEntity;

    /// <summary>
    /// 触发者（谁触发的这个效果（skill））
    /// </summary>
    protected ITimeLineTriggerEntity _timeLineTriggerEntity;

    protected BetterList<ILifetCycleHitPoint> currTarget;
    protected TriggerData _extraData;

    public Vector3[] customizePos
    {
        get { return _customizePos; }
    }

    protected Vector3[] _customizePos;


    protected TimeLineEventLogicGroupBasic _timeLineEventLogicGroup;

    public TimeLineEventLogicGroupBasic TimeLineEventLogicGroup
    {
        get { return _timeLineEventLogicGroup; }
    }

    public void InitEventLogic(TimeLineEventLogicGroupBasic timeLineEventLogicGroup,
        TimeLineAssetSerialization timeLineAssetSerialization)
    {
        mTimeLineAssetSerialization = timeLineAssetSerialization;
        this._timeLineEventLogicGroup = timeLineEventLogicGroup;
    }


    /// <summary>
    /// timeline触发后持续更新
    /// </summary>
    /// <param name="time">当前时间线从执行到现在的时间</param>
    public void TimeUpdate(float time)
    {
        if (time >= mTimeLineAssetSerialization.startTime)
        {
            if (!IsEnter)
            {
                IsEnter = true;
                Enter();
            }
        }

        ProTimeUpdate();
        if (time >= mTimeLineAssetSerialization.endTime)
        {
            if (!IsLeave)
            {
                IsLeave = true;
                Leave();
            }
        }
    }

    protected void Enter()
    {
        ProEnter();
    }

    protected void Leave()
    {
        ProLeave();
    }

    public void Pause()
    {
        ProPause();
    }


    protected virtual void ProPause()
    {
    }

    public void SetCombatInfo(ILifeCycle castEntity, ITimeLineTriggerEntity targetEntity,
        BetterList<ILifetCycleHitPoint> currTarget,
        TriggerData extraData,
        Vector3[] customizePos)
    {
        this._castEntity = castEntity;
        this._timeLineTriggerEntity = targetEntity;
        this.currTarget = currTarget;
        this._extraData = extraData;
        this._customizePos = customizePos;
        ProSetCombatInfo();
    }


    ///// <summary>m
    ///// 关闭FXinfo，隐藏掉改info创建并在使用的特效
    ///// </summary>
    public void BreakTimeLine()
    {
        if (currTarget != null)
        {
            currTarget.Clear();
        }

        ProBreakTimeLine();
        Dispose();
    }


    public void Dispose()
    {
        if (!IsLeave && IsEnter)
        {
            IsLeave = true;
            Leave();
        }

        ProDispose();
    }


    public BetterList<ILifetCycleHitPoint> GetSkillTarget()
    {
        if (mTimeLineAssetSerialization.targetEntityType.HasFlag(FXTargetType.Oneself))
        {
            BetterList<ILifetCycleHitPoint> target = CombatListPool<ILifetCycleHitPoint>.Instance.Get();
            target.Add(_castEntity.GetMainHotPoin<ILifetCycleHitPoint>(true));
            return target;
        }
        else
        {
            if (currTarget != null && currTarget.Count > 0)
            {
                return FilterTargetForCombat(currTarget);
            }

            if (_timeLineTriggerEntity == null)
            {
                return new BetterList<ILifetCycleHitPoint>(1);
            }

            ITimeLineTargetEntity timeLineTargetEntity = _timeLineTriggerEntity as ITimeLineTargetEntity;
            if (timeLineTargetEntity == null)
            {
                return new BetterList<ILifetCycleHitPoint>(1);
            }

            currTarget = CombatListPool<ILifetCycleHitPoint>.Instance.Get();
            BetterList<ILifetCycleHitPoint> getTarget = timeLineTargetEntity.GetTineLineTargetEntity(this);
            currTarget.AddRange(getTarget);
            if ((int)mTimeLineAssetSerialization.targetEntityType != 2)
            {
                BetterList<ILifetCycleHitPoint> point =
                    timeLineTargetEntity.FilterTarget(this, mTimeLineAssetSerialization.targetEntityType, currTarget);
                if (currTarget != point)
                {
                    currTarget.Dispose();
                }
                currTarget = point;
            }

            return FilterTargetForCombat(currTarget);
        }
    }

    private BetterList<ILifetCycleHitPoint> FilterTargetForCombat(BetterList<ILifetCycleHitPoint> point)
    {
        ITimeLineTargetEntity timeLineTargetEntity = _timeLineTriggerEntity as ITimeLineTargetEntity;
        if (timeLineTargetEntity == null)
        {
            return point;
        }

        return timeLineTargetEntity.FilterTargetForCombat(this, mTimeLineAssetSerialization.targetEntityType, point);
    }

    protected abstract void ProSetCombatInfo();
    protected abstract void ProEnter();
    protected abstract void ProLeave();
    protected abstract void ProTimeUpdate();
    protected abstract void ProBreakTimeLine();

    protected virtual void ProDispose()
    {
        this._timeLineTriggerEntity = null;
        this._castEntity = null;
        this.currTarget = null;
        this._extraData = default;
        this._customizePos = null;
    }
}