using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.CodeDom.Compiler;
using Microsoft.Practices.RecipeFramework.Extensions.CommonHelpers;

namespace Microsoft.Practices.RecipeFramework.Extensions.Converters
{
    /// <summary>
    /// Converts strings to valid C# identifiers
    /// </summary>
    public class CSharpIdentifierConverter : StringConverter
    {
        private CodeDomProvider codeDomProvider;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:CSharpIdentifierConverter"/> class.
        /// </summary>
        public CSharpIdentifierConverter()
        {
            codeDomProvider = new CSharp.CSharpCodeProvider();
        }

        /// <summary>
        /// Converts the specified value object to a <see cref="T:System.String"></see> object.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
        /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"></see> to use.</param>
        /// <param name="value">The <see cref="T:System.Object"></see> to convert.</param>
        /// <returns>
        /// An <see cref="T:System.Object"></see> that represents the converted value.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The conversion could not be performed. </exception>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                return CodeDomHelper.ConvertToValidIdentifier((string)value);
            }

            return base.ConvertFrom(context, culture, value);
        }
    }
}
