using System;
using System.Globalization;
using System.Windows.Data;

namespace MECF.Framework.UI.Core.Converters
{
    public class DoubleToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";

            if(value is double dbl)
                return dbl.ToString(CultureInfo.InvariantCulture);

            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value == null)
                return double.NaN;

            if (double.TryParse(value.ToString(), out var dbl))
                return dbl;

            return double.NaN;
        }
    }
}
