#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.Runtime.CompilerServices;
using dataMagus.Common;
using System.Collections;
using dataMagus.Common.Physical.Relational;
using dataMagus.Common.Physical.Objectoriented;
#endregion

[assembly: InternalsVisibleTo("dataMagus.Forms")]
namespace dataMagus.Common.PlugIn.Generator
{
	/// <summary>
	/// This singelton class manages all generators in the application.
	/// </summary>
    internal sealed class GeneratorManager
    {
		#region Members
		/// <summary>
		/// Singelton instance object.
		/// </summary>
        private static GeneratorManager _instance = null;
        
        /// <summary>
        /// Lock object for thread safe access.
        /// </summary>
        private static readonly object _padlock = new object();
		
        /// <summary>
        /// Holds all loaded generators implementing the IGenerator interface.
        /// </summary>
        private InterfaceLoadManager<IGenerator> _loadedPlugIns = new InterfaceLoadManager<IGenerator>();
		#endregion
		
		#region Constructors / Destructors
		/// <summary>
		/// Default constructor.
		/// </summary>
        private GeneratorManager()
        {
        }
		#endregion
		
		#region Properties
		/// <summary>
		/// Get the singelton instance of the object.
		/// </summary>
        public static GeneratorManager Instance
        {
            get
            {
                if (null == GeneratorManager._instance)
                {
                    lock (GeneratorManager._padlock)
                    {
                        if (null == GeneratorManager._instance)
                        {
                            GeneratorManager._instance = new GeneratorManager();
                        }
                    }
                }
                return GeneratorManager._instance;
            }
        }

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

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

		/// <summary>
		/// Find all loaded generators implementing a specified type.
		/// </summary>
		/// <param name="type">Type to search for.</param>
		/// <returns>List of found items.</returns>
		public List<IGenerator> FindType(Type type)
		{
			return this._loadedPlugIns.FindType(type);
		}

		/// <summary>
		/// Find all loaded generators implementing a specified interface by its FQN.
		/// </summary>
		/// <param name="fullname">Full qualified name of the interface.</param>
		/// <returns>List of found items.</returns>
        public List<IGenerator> FindInterfaceType(string fullname)
        {
            return this._loadedPlugIns.FindInterfaceType(fullname);
        }

        /// <summary>
		/// Find all loaded generators 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 generators will be returned.
		/// For the PhysicalRelationalSchema all IRelationalGenerator will be returned.
		/// For the PhysicalObjectorientedSchema all IObjectorientedGenerator will be returned.
		/// On all other types null will be returned.</returns>
        public List<IGenerator> FindInterfaceTypeForSchema(ISchema schema)
        {
            if (null == schema)
            {
                return this.FindInterfaceType(typeof(IGenerator));
            }
            else if (schema is PhysicalRelationalSchema)
            {
                return this.FindInterfaceType(typeof(IRelationalGenerator));
            }
            else if (schema is PhysicalObjectorientedSchema)
            {
                return this.FindInterfaceType(typeof(IObjectorientedGenerator));
            }
            return null;
        }

        /// <summary>
		/// Find all loaded generators implementing a specified type by its FQN.
        /// </summary>
		/// <param name="fullname">Full qualified name of the type.</param>
		/// <returns>List of found items.</returns>
        public List<IGenerator> FindType(string fullname)
        {
            return this._loadedPlugIns.FindType(fullname);
        }
		#endregion
    }
}
