using System;
using Common.Utility.CombatTimer;
using Fort23.UTool;
#if !COMBAT_SERVER
using UnityEngine;
#endif
namespace Common.Utility.CombatTimer
{
    public class CombatTimer
    {
        public enum TimerType
        {
            GameTimeUpdate, //游戏时间更新,受制于游戏时间
            Invalid, //释放
        }
        public bool isCombat;
        public delegate void OnTimerCallBack();
        private float m_maxDuration;
        public float maxDuration
        {
            get { return m_maxDuration; }
            set { m_maxDuration = value; }
        }
        private int m_curHitCount;
        private uint m_maxHitCount;
        private float duration;
        private OnTimerCallBack m_callBack;
        private TimerType m_type = TimerType.GameTimeUpdate;
        public TimerType Type
        {
            get { return m_type; }
        }
        private int m_timerGuid;
        public int TimerGuid
        {
            get { return m_timerGuid; }
        }
        private bool m_isEnabled = true;
        public bool IsEnabled
        {
            get { return m_isEnabled; }
            set { m_isEnabled = value; }
        }
        public string timerName { set; get; }
        /// 
        /// 当前tiemr 已运行时长
        /// 
        public float Duration
        {
            get { return duration; }
        }
        /// 
        /// 当前tiemr 剩余时长
        /// 
        public float LeftTime
        {
            get { return m_maxDuration - duration; }
        }
        private float m_startTime;
        //public bool isStop;
        public CombatTimer(float duration, OnTimerCallBack callBack, TimerType type = TimerType.GameTimeUpdate,
            uint maxHitCount = 1)
        {
            m_maxDuration = duration;
            m_callBack = callBack;
            m_type = type;
            m_maxHitCount = maxHitCount;
        }
       
        public void Begin()
        {
             m_startTime = 0;
            // m_startTime = Time.unscaledTime;
        }
        public void DeltaUpdate(float dateTime) //Executed by FixedUpdate
        {
            if (!IsEnabled)
            {
                return;
            }
            duration += dateTime;
            if (duration >= m_maxDuration)
            {
                int maxCount = 1;
                if (m_maxDuration != 0)
                {
                    maxCount = (int) (duration / m_maxDuration);
                }
                for (int i = 0; i < maxCount; i++)
                {
                    m_curHitCount++;
                    if (m_callBack != null)
                    {
                        m_callBack();
                    }
                    
                    if (m_curHitCount >= m_maxHitCount)
                    {
                        CombatTimerManager.Instance.RemoveTimer(this);
                        break;
                    }
                    else
                    {
                        duration = duration - m_maxDuration;
                    }
                    //m_callBack();
                }
            }
        }
        public void Dispose()
        {
            IsEnabled = false;
            m_maxDuration = 0;
            m_callBack = null;
            m_type = TimerType.Invalid;
            m_curHitCount = 0;
            m_maxHitCount = 0;
        }
    } //Timer
} //Timer