namespace HistoryView.Converters;

public class MutiValueToVisibility : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values is null || values.Length < 6)
            return Visibility.Collapsed;

        if (parameter is not string status)
            return Visibility.Collapsed;

        if (values[0] is not float caps)
            return Visibility.Collapsed;
        if (values[1] is not float floor)
            return Visibility.Collapsed;
        if (values[2] is not float current)
            return Visibility.Collapsed;
        if (values[3] is not Inhibit mode)
            return Visibility.Collapsed;
        if (values[4] is not ChannelMode channelMode)
            return Visibility.Collapsed;
        if (values[5] is not bool IsEnable || !IsEnable)
            return Visibility.Collapsed;
        if (values[6] is not bool IsConnected || !IsConnected)
            return Visibility.Collapsed;


        if (mode != Inhibit.Enable)
            return Visibility.Collapsed;

        if (channelMode == ChannelMode.Monitor)
            return Visibility.Collapsed;

        switch (channelMode)
        {
            case ChannelMode.Control:
                break;
            case ChannelMode.Monitor:
            case ChannelMode.UnUsed:
            default:
                return Visibility.Collapsed;
        }

        switch (status)
        {
            case "Over":
                if (current > caps)
                    return Visibility.Visible;

                break;

            case "Normal":
                if (current <= caps && current >= floor)
                    return Visibility.Visible;
                break;

            case "Under":
                if (current < floor)
                    return Visibility.Visible;
                break;

            default:
                break;
        }

        return Visibility.Collapsed;
    }

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

public class StatusToVisibility : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is not Inhibit mode)
            return Visibility.Visible;

        return mode switch
        {
            Inhibit.Enable => Visibility.Visible,
            Inhibit.Disable => Visibility.Visible,
            _ => Visibility.Hidden
        };
    }

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

public class TcBrockenToVisibility : IMultiValueConverter
{
    public object? Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length < 2)
            return Visibility.Collapsed;

        if (values[0] is not ChannelMode mode)
            return Visibility.Collapsed;

        if (values[1] is not TcBorken tcBorken)
            return Visibility.Collapsed;

        switch (mode)
        {
            case ChannelMode.Control:
            case ChannelMode.Monitor:
                break;
            case ChannelMode.UnUsed:
            default:
                return Visibility.Collapsed;
        }

        return tcBorken switch
        {
            TcBorken.Error => Visibility.Visible,
            TcBorken.Normal => Visibility.Collapsed,
            _ => Visibility.Collapsed
        };
    }

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

public class AutoTuntToVisibility : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length < 3)
            return Visibility.Collapsed;

        if (values[0] is not Inhibit inhibit)
            return Visibility.Collapsed;

        if (inhibit == Inhibit.Disable)
            return Visibility.Collapsed;

        if (values[1] is not AutoTuneStatus autoTune)
            return Visibility.Collapsed;

        if (values[2] is not ActiveTuneSet active)
            return Visibility.Collapsed;

        if (active == ActiveTuneSet.Running)
            return Visibility.Collapsed;

        return autoTune switch
        {
            AutoTuneStatus.Triggered => Visibility.Visible,
            AutoTuneStatus.Tuning => Visibility.Visible,
            AutoTuneStatus.Complete => Visibility.Visible,
            AutoTuneStatus.Timeout => Visibility.Visible,
            AutoTuneStatus.Overflow => Visibility.Visible,
            _ => (object)Visibility.Collapsed,
        };
    }

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

public class ChannelModeToVisbility : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is not ChannelMode channelMode)
            return Visibility.Collapsed;

        return channelMode switch
        {
            ChannelMode.Control => Visibility.Collapsed,
            ChannelMode.Monitor => Visibility.Visible,
            ChannelMode.UnUsed => Visibility.Visible,
            _ => Visibility.Collapsed
        };
    }

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

public class ChannelModeToChannelDisplay : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is not ChannelMode channelMode)
            return Visibility.Visible;

        return channelMode switch
        {
            ChannelMode.Control => Visibility.Visible,
            ChannelMode.Monitor => Visibility.Visible,
            ChannelMode.UnUsed => Visibility.Hidden,
            _ => Visibility.Visible
        };
    }

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

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

        if (parameter is not string light)
            light = "Bright";

        if (values[0] is not Inhibit mode)
            return default;
        if (values[1] is not Mini8Status mini8Status)
            return default;
        if (values[2] is not ChannelSuatus status)
            return default;
        if (values[3] is not bool isConnected)
            return default;



        if (!isConnected)
        {
            return light switch
            {
                "Bright" => App.Current.Resources["DisableColor"],
                "Light" => App.Current.Resources["LightDisableColor"],
                _ => default,
            };
        }

        if (values.Length >= 5)
        {
            if (values[4] is not AutoTuneStatus autoTuneStatus)
                return default;
            switch (autoTuneStatus)
            {
                case AutoTuneStatus.Ready:
                case AutoTuneStatus.Triggered:
                case AutoTuneStatus.Tuning:
                case AutoTuneStatus.Complete:
                case AutoTuneStatus.Aborted:
                default:
                    break;
                case AutoTuneStatus.Unavailable:
                case AutoTuneStatus.Timeout:
                case AutoTuneStatus.Overflow:
                    return App.Current.Resources["EmergencyColor"];
            }
        }

        switch (mini8Status)
        {
            case Mini8Status.Normal:
            case Mini8Status.Error:
            case Mini8Status.Warning:
                break;
            case Mini8Status.LostConnection:
            case Mini8Status.Unknow:
            case Mini8Status.Disabled:
            default:
                return light switch
                {
                    "Bright" => App.Current.Resources["DisableColor"],
                    "Light" => App.Current.Resources["LightDisableColor"],
                    _ => default,
                };
        }


        switch (mode)
        {
            case Inhibit.Enable:
                if (status == ChannelSuatus.Error)
                {
                    return light switch
                    {
                        "Bright" => App.Current.Resources["EmergencyColor"],
                        "Light" => App.Current.Resources["LightEmergencyColor"],
                        _ => default,
                    };
                }


                if (status == ChannelSuatus.Warning)
                {
                    return light switch
                    {
                        "Bright" => App.Current.Resources["WarningColor"],
                        "Light" => App.Current.Resources["LightWarningColor"],
                        _ => default,
                    };
                }

                return light switch
                {
                    "Bright" => App.Current.Resources["NormalColor"],
                    "Light" => App.Current.Resources["LightNormalColor"],
                    _ => default,
                };

            case Inhibit.Disable:
                return light switch
                {
                    "Bright" => App.Current.Resources["DisableColor"],
                    "Light" => App.Current.Resources["LightDisableColor"],
                    _ => default,
                };
            default:
                return default;
        }
    }

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