using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;

namespace CustomTypeConverter
{
    class StreetAddressConverter : TypeConverter
    {
        // If the type is string, return true to indicate
        // that we can convert from a string
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string)) return true;
            return base.CanConvertFrom(context, sourceType);
        }

        // If the type is string, return true to indicate
        // that we can convert into a string.
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(String)) return true;
            return base.CanConvertTo(context, destinationType);
        }

        // Convert from a string.
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value.GetType() == typeof(string))
            {
                // Split the string separated by commas.
                string text = (string)(value);
                string[] fields = text.Split(new char[] { ',' });

                try
                {
                    StreetAddress address = new StreetAddress();
                    if (fields.Length == 0) return address;
                    address.Street = fields[0];
                    if (fields.Length == 1) return address;
                    address.City = fields[1];
                    if (fields.Length == 2) return address;
                    address.State = fields[2];
                    if (fields.Length == 3) return address;
                    address.Zip = fields[3];
                    return address;
                }
                catch
                {
                    string msg = "Cannot convert the string '" +
                        text + "' into a StreetAddress";
                    MessageBox.Show(msg);
                    throw new InvalidCastException(msg);
                }
            }
            else
            {
                return base.ConvertFrom(context, culture, value);
            }
        }

        // Convert the StreetAddress to a string.
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string)) return value.ToString();
            return base.ConvertTo(context, culture, value, destinationType);
        }

        // Return true to indicate that the object supports properties.
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        // Return a property description collection.
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(value);
        }
    }
}
