namespace ConfigFileManager.Controls;

public partial class HarewareCompare : UserControl
{
    public HarewareCompare()
    {
        InitializeComponent();
    }

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

    public static readonly DependencyProperty SourceProperty =
        DependencyProperty.Register("Source", typeof(DeviceInfo_VM), typeof(HarewareCompare), new PropertyMetadata(default, PropertyChangedCallback));

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

    public static readonly DependencyProperty TargetProperty =
        DependencyProperty.Register("Target", typeof(DeviceInfo_VM), typeof(HarewareCompare), new PropertyMetadata(default, PropertyChangedCallback));

    private static readonly ImageSource normal = (ImageSource)Application.Current.Resources["Icon_finish"];
    private static readonly ImageSource warning = (ImageSource)Application.Current.Resources["Icon_Error"];
    private static readonly ImageSource fatal = (ImageSource)Application.Current.Resources["Icon_Fatal"];
    private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is not HarewareCompare compare)
            return;

        if (compare.Source is null || compare.Target is null)
            return;

        if (compare.Source.DeviceModel != compare.Target.DeviceModel)
        {
            compare.DeviceModelImage.Source = fatal;
            compare.DeviceSubModelImage.Source = fatal;
            compare.SoftwareVersionImage.Source = fatal;
            compare.Migrate.IsEnabled = false;
            compare.MigrateRecipe.IsEnabled = false;
            return;
        }

        if (compare.Source.DeviceSubModel != compare.Target.DeviceSubModel)
        {
            compare.DeviceModelImage.Source = normal;
            compare.DeviceSubModelImage.Source = fatal;
            compare.SoftwareVersionImage.Source = fatal;
            compare.Migrate.IsEnabled = false;
            compare.MigrateRecipe.IsEnabled = false;
            return;
        }

        compare.Migrate.IsEnabled = true;
        compare.MigrateRecipe.IsEnabled = true;
        if (compare.Source.SoftwareVersion != compare.Target.SoftwareVersion)
        {
            compare.DeviceModelImage.Source = normal;
            compare.DeviceSubModelImage.Source = normal;
            compare.SoftwareVersionImage.Source = warning;
            return;
        }
        compare.DeviceModelImage.Source = normal;
        compare.DeviceSubModelImage.Source = normal;
        compare.SoftwareVersionImage.Source = normal;

    }




    public ICommand MigrateCommand
    {
        get { return (ICommand)GetValue(MigrateCommandProperty); }
        set { SetValue(MigrateCommandProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MigrateCommand.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MigrateCommandProperty =
        DependencyProperty.Register("MigrateCommand", typeof(ICommand), typeof(HarewareCompare), new PropertyMetadata(default));



    public ICommand MigrateRecipeCommand
    {
        get { return (ICommand)GetValue(MigrateRecipeCommandProperty); }
        set { SetValue(MigrateRecipeCommandProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MigrateRecipeCommand.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MigrateRecipeCommandProperty =
        DependencyProperty.Register("MigrateRecipeCommand", typeof(ICommand), typeof(HarewareCompare), new PropertyMetadata(default));




}
