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

namespace OrderEntrySystem.Converters
{
    public class DecimalToStringConverter : IValueConverter
    {
        /// <summary>
        /// Store the last value.
        /// </summary>
        private string lastEnteredValue;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string result = this.lastEnteredValue ?? value.ToString();
            this.lastEnteredValue = null;
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            decimal decimalValue;
            string stringValue = value.ToString();
            if (decimal.TryParse(stringValue, out decimalValue))
            {
                this.lastEnteredValue = stringValue;
            }

            return decimalValue;
        }
    }
}
