using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace InABox.Wpf;

public class MultiFuncConverter(Func<object?[], object?> convert, Func<object?, object?[]?>? convertBack = null, IValueConverter? wrappedConverter = null) : IMultiValueConverter
{
    public Func<object?[], object?> ConvertFunc { get; set; } = convert;

    public Func<object?, object?[]?>? ConvertBackFunc { get; set; } = convertBack;

    public IValueConverter? wrappedConverter = wrappedConverter;

    public object? Convert(object?[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var val = ConvertFunc(values);
        if(wrappedConverter is not null)
        {
            return wrappedConverter.Convert(val, targetType, parameter, culture);
        }
        else
        {
            return val;
        }
    }

    public object?[]? ConvertBack(object? value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        if(wrappedConverter is not null)
        {
            value = wrappedConverter.Convert(value, typeof(object), parameter, culture);
        }
        if(ConvertBackFunc is not null)
        {
            return ConvertBackFunc(value);
        }
        return null;
    }
}
