// WS-POS version 1.3 参照実装
// 
// 実装 version 0.6 

using System;
using System.Reflection;
using System.Configuration;
using System.Collections.Generic;

namespace WSPOSInitiative.Common
{
    /// <summary>
    /// セッションコンテキストのインスタンスを生成するファクトリ
    /// The factory which generates the instance of a session context 
    /// </summary>
    class SessionContextFactory
    {
        /// <summary>
        /// セッションコンテキストのインスタンスを生成します
        /// Generates the instance of a session context. 
        /// </summary>
        /// <param name="sessionID">セッションID | Session ID</param>
        /// <param name="device">POSのデバイス | POS Device</param>
        /// <param name="consumerEventPort">双方向通信時のコンシューマイベントポートのチャネル | The channel of the consumer event port at the time of bi-directional communication</param>
        /// <param name="devicename">POSfor.NET,OpenPOSのデバイス名 | Device Name of POS for .NET or OpenPOS </param>
        /// <param name="log">ログインスタンス | log instance </param>
        /// <returns>セッションコンテキスト | Session Context</returns>
        internal static ISessionContext CreateSessionContextInstance(string sessionID, Microsoft.PointOfService.PosDevice device, object consumerEventPort, ILog log, string devicename)
        {
            if (StringArrayContainsHelper.Contains(ConfigurationManager.AppSettings.AllKeys, "CommonBehavior_SessionContextImpl") == false)
            {
                return new SessionContextDefaultImpl(sessionID, device, consumerEventPort, log, devicename);
            }

            string[] contextLoadString = ConfigurationManager.AppSettings["CommonBehavior_SessionContextImpl"].Split(',');

            Assembly sessionContextAssembly = Assembly.LoadFrom(contextLoadString[0]);

            Type sessionContextType = sessionContextAssembly.GetType(contextLoadString[1]);

            object[] constructorArgs = new object[5];
            constructorArgs[0] = sessionID;
            constructorArgs[1] = device;
            constructorArgs[2] = consumerEventPort;
            constructorArgs[3] = log;
            constructorArgs[4] = devicename;

            ISessionContext sessionContext = (ISessionContext)Activator.CreateInstance(sessionContextType, constructorArgs);

            return sessionContext;
        }
    }
}
