#region Copyright and GPL
/*************************************************************************
 * dataMagus - The open source database learning software
 * Copyright (C) 2007 by all authors
 * Authors: Monika Schwitter (monsy@gmx.ch)
 *          Jürg Zgraggen (j.zgraggen@ejaz.net)
 *          HSLU (Hochschule Luzern - Technik+Architektur)
 * 
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
#endregion
#region Using
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Reflection;
using System.Collections;
using dataMagus.Common;
using dataMagus.Common.Logical;
using dataMagus.Common.Physical.Relational;
using dataMagus.Common.Physical.Objectoriented;
#endregion

namespace dataMagus.Common.PlugIn.Notation
{
	/// <summary>
	/// This singelton class manages all notations in the application.
	/// </summary>
    internal sealed class NotationManager
    {
		#region Members
		/// <summary>
		/// Singelton instance object.
		/// </summary>
        private static NotationManager _instance = null;
        
        /// <summary>
        /// Lock object for thread safe access.
        /// </summary>
        private static readonly object _padlock = new object();

        /// <summary>
        /// Holds all loaded schemas implementing the INotation interface.
        /// </summary>
        private InterfaceLoadManager<INotation> _loadedPlugIns = new InterfaceLoadManager<INotation>();
		#endregion
		
		#region Constructors / Destructors
		/// <summary>
		/// Default constructor.
		/// </summary>
        private NotationManager()
        {
        }
		#endregion
		
		#region Properties
		/// <summary>
		/// Get the singelton instance of the object.
		/// </summary>
        public static NotationManager Instance
        {
            get
            {
                if (null == NotationManager._instance)
                {
                    lock (NotationManager._padlock)
                    {
                        if (null == NotationManager._instance)
                        {
                            NotationManager._instance = new NotationManager();
                        }
                    }
                }
                return NotationManager._instance;
            }
        }

        /// <summary>
        /// Count all loaded notations.
        /// </summary>
        public int CountLoadedPlugIns
        {
            get { return this._loadedPlugIns.LoadedInterfaces.Count; }
        }
        #endregion
		
		#region Methodes
		/// <summary>
		/// Get all loaded notations.
		/// </summary>
		/// <returns>Enumerator to parse.</returns>
        public IEnumerator GetLoadedPlugIns()
        {
            return this._loadedPlugIns.LoadedInterfaces.GetEnumerator();
        }

        /// <summary>
        /// Unload all notations.
        /// </summary>
		public void UnLoadPlugIns()
		{
			this._loadedPlugIns.LoadedInterfaces.Clear();
		}
		
		/// <summary>
		/// Load all notations implementing INotation from a specified path.
		/// </summary>
		/// <param name="path">Path where the notations are located.</param>
		public void LoadPlugIns(string path)
		{
			this._loadedPlugIns.LoadInterfaces(path, "*.dll", true);
		}
		
		/// <summary>
		/// Find all loaded notations implementing a specified interface.
		/// </summary>
		/// <param name="type">Interface to search for.</param>
		/// <returns>List of found items.</returns>
		public List<INotation> FindInterfaceType(Type type)
		{
			return this._loadedPlugIns.FindInterfaceType(type);
		}

		/// <summary>
		/// Find all loaded notations implementing a specified interface.
		/// </summary>
		/// <param name="fullname">Full qualified name of the interface to search for.</param>
		/// <returns>List of found items.</returns>
        public List<INotation> FindInterfaceType(string fullname)
        {
            return this._loadedPlugIns.FindInterfaceType(fullname);
        }

        /// <summary>
		/// Find all loaded notations depending on the schema type.
        /// </summary>
        /// <param name="schemaType">Type of the schema to find the generators for.</param>
		/// <returns>List of found items.
		/// If the schema is null, all notations will be returned.
		/// For the LogicalSchema all ILogicalNotation will be returned.
		/// For the PhysicalRelationalSchema all IPhysicalRelationalNotation will be returned.
		/// For the PhysicalObjectorientedSchema all IPhysicalObjectorientedNotation will be returned.
		/// On all other types null will be returned.</returns>
        public List<INotation> FindInterfaceTypeForSchema(Type schemaType)
        {
            if (null == schemaType)
            {
                return this.FindInterfaceType(typeof(INotation));
            }
            else if (schemaType == typeof(LogicalSchema))
            {
                return this.FindInterfaceType(typeof(ILogicalNotation));
            }
            else if (schemaType == typeof(PhysicalRelationalSchema))
            {
                return this.FindInterfaceType(typeof(IPhysicalRelationalNotation));
            }
            else if (schemaType == typeof(PhysicalObjectorientedSchema))
            {
                return this.FindInterfaceType(typeof(IPhysicalObjectorientedNotation));
            }
            return null;
        }

        /// <summary>
		/// Find all loaded notations depending on the schema type.
        /// </summary>
        /// <param name="schema">Schema to find the generators for.</param>
		/// <returns>List of found items.
		/// If the schema is null, all notations will be returned.
		/// For the LogicalSchema all ILogicalNotation will be returned.
		/// For the PhysicalRelationalSchema all IPhysicalRelationalNotation will be returned.
		/// For the PhysicalObjectorientedSchema all IPhysicalObjectorientedNotation will be returned.
		/// On all other types null will be returned.</returns>
        public List<INotation> FindInterfaceTypeForSchema(ISchema schema)
        {
            if (null == schema)
            {
                return this.FindInterfaceType(typeof(INotation));
            }
            return this.FindInterfaceTypeForSchema(schema.GetType());
        }

        /// <summary>
		/// Find all loaded notations implementing a specified type by its FQN.
        /// </summary>
		/// <param name="type">Type to search for.</param>
		/// <returns>List of found items.</returns>
		public List<INotation> FindType(Type type)
		{
			return this._loadedPlugIns.FindType(type);
		}
		#endregion
    }
}
