using e_suite.Service.Performance.Interfaces;

namespace e_suite.Service.Performance;

public class PerformanceMarker
{
    private List<PerformanceMarker> _children = new List<PerformanceMarker>();
    private IStopwatch _stopwatch;

    public PerformanceMarker(string name, IStopwatch stopwatch)
    {
        Name = name;
        _stopwatch = stopwatch;
        AddMarker("Start");
    }

    public string Name { get; }

    public List<Marker> Markers { get; } = new List<Marker>();

    public PerformanceMarker this[string name] => _children.Single(child => child.Name.Equals(name));

    public PerformanceMarker this[int index] => _children[index];

    public int Count => _children.Count;

    public PerformanceMarker AddChild(string name)
    {
        var child = new PerformanceMarker(name, _stopwatch);
        _children.Add(child);
        return child;
    }

    public void AddMarker(string name)
    {
        Markers.Add(new Marker(name, (long)(_stopwatch.Elapsed.TotalMilliseconds * 1000000)));
       
    }
}
