using System;
using System.Collections.Generic;
using MrWu.Debug;
using System.Threading;

namespace Server
{
    /// <summary>
    /// 定时器标记管理
    /// </summary>
    public class TimerManager
    {
        /// <summary>
        /// 是否触发定时器事件
        /// </summary>
        private volatile bool isTimerMillEvt;
        /// <summary>
        /// 秒级定时器
        /// </summary>
        private volatile bool isSecondTimeEvt;

        private volatile int mInterval;

        /// <summary>
        /// 是否在运行中
        /// </summary>
        public bool isRun {
            get;
            private set;
        }

        /// <summary>
        /// 触发定时器的时间间隔
        /// </summary>
        public int Interval {
            get {
                return mInterval;
            }
        }

        /// <summary>
        /// 设置定时器的时间间隔
        /// </summary>
        /// <param name="Interval"></param>
        public void SetInterval(int Interval) {
            if (Interval < 1) {
                Debug.Error("不能设置这么小的事件间隔:" + Interval);
                return;
            }

            mInterval = Interval;
        }

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="Interval">定时器的时间间隔</param>
        public TimerManager(int Interval) {
            if (Interval < 1)
                throw new ArgumentOutOfRangeException("Interval 必须为大于 1 的值!");

            this.mInterval = Interval;
        }

        /// <summary>
        /// 秒级定时器
        /// </summary>
        private Timer SecondTimer;

        /// <summary>
        /// 毫秒级定时器
        /// </summary>
        private Timer MillTimer;

        /// <summary>
        /// 开始定时器的触发
        /// </summary>
        public void Start() {
            if (SecondTimer == null) {
                SecondTimer = new Timer(SecoendTime, null, 0, 1000);
                MillTimer = new Timer(MillTime, null, 0, Interval);
            } else {
                SecondTimer.Change(0, 1000);
                MillTimer.Change(0, 100);
            }
        }

        /// <summary>
        /// 停止定时器的触发
        /// </summary>
        public void Stop() {
            SecondTimer.Change(-1, 1000);
            MillTimer.Change(-1, Interval);
        }

        private void SecoendTime(object state) {
            isSecondTimeEvt = true;
        }

        private void MillTime(object state) {
            isTimerMillEvt = true;
        }

        /// <summary>
        /// 获取是否需要定时器触发
        /// </summary>
        /// <returns></returns>
        public bool GetSecoend() {
            if (isSecondTimeEvt) {
                isSecondTimeEvt = false;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取是否需要触发毫秒定时器
        /// </summary>
        /// <returns></returns>
        public bool GetMillScoend() {
            if (isTimerMillEvt) {
                isTimerMillEvt = false;
                return true;
            }
            return false;
        }

    }
}