using System;
// ReSharper disable InconsistentNaming

namespace BO.ProcessServer.Business.StatisticCounters
{
    internal class TimespanMAXCounter : TimespanCounter
    {
        public TimeSpan TotalValue { get; private set; }

        public TimespanMAXCounter(string name) : base(name)
        {
        }

        public override void Stop()
        {
            base.Stop();
            var value = GetValueInternal();
            if (TotalValue < value)
                TotalValue = value;
        }

        public override object GetValue()
        {
            return TotalValue;
        }

        private TimeSpan GetValueInternal()
        {
            if (!StartTimestamp.HasValue)
                return TimeSpan.Zero;

            if (!StopTimestamp.HasValue)
                return DateTime.Now - StartTimestamp.GetValueOrDefault();

            return StopTimestamp.GetValueOrDefault() - StartTimestamp.GetValueOrDefault();
        }
    }
}
