namespace ProximaAnalizer.Helpers;

internal class DisplayDataHelper(IDictionary<string, LineType> Left, IDictionary<string, LineType> Right)
{
    public List<DateTime> Time { get; } = [];
    public Dictionary<string, List<float>> DataLeft { get; } = [];
    public Dictionary<string, List<float>> DataRight { get; } = [];

    public void CreateData(List<dynamic> main, List<dynamic>? sub)
    {
        while (sub is not null && main.Count != sub.Count!)
        {
            switch (main.Count > sub.Count)
            {
                case true:
                    main.RemoveAt(main.Count - 1);
                    break;
                case false:
                    sub.RemoveAt(sub.Count - 1);
                    break;
            }
        }

        this.ClearData();


        foreach (var item in main)
        {
            if (item is not IDictionary<string, object> data)
                continue;

            long ticks = (long)data["time"];
            //ticks -= ticks % 10000000;
            DateTime time = new(ticks);
            Time.Add(time);

            foreach (string key in Left.Keys)
            {
                if (!data.TryGetValue(key, out object? value) || value is null)
                    continue;

                if (!DataLeft.TryGetValue(key, out List<float>? collections) || collections is null)
                {
                    collections = [];
                    DataLeft[key] = collections;
                }
                collections.Add(Convert.ToSingle(value));
            }

            foreach (string key in Right.Keys)
            {
                if (!data.TryGetValue(key, out object? value) || value is null)
                    continue;

                if (!DataRight.TryGetValue(key, out List<float>? collections) || collections is null)
                {
                    collections = [];
                    DataRight[key] = collections;
                }
                collections.Add(Convert.ToSingle(value));
            }
        }

        if (sub is null)
            return;

        foreach (var item in sub)
        {
            if (item is not IDictionary<string, object> data)
                continue;

            foreach (string key in Left.Keys)
            {
                if (!data.TryGetValue(key, out object? value) || value is null)
                    continue;

                if (!DataLeft.TryGetValue(key, out List<float>? collections) || collections is null)
                {
                    collections = [];
                    DataLeft[key] = collections;
                }
                collections.Add(Convert.ToSingle(value));
            }

            foreach (string key in Right.Keys)
            {
                if (!data.TryGetValue(key, out object? value) || value is null)
                    continue;

                if (!DataRight.TryGetValue(key, out List<float>? collections) || collections is null)
                {
                    collections = [];
                    DataRight[key] = collections;
                }
                collections.Add(Convert.ToSingle(value));
            }
        }
    }

    public void ClearData()
    {
        this.Time.Clear();
        this.DataLeft.Clear();
        this.DataRight.Clear();
    }
}
