using System;
using System.Threading;
using System.Threading.Tasks;

namespace Binance.TradeRobot.Common
{
    /// <summary>
    /// 延迟触发器
    /// </summary>
    public class DelayTrigger
    {
        public DelayTrigger(int delayTime = 500)
        {
            if (delayTime < 500)
                delayTime = 500;
            this.delayTime = delayTime;
        }
        /// <summary>
        /// 延迟执行时间(ms)
        /// </summary>
        private int delayTime;

        /// <summary>
        /// 关键字
        /// </summary>
        private string currentKey;

        /// <summary>
        /// 是否可以执行
        /// </summary>
        private volatile bool canExecute;

        /// <summary>
        /// 是否正在延迟响应中
        /// </summary>
        private volatile bool isDelaying;


        public Action<string> OnExecute;


        public void SetKey(string key)
        {
            currentKey = key;
            if (isDelaying)
            {
                canExecute = false;
                return;
            }
            Task.Factory.StartNew(delegate
            {
                isDelaying = true;
                while (true)
                {
                    canExecute = true;
                    Thread.Sleep(delayTime);
                    if (canExecute)
                    {
                        Console.WriteLine($"{currentKey} Execute at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")}");
                        OnExecute?.Invoke(currentKey);
                        isDelaying = false;
                        break;
                    }
                }
            });
        }
    }
}