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

namespace BeWo.Converter
{
    public class ObjectVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool lVisibleIfNull = false;

            if (parameter != null || parameter is string)
            {
                bool.TryParse((string)parameter, out lVisibleIfNull);
            }

            if (value is string)
            {
                if (lVisibleIfNull)
                {
                    return !string.IsNullOrEmpty((string)value) ? Visibility.Collapsed : Visibility.Visible;
                }
                else
                {
                    return string.IsNullOrEmpty((string)value) ? Visibility.Collapsed : Visibility.Visible;
                }
            }

            if (lVisibleIfNull)
            {
                return value != null ? Visibility.Collapsed : Visibility.Visible;
            }

            return value == null ? Visibility.Collapsed : Visibility.Visible;
        }

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