using System;
using System.Diagnostics;

namespace Valg.EVA.Skanning.Diagnostics
{
    public class TimingScope : IDisposable
    {
        private readonly string scopeName;
        private readonly ITimingResult timingResult;
        private readonly Stopwatch watch;

        public TimingScope(string scopeName = "TimingScope", ITimingResult timingResult = null)
        {
            this.scopeName = scopeName;
            this.timingResult = timingResult;
            watch = new Stopwatch();
            watch.Start();
        }

        public void Dispose()
        {
            watch.Stop();

            if (timingResult == null)
            {
                Trace.TraceInformation("{0} took {1} seconds to complete", scopeName, watch.Elapsed.TotalSeconds);
            }
            else
            {
                timingResult.TimingResult = watch.Elapsed;
            }

            
        }
    }
}
