using Newtonsoft.Json.Linq;

namespace HistoryView.Converters;

public class Mini8StatusToBrush : IValueConverter
{
    public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is not Mini8Status status)
            return default;

        return parameter switch
        {
            "Light" => status switch
            {
                Mini8Status.Error => App.Current.Resources["LightEmergencyColor"],
                Mini8Status.Warning => App.Current.Resources["LightWarningColor"],
                Mini8Status.LostConnection => App.Current.Resources["LightWarningColor"],
                Mini8Status.Normal => App.Current.Resources["LightNormalColor"],
                Mini8Status.Disabled => App.Current.Resources["LightDisableColor"],
                _ => App.Current.Resources["LightDisableColor"],
            },

            _ => status switch
            {
                Mini8Status.Error => App.Current.Resources["EmergencyColor"],
                Mini8Status.Warning => App.Current.Resources["WarningColor"],
                Mini8Status.LostConnection => App.Current.Resources["WarningColor"],
                Mini8Status.Normal => App.Current.Resources["NormalColor"],
                Mini8Status.Disabled => App.Current.Resources["DisableColor"],
                _ => App.Current.Resources["DisableColor"],
            },
        };
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public class Mini8StatusToBrushMulti : IMultiValueConverter
{
    public object? Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values is null || values.Length < 2)
            return default;

        if (values[0] is not Mini8Status status)
            return default;

        if (values[1] is not bool isConnected)
            return default;

        if (status==Mini8Status.Disabled)
        {
            return parameter switch
            {
                "Light" => App.Current.Resources["LightDisableColor"],
                _ => App.Current.Resources["DisableColor"],
            };
        }

        if (!isConnected)
        {
            return parameter switch
            {
                "Light" => App.Current.Resources["LightWarningColor"],
                _ => App.Current.Resources["WarningColor"],
            };
        }

        return parameter switch
        {
            "Light" => status switch
            {
                Mini8Status.Error => App.Current.Resources["LightEmergencyColor"],
                Mini8Status.Warning => App.Current.Resources["LightWarningColor"],
                Mini8Status.LostConnection => App.Current.Resources["LightWarningColor"],
                Mini8Status.Normal => App.Current.Resources["LightNormalColor"],
                Mini8Status.Disabled => App.Current.Resources["LightDisableColor"],
                _ => App.Current.Resources["LightDisableColor"],
            },

            _ => status switch
            {
                Mini8Status.Error => App.Current.Resources["EmergencyColor"],
                Mini8Status.Warning => App.Current.Resources["WarningColor"],
                Mini8Status.LostConnection => App.Current.Resources["WarningColor"],
                Mini8Status.Normal => App.Current.Resources["NormalColor"],
                Mini8Status.Disabled => App.Current.Resources["DisableColor"],
                _ => App.Current.Resources["DisableColor"],
            },
        };
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}