using System;
using System.Collections;


namespace DPLib
{
    /// <summary>
    /// A collection to handle ClientScript objects.  This is required to 
    /// include implementations of IndexOf and the indexer that will avoid
    /// the problem described in Microsoft KnowledgeBase article 823194.
    /// </summary>
    public class ScriptCollection : CollectionBase
    {
        private ClientScripts csContainer;
        public ScriptCollection(ClientScripts CsContainer){
            csContainer=CsContainer;
        }
        /// <summary>
        /// An indexer that returns a ClientScript object matching the 
        /// integer index or Name provided.
        /// </summary>
        public ClientScript this[object obj]
        {
            get
            {
                return (ClientScript) this.List[IndexOf(obj)]; 
            }
            set
            {
                this.List[IndexOf(obj)] = value;
            }
        }


        /// <summary>
        /// Adds a ClientScript to the end of the ScriptCollection.
        /// </summary>
        /// <param name="script">The ClientScript to add to the ScriptCollection.</param>
        public void Add(ClientScript script) 
        {
            //script.Site=script.Parent.Site;
            this.List.Add(script);
        }


        /// <summary>
        /// Inserts a ClientScript object at the specified index within the ScriptCollection.
        /// </summary>
        /// <param name="index">The location within the ScriptCollection to insert the ClientScript.</param>
        /// <param name="script">The ClientScript object to insert.</param>
        public void Insert(int index, ClientScript script) 
        {
            this.List.Insert(index, script);
        }


        /// <summary>
        /// Removes the script passed as a parameter from the ScriptCollection.
        /// </summary>
        /// <param name="script">The ClientScript script object to remove.</param>
        public void Remove(ClientScript script) 
        {
            List.Remove(script);
        }


        /// <summary>
        /// Removes the first script with name matching the provided keyName value.
        /// </summary>
        /// <param name="keyName">The name of the script(s) to remove.</param>
        public void Remove(string keyName)
        {
            IEnumerator enumerator = this.GetEnumerator();
            while(enumerator.MoveNext())
            {
                string name = ((ClientScript)enumerator.Current).ID; 
                if (string.Compare(keyName,name,true) == 0) 
                    this.Remove((ClientScript)enumerator.Current); 
                break;
            } 
        } 


        /// <summary>
        /// Determines whether the ScriptCollection contains the script passed
        /// in the script parameter.
        /// </summary>
        /// <param name="script">The name of the script to search for.</param>
        /// <returns>True if found, otherwise false.</returns>
        public bool Contains(ClientScript script) 
        {
            return this.List.Contains(script);
        }

        /// <summary>
        /// Gets the index of the script matching the integer index or string
        /// name provided.
        /// </summary>
        /// <param name="obj">Must be either an integer or string</param>
        /// <returns>True if a matching script is found in the collection, 
        /// otherwise false.
        /// </returns>
        public int IndexOf(object obj) 
        { 
            // Return the integer value if obj if it is within the range of 
            // the ScriptCollection count.  Otherwise, return -1 as defined
            // in the IList.IndexOf documentation.
            if (obj is int)
            {
                int index = (int)obj;
                if((index > -1) && (index < this.Count))
                {
                    return (int)obj;
                }
                else
                {
                    return -1;
                }
            }

            // If the object is a string, search for a ClientScript object with 
            // a matching name within the ScriptCollection
            if (obj is string)
            {
                for (int i = 0; i < List.Count; i++)
                    if (string.Compare((string)obj,((ClientScript)List[i]).ID,true)==0)
                        return i;
                return -1; 
            }
            else 
            {
                throw new ArgumentException("Only a string or an integer is permitted for the indexer.");
            } 
        } 

        /// <summary>
        /// Copies the ScriptCollection to a ClientScript array.
        /// </summary>
        /// <param name="array">The array to populate with the contents of the ScriptCollection.</param>
        /// <param name="index">Where in the array to begin copying at.</param>
        public void CopyTo(ClientScript[] array, int index) 
        { 
            List.CopyTo(array, index); 
        } 


        /// <summary>
        /// Determines if there is a script in the ScriptCollection with a name 
        /// matching the value provided.
        /// </summary>
        /// <param name="keyName">The name of the script to search for.</param>
        /// <returns>True if found, otherwise false.</returns>
        public bool Contains(string keyName)
        {
            IEnumerator enumerator = this.GetEnumerator();
            while(enumerator.MoveNext())
            {
                string name = ((ClientScript)enumerator.Current).ID; 
                if (string.Compare(keyName,name,true) == 0) 
                    return true; 
            }
            return false;
        }
    }
}