using System;
using System.ComponentModel;
using System.Globalization;

namespace LeafWeb.Core.Utility
{
	// http://stackoverflow.com/a/19022931/99492
	public class BoolTypeConverter : TypeConverter
	{
		public static void Register()
		{
			TypeDescriptor.AddAttributes(typeof(Boolean),
				new TypeConverterAttribute(typeof(BoolTypeConverter)));
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			if (destinationType == typeof(bool))
			{
				return true;
			}
			return base.CanConvertTo(context, destinationType);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if (value is bool)
				return value;
			if (value is string)
			{
				var s = value as string;
				if (string.IsNullOrEmpty(s))
					return false;
				switch (s.Trim().ToUpper())
				{
					case "TRUE":
					case "YES":
					case "1":
					case "-1":
						return true;

					default:
						return false;
				}
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}
	}
}