using SWRIS.Enums;
using System;
using System.Globalization;
using System.Windows.Data;

namespace SWRIS.Converters
{
    public class LimitSwitchNumberToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null) return false;

            if (value is int valueNumber && int.TryParse(parameter.ToString(), out int parameterNumber))
            {
                return valueNumber == parameterNumber;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return Binding.DoNothing;

            if (value is bool boolValue && boolValue)
            {
                if (parameter is int parameterNumber)
                {
                    return parameterNumber;
                }

                if (parameter is string parameterString && int.TryParse(parameterString, out int parsedNumber))
                {
                    return parsedNumber;
                }
            }

            return Binding.DoNothing;
        }
    }
}

