using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel;
using Csla.Properties;
namespace Csla
{
  /// 
  /// This is the base class from which readonly name/value
  /// collections should be derived.
  /// 
  /// Type of the key values.
  /// Type of the values.
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
  [Serializable()]
  public abstract class NameValueListBase : 
    Core.ReadOnlyBindingList.NameValuePair>, 
    ICloneable, Core.IBusinessObject
  {
    #region Core Implementation
    /// 
    /// Returns the value corresponding to the
    /// specified key.
    /// 
    /// Key value for which to retrieve a value.
    public V Value(K key)
    {
      foreach (NameValuePair item in this)
        if (item.Key.Equals(key))
          return item.Value;
      return default(V);
    }
    /// 
    /// Returns the key corresponding to the
    /// first occurance of the specified value
    /// in the list.
    /// 
    /// Value for which to retrieve the key.
    public K Key(V value)
    {
      foreach (NameValuePair item in this)
        if (item.Value.Equals(value))
          return item.Key;
      return default(K);
    }
    /// 
    /// Gets a value indicating whether the list contains the
    /// specified key.
    /// 
    /// Key value for which to search.
    public bool ContainsKey(K key)
    {
      foreach (NameValuePair item in this)
        if (item.Key.Equals(key))
          return true;
      return false;
    }
    /// 
    /// Gets a value indicating whether the list contains the
    /// specified value.
    /// 
    /// Value for which to search.
    public bool ContainsValue(V value)
    {
      foreach (NameValuePair item in this)
        if (item.Value.Equals(value))
          return true;
      return false;
    }
    #endregion
    #region Constructors
    /// 
    /// Creates an instance of the object.
    /// 
    protected NameValueListBase()
    {
      Initialize();
    }
    #endregion
    #region Initialize
    /// 
    /// Override this method to set up event handlers so user
    /// code in a partial class can respond to events raised by
    /// generated code.
    /// 
    protected virtual void Initialize()
    { /* allows subclass to initialize events before any other activity occurs */ }
    #endregion
    #region NameValuePair class
    /// 
    /// Contains a key and value pair.
    /// 
    [Serializable()]
    public class NameValuePair
    {
      private K _key;
      private V _value;
      /// 
      /// The Key or Name value.
      /// 
      public K Key
      {
        get { return _key; }
      }
      /// 
      /// The Value corresponding to the key/name.
      /// 
      public V Value
      {
        get { return _value; }
      }
      /// 
      /// Creates an instance of the object.
      /// 
      /// The key.
      /// The value.
      public NameValuePair(K key, V value)
      {
        _key = key;
        _value = value;
      }
    }
    #endregion
    #region ICloneable
    object ICloneable.Clone()
    {
      return GetClone();
    }
    /// 
    /// Creates a clone of the object.
    /// 
    /// A new object containing the exact data of the original object.
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    protected virtual object GetClone()
    {
      return Core.ObjectCloner.Clone(this);
    }
    /// 
    /// Creates a clone of the object.
    /// 
    public NameValueListBase Clone()
    {
      return (NameValueListBase)GetClone();
    }
    #endregion
    #region Criteria
    /// 
    /// Default Criteria for retrieving simple
    /// name/value lists.
    /// 
    /// 
    /// This criteria merely specifies the type of
    /// collection to be retrieved. That type information
    /// is used by the DataPortal to create the correct
    /// type of collection object during data retrieval.
    /// 
    [Serializable()]
    protected class Criteria : CriteriaBase
    {
      /// 
      /// Creates an instance of the object.
      /// 
      /// 
      /// The  of the business
      /// collection class.
      /// 
      public Criteria(Type collectionType)
        : base(collectionType)
      { }
    }
    #endregion
    #region Data Access
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "criteria")]
    private void DataPortal_Create(object criteria)
    {
      throw new NotSupportedException(Resources.CreateNotSupportedException);
    }
    /// 
    /// Override this method to allow retrieval of an existing business
    /// object based on data in the database.
    /// 
    /// An object containing criteria values to identify the object.
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", MessageId = "Member")]
    protected virtual void DataPortal_Fetch(object criteria)
    {
      throw new NotSupportedException(Resources.FetchNotSupportedException);
    }
    private void DataPortal_Update()
    {
      throw new NotSupportedException(Resources.UpdateNotSupportedException);
    }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "criteria")]
    private void DataPortal_Delete(object criteria)
    {
      throw new NotSupportedException(Resources.DeleteNotSupportedException);
    }
    /// 
    /// Called by the server-side DataPortal prior to calling the 
    /// requested DataPortal_XYZ method.
    /// 
    /// The DataPortalContext object passed to the DataPortal.
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", MessageId = "Member")]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    protected virtual void DataPortal_OnDataPortalInvoke(DataPortalEventArgs e)
    {
    }
    /// 
    /// Called by the server-side DataPortal after calling the 
    /// requested DataPortal_XYZ method.
    /// 
    /// The DataPortalContext object passed to the DataPortal.
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", MessageId = "Member")]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    protected virtual void DataPortal_OnDataPortalInvokeComplete(DataPortalEventArgs e)
    {
    }
    /// 
    /// Called by the server-side DataPortal if an exception
    /// occurs during data access.
    /// 
    /// The DataPortalContext object passed to the DataPortal.
    /// The Exception thrown during data access.
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", MessageId = "Member")]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    protected virtual void DataPortal_OnDataPortalException(DataPortalEventArgs e, Exception ex)
    {
    }
    #endregion
  }
}