using System.Diagnostics;
using System.Threading;

namespace VCommon.VApplication.Security
{
    public class Delayer
    {
        private readonly int _milliseconds;
        private readonly Stopwatch _stopwatch;

        public Delayer(int milliseconds = 1000, bool immediatelyStart = false)
        {
            _milliseconds = milliseconds;
            _stopwatch = new Stopwatch();
            if (immediatelyStart) Start();
        }

        public void Start()
        {
            _stopwatch.Restart();
        }

        public void StopAndDelay()
        {
            _stopwatch.Stop();
            if (_stopwatch.ElapsedMilliseconds < _milliseconds) Thread.Sleep((int)(_milliseconds - _stopwatch.ElapsedMilliseconds));
        }
    }
}