#region License
// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
#endregion
using System.Collections;
using System.ComponentModel;
using System.Data.Common;
using System.Reflection;
namespace MiniSqlQuery.PlugIns.ConnectionStringsManager
{
    /// The generic connection string builder.
    [DefaultMember("Item"), DefaultProperty("ConnectionString")]
    public class GenericConnectionStringBuilder : DbConnectionStringBuilder
    {
        /// The _properties.
        private Hashtable _properties;
        /// Initializes a new instance of the  class.
        public GenericConnectionStringBuilder()
        {
            Initialize(null);
        }
        /// Initializes a new instance of the  class.
        /// The connection string.
        public GenericConnectionStringBuilder(string connectionString)
        {
            Initialize(connectionString);
        }
        /// The try get value.
        /// The keyword.
        /// The value.
        /// The try get value.
        public override bool TryGetValue(string keyword, out object value)
        {
            bool success = base.TryGetValue(keyword, out value);
            if (_properties.ContainsKey(keyword))
            {
                PropertyDescriptor descriptor = _properties[keyword] as PropertyDescriptor;
                if (descriptor == null)
                {
                    return success;
                }
                if (success)
                {
                    value = TypeDescriptor.GetConverter(descriptor.PropertyType).ConvertFrom(value);
                    return success;
                }
                DefaultValueAttribute attribute = descriptor.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
                if (attribute != null)
                {
                    value = attribute.Value;
                    success = true;
                }
            }
            return success;
        }
        /// The initialize.
        /// The cnn string.
        private void Initialize(string cnnString)
        {
            _properties = new Hashtable();
            this.GetProperties(_properties);
            if (!string.IsNullOrEmpty(cnnString))
            {
                ConnectionString = cnnString;
            }
        }
    }
}