using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;

namespace EasyDev.Database
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public sealed class EasyDbParameterCollection : IEnumerable<EasyDbParameter>
    {
        private Dictionary<string, EasyDbParameter> m_List;

        /// <summary>
        /// Initializes a new instance of the <see cref="EasyDbParameterCollection" /> class.
        /// </summary>
        public EasyDbParameterCollection()
        {
            m_List = new Dictionary<string, EasyDbParameter>();
        }

        /// <summary>
        /// Adds the specified p.
        /// </summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns></returns>
        /// <column name="p">The p.</column>
        public EasyDbParameter Add(EasyDbParameter parameter)
        {
            m_List.Add(parameter.ParameterName, parameter);
            return parameter;
        }

        /// <summary>
        /// Removes the specified parameter name.
        /// </summary>
        /// <param name="parameterName">Name of the parameter.</param>
        /// <returns></returns>
        public bool Remove(string parameterName)
        {
            return m_List.Remove(parameterName);
        }

        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <column>The count.</column>
        public int Count
        {
            get
            {
                return m_List.Count;
            }
        }

        /// <summary>
        /// Determines whether [contains] [the specified name].
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>
        ///   <c>true</c> if [contains] [the specified name]; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(string name)
        {
            return m_List.ContainsKey(name);
        }

        /// <summary>
        /// Gets the <see cref="EasyDbParameter" /> with the specified name.
        /// </summary>
        /// <value>
        /// The <see cref="EasyDbParameter" />.
        /// </value>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public EasyDbParameter this[string name]
        {
            get
            {
                return Contains(name) ? m_List[name] : null;
            }
        }

        /// <summary>
        /// Gets the <see cref="EasyDbParameter" /> at the specified index.
        /// </summary>
        /// <value>
        /// The <see cref="EasyDbParameter" />.
        /// </value>
        /// <param name="index">The index.</param>
        /// <returns></returns>
        public EasyDbParameter this[int index]
        {
            get
            {
                return m_List.Values.ElementAtOrDefault(index);
            }
        }

        #region IEnumerable<DbDataParameter> Members

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        IEnumerator<EasyDbParameter> IEnumerable<EasyDbParameter>.GetEnumerator()
        {
            return m_List.Values.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            IEnumerable<EasyDbParameter> enumerable = this;
            return enumerable.GetEnumerator();
        }

        #endregion
    }
}
