namespace ProximaAnalizer.Helpers;

public partial class DisplayHistroyHelper1(int key, int value, string[] keywords) : ObservableObject
{
    [ObservableProperty]
    private ObservableDictionary<string, object> _Data = [];

    public Dictionary<string, string> Keys { get; } = [];

    public void UpdateData(List<dynamic>? data)
    {
        if (data is null)
            return;

        if (data.FirstOrDefault() is not IDictionary<string, object> source)
            return;

        App.Current.Dispatcher?.Invoke(() =>
        {
            foreach (var data in source)
            {
                string[] keys = data.Key.Split('.');
                if (keys.Length <= value)
                    continue;

                if (keywords.Any(t => keys[key].StartsWith(t)))
                {
                    this.Keys[keys[value]] = data.Key;
                    this.Data[keys[value]] = data.Value;
                }
            }
        });
    }


    public void Clear()
    {
        this.Data ??= [];
        this.Data.Clear();
    }
}


public partial class DisplayHistroyHelper2(int key, int value, string keyWord) : ObservableObject
{
    [ObservableProperty]
    private ObservableDictionary<string, ObservableDictionary<string, object>> _Data = [];

    public Dictionary<string, Dictionary<string, string>> Keys { get; } = [];

    public void UpdateData(List<dynamic>? data)
    {
        if (data is null)
            return;

        if (data.FirstOrDefault() is not IDictionary<string, object> source)
            return;

        App.Current.Dispatcher?.Invoke(() =>
        {
            foreach (var data in source)
            {
                string[] keys = data.Key.Split('.');

                if (keys.Length <= value)
                    continue;

                if (!keys[1].StartsWith(keyWord))
                    continue;

                if (!this.Data.TryGetValue(keys[key], out var output) || output is null)
                {
                    output = [];
                    this.Data[keys[key]] = output;
                    this.Keys[keys[key]] = [];
                }

                output[keys[value]] = data.Value;
                this.Keys[keys[key]][keys[value]] = data.Key;
            }
        });
    }

    public void Clear()
    {
        this.Data ??= [];
        this.Data.Clear();
    }
}

public partial class DisplayHistoryHelperSensor : ObservableObject
{
    [ObservableProperty]
    private ObservableDictionary<string, object> _Data = [];

    public Dictionary<string, string> Keys { get; } = [];

    public void UpdateData(List<dynamic>? data)
    {
        if (data is null)
            return;

        if (data.FirstOrDefault() is not IDictionary<string, object> source)
            return;

    }
}