namespace HistoryView.Controls.Configs;

public partial class Mini8Comparision : UserControl
{
    public Mini8Comparision()
    {
        InitializeComponent();
        Comparision = [];
    }

    public TempConfigSubMini8 Source
    {
        get { return (TempConfigSubMini8)GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }

    public static readonly DependencyProperty SourceProperty =
        DependencyProperty.Register("Source", typeof(TempConfigSubMini8), typeof(Mini8Comparision), new PropertyMetadata(default, SourceChangedCallback));

    public TempConfigSubMini8 Target
    {
        get { return (TempConfigSubMini8)GetValue(TargetProperty); }
        set { SetValue(TargetProperty, value); }
    }

    public static readonly DependencyProperty TargetProperty =
        DependencyProperty.Register("Target", typeof(TempConfigSubMini8), typeof(Mini8Comparision), new PropertyMetadata(default, TargetChangedCallback));

    private static void SourceChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is not Mini8Comparision act)
            return;

        if (e.NewValue is not TempConfigSubMini8 config)
            return;

        if (config.ChannelConfig is null)
            return;

        foreach (var mini8 in config.ChannelConfig)
        {
            act.Comparision[mini8.Key] ??= new();
            act.Comparision[mini8.Key].Index = mini8.Key;
            act.Comparision[mini8.Key].Source = mini8.Value;
        }
    }
    private static void TargetChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is not Mini8Comparision act)
            return;

        if (e.NewValue is not TempConfigSubMini8 config)
            return;

        if (config.ChannelConfig is null)
            return;

        foreach (var mini8 in config.ChannelConfig)
        {
            act.Comparision[mini8.Key] ??= new();
            act.Comparision[mini8.Key].Index = mini8.Key;
            act.Comparision[mini8.Key].Target = mini8.Value;
        }
    }

    public ObservableDictionary<byte, Mini8COnfigComparisionData> Comparision
    {
        get { return (ObservableDictionary<byte, Mini8COnfigComparisionData>)GetValue(ComparisionProperty); }
        set { SetValue(ComparisionProperty, value); }
    }

    public static readonly DependencyProperty ComparisionProperty =
        DependencyProperty.Register("Comparision", typeof(ObservableDictionary<byte, Mini8COnfigComparisionData>), typeof(Mini8Comparision), new PropertyMetadata(default));
}

public partial class Mini8COnfigComparisionData : ObservableObject
{
    [ObservableProperty]
    private byte _Index;

    [ObservableProperty]
    private string? _FileName;

    [ObservableProperty]
    private TempConfigSubChannel? _Source;

    [ObservableProperty]
    private TempConfigSubChannel? _Target;
}