// ******************************************************************************************
// **                                                                                       **
// **      RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION                 **
// **      PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS         **
// **      AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT.DISCLOSURE TO                **
// **      UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO          **
// **      RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS              **
// **      CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS         **
// **      OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON        **
// **      COMPANY.                                                                         **
// **                                                                                       **
// **      THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.               **
// **      GOVERNMENT.                                                                      **
// **                                                                                       **
// **      UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.                                   **
// **                                                                                       **
// **      WARNING: THIS DOCUMENT CONTAINS TECHNICAL DATA AND / OR TECHNOLOGY WHOSE         **
// **      EXPORT OR DISCLOSURE TO NON-U.S.PERSONS, WHEREVER LOCATED, IS RESTRICTED         **
// **      BY THE INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) (22 C.F.R.SECTION        **
// **      120-130) OR THE EXPORT ADMINISTRATION REGULATIONS(EAR) (15 C.F.R.SECTION         **
// **      730-774). THIS DOCUMENT CANNOT BE EXPORTED(E.G., PROVIDED TO A SUPPLIER          **
// **      OUTSIDE OF THE UNITED STATES) OR DISCLOSED TO A NON-U.S.PERSON, WHEREVER         **
// **      LOCATED, UNTIL A FINAL JURISDICTION AND CLASSIFICATION DETERMINATION HAS         **
// **      BEEN COMPLETED AND APPROVED BY RAYTHEON, AND ANY REQUIRED U.S.GOVERNMENT         **
// **      APPROVALS HAVE BEEN OBTAINED. VIOLATIONS ARE SUBJECT TO SEVERE CRIMINAL          **
// **      PENALTIES.                                                                       **
// **                                                                                       **
// **      CAPITAL EQUIPMENT/SOFTWARE: THIS TECHNICAL DATA WAS DEVELOPED OR ACQUIRED        **
// **      EXCLUSIVELY AT CONTRACTOR EXPENSE AND IS INTENDED FOR USE ON MULTIPLE            **
// **      PROJECTS/PROGRAMS.                                                               **
// **                                                                                       **
// *******************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raytheon.Communication;
using Raytheon.Communication.Rpc;
using System.Globalization;

namespace Raytheon.Instruments
{
    /// <summary>
    /// Generic (and abstract) proxy factory for helping to do all of the work that needs to
    /// be done in proxy factories.  All that needs implementation is the GetSupportedInterfaces
    /// method, this will be up to the impl
    /// 
    /// What shall be done is a proxy factory for every interface should be declared in
    /// a dll.  Then a consumer would import via MEF all the factories and use them appropriately
    /// </summary>
    public abstract class InstrumentProxyFactory<T> : IInstrumentProxyFactory
        where T : class
    {
        #region Private Fields

        //as of right now these do not appear to need access to the implementing class
        private IUms _UmsHost;
        private IUmsClientFactory _UmsClientFactory;

        //Added a GetProxy method for access to a specific proxy
        private Dictionary<string, IUmsClient<T>> _Proxies;

        #endregion

        #region IInstrumentProxyFactory Members

        /// <summary>
        /// This must be called to initialize a factory with a media port to create the client
        /// </summary>
        /// <param name="umsHost">host for the client factory to use to create the proxy</param>
        /// <param name="umsClientFactory">client factory to use to create proxies</param>
        public virtual void Initialize(IUms umsHost, IUmsClientFactory umsClientFactory)
        {
            _Proxies = new Dictionary<string, IUmsClient<T>>();
            _UmsClientFactory = umsClientFactory;
            _UmsHost = umsHost;
        }

        /// <summary>
        /// This is the call any client rpc instrument manager would use to get the proxy
        /// it will return the specific instrument proxy which can be cast to a generic 
        /// IInstrument if needed, the only way to cast to a derived type is to return one
        /// in the first place since they are proxies
        /// </summary>
        /// <param name="name">instrument name</param>
        /// <returns>the Type proxy (not the IInstrument)</returns>
        public virtual IInstrument GetInstrument(string name)
        {
            return GetSpecificInstrument(name) as IInstrument;
        }

        /// <summary>
        /// Implementing class must provide the interfaces it supports
        /// Since this is purely an interface driven proxy the acutal implementation
        /// under the hood of the Interfaces matters not
        /// </summary>
        /// <returns>collection of interfaces it can create proxies to</returns>
        public abstract ICollection<string> GetSupportedInterfaces();

        #endregion

        #region Protected Methods

        /// <summary>
        /// returns the specific instruments proxy, this is a generic operation so put into abstract class
        /// </summary>
        /// <param name="name">instrument name</param>
        /// <returns>the specific instruments proxy</returns>
        protected virtual T GetSpecificInstrument(string name)
        {
            if (null == _Proxies)
            {
                string message = typeof(T).FullName + " Factory not initialized, proxy container not created";
                throw new InstrumentException(message);
            }

            if (!_Proxies.ContainsKey(name))
            {
                var proxy = CreateProxy(name);
                _Proxies.Add(name, proxy);
            }

            return _Proxies[name].Contract;
        }

        /// <summary>
        /// added to allow an implementing class to get access to proxies so that it may add properties or
        /// whatever else needs to be done to the objects
        /// </summary>
        /// <param name="proxyName">instrument name</param>
        /// <returns>the proxy to that specific instrument</returns>
        protected IUmsClient<T> GetProxy(string proxyName)
        {
            IUmsClient<T> proxy = null;
            _Proxies.TryGetValue(proxyName, out proxy);
            return proxy;
        }

        /// <summary>
        /// creates the proxy, this is a generic operation, so put into the abstract class
        /// </summary>
        /// <param name="proxyName">instrument name</param>
        /// <returns>the proxy to the given instrument</returns>
        protected virtual IUmsClient<T> CreateProxy(string proxyName)
        {
            if (null == _UmsClientFactory || null == _UmsHost)
            {
                string message = typeof(T).FullName + " Factory not initialized, ums information is null";
                throw new InstrumentException(message);
            }

            IUmsClient<T> proxy = _UmsClientFactory.GetClient<T>(_UmsHost);
            proxy.AddProperty("instName", proxyName);
            proxy.Open();

            return proxy;
        }

        #endregion
    }
}
