//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Xml;
using CommunityServer.Configuration;

namespace CommunityServer.Components
{
    public class ApplicationSet : IEnumerable
    {
        private Hashtable apps = new Hashtable();
        private static readonly ApplicationSet appSet = new ApplicationSet();

        public CSApplicationData this[ApplicationType app]
        {
            get{return apps[app] as CSApplicationData;}
            set{apps[app] = value;}
        }

        static ApplicationSet()
        {
            XmlNode node = CSConfiguration.GetConfig().GetConfigSection("CommunityServer/Applications");
            foreach(XmlNode n in node.ChildNodes)
            {
                if(n.NodeType != XmlNodeType.Comment)
                {
                    if(n.Name == "add")
                    {
                        string itemType = n.Attributes["type"].Value;
                        Type t = Type.GetType(itemType);
                        if(t == null)
                            throw new Exception(itemType);

                        
                        CSApplicationData app = Activator.CreateInstance(t) as CSApplicationData;
                        if(app != null)
                            appSet[app.ApplicationType] = app;
                    }

                }
            }
        }

        public static CSApplicationData Data(ApplicationType appType)
        {
            return ApplicationSet.Applications[appType];
        }


        public static ApplicationSet Applications
        {
            get{ return appSet;}
        }

        public IEnumerator GetEnumerator()
        {
			ArrayList a = new ArrayList(apps.Values);
			a.Sort();

            return a.GetEnumerator();
        }
    }
}