using System;
using System.Collections.Generic;
using Fort23.UTool;
using Utility;

namespace Common.Utility.CombatTimer
{
    public class CombatTimerManager : Singleton<CombatTimerManager>
    {
        private List<CombatTimer> m_LifeList = new List<CombatTimer>();
        //private List<Timer> m_suspendableList = new List<Timer>();

        private bool m_isPause = false;

        public bool IsPause
        {
            get { return m_isPause; }
            set { m_isPause = value; }
        }

        public void InitComatTime()
        {
            m_LifeList.Clear();
        }

        public void Release()
        {
            m_LifeList.Clear();
        }

        private void PreSceneChange(params object[] obj)
        {
            m_LifeList.Clear();
            //m_suspendableList.Clear();
        }

        public override void Dispose()
        {
            for (int i = 0; i < m_LifeList.Count; i++)
            {
                m_LifeList[i].Dispose();
            }

            m_LifeList.Clear();
            base.Dispose();
        }

        public void AddTimer(CombatTimer combatTimer)
        {
            if (combatTimer != null)
            {
                //timer.d
                m_LifeList.Add(combatTimer);
                combatTimer.Begin();
            }
        }

        public CombatTimer AddTimer(float duration, CombatTimer.OnTimerCallBack callBack,
            CombatTimer.TimerType type = CombatTimer.TimerType.GameTimeUpdate, uint maxHitCount = 1)
        {
            if (duration <= 0)
            {
                callBack();
                return null;
            }

            CombatTimer combatTimer = new CombatTimer(duration, callBack, type, maxHitCount);
            combatTimer.isCombat = true;
            AddTimer(combatTimer);
            return combatTimer;
        }

        public void RemoveTimer(CombatTimer combatTimer)
        {
            if (combatTimer != null)
            {
                if (m_LifeList.Contains(combatTimer))
                {
                    m_LifeList.Remove(combatTimer);
                }

                combatTimer.Dispose();
                combatTimer = null;
            }
        }

        private void Adavance(float deltaTime)
        {
            if (!IsPause)
            {
                if (m_LifeList.Count <= 0)
                {
                    return;
                }

                AdvanceLifeTime(deltaTime);
            }
        }

        private void AdvanceLifeTime(float deltaTime)
        {
            try
            {
                float curTime = deltaTime;
                CombatTimer[] timers = m_LifeList.ToArray();
                for (int i = 0; i < timers.Length; i++)
                {
                    timers[i].DeltaUpdate(curTime);
                }
            }
            catch (Exception e)
            {
                LogTool.Exception(e);
                throw;
            }
        }

        public void ComabtUpdate(float deltaTime)
        {
            Adavance(deltaTime);
        }
    } //TimerManager
}