using System.Configuration;

namespace WSPOSInitiative.Common
{
    #region .configのセクション定義

    /// <summary>
    /// .configファイルにWS-POSのサービスホストプロセスで開始するサービスを記述するセクションを定義するクラス
    /// The class which defines the section which describes the service started in the service host process of WS-POS as a .config file 
    /// </summary>
    public class wsposInfoSection : ConfigurationSection
    {
        /// <summary>
        /// wsposInfoSectionのコンストラクタ
        /// Constructor
        /// </summary>
        public wsposInfoSection()
        {
        }

        /// <summary>
        /// サービスホストプロセスで開始するサービスのコレクションを記述するセクションの定義
        /// describes the collection of the service started in a service host process 
        /// </summary>
        [ConfigurationProperty("startServiceList")]
        public startServiceElementCollection startServiceCollection
        {
            get
            {
                startServiceElementCollection childCollection = (startServiceElementCollection)this["startServiceList"];
                return childCollection;
            }
        }
    }

    #endregion

    #region startService Element Definision

    /// <summary>
    /// サービスホストプロセスで開始するサービスを記述する要素クラスの定義
    /// The definition of the element class which describes the service started in a service host process 
    /// </summary>
    public class startServiceElement : ConfigurationElement
    {
        /// <summary>
        /// startServiceElementのコンストラクタ
        /// Constructor
        /// </summary>
        public startServiceElement()
        {
        }

        /// <summary>
        /// 開始するServiceTypeを指定する文字列 "名前空間.クラス名"
        /// The character string which specifies ServiceType to start. "Namespace.classname" 
        /// </summary>
        [ConfigurationProperty("type", IsRequired = true)]
        public string type
        {
            get { return (string)this["type"]; }
            set { this["type"] = value; }
        }

        /// <summary>
        /// 開始するServiceTypeが実装されているAssemblyFilenameを指定する文字列 拡張子付きで指定する
        /// The character string which specifies AssemblyFilename in which ServiceType to start is mounted. 
        /// It specifies with an extension. 
        /// </summary>
        [ConfigurationProperty("assembly", IsRequired = true)]
        public string assembly
        {
            get { return (string)this["assembly"]; }
            set { this["assembly"] = value; }
        }
    }

    #endregion

    #region Elements Collection Operation Definision 

    /// <summary>
    /// サービスホストプロセスで開始するサービスのコレクションを操作するためのクラス定義
    /// The class definition for operating the collection of the service started in a service host process 
    /// </summary>
    public class startServiceElementCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// ConfigurationElementCollection 内に設定する ConfigurationElement の名前を設定
        /// The name of ConfigurationElement set up in ConfigurationElementCollection is set up.
        /// </summary>
        public startServiceElementCollection()
        {
            this.AddElementName = "startService";
        }

        /// <summary>
        /// ConfigurationElementCollection 内の ConfigurationElement をインデックスから取得
        /// ConfigurationElement in ConfigurationElementCollection is acquired from an index. 
        /// </summary>
        /// <param name="index">取得するConfigurationElementのインデックス位置</param>
        /// <returns>取得したConfigurationElement</returns>
        public startServiceElement Get(int index)
        {
            return (startServiceElement)base.BaseGet(index);
        }

        /// <summary>
        /// ConfigurationElementCollection 内の ConfigurationElement をキーから取得
        /// ConfigurationElement in ConfigurationElementCollection is acquired from a key. 
        /// </summary>
        /// <param name="name">取得するConfigurationElementを特定するキー名</param>
        /// <returns>キーに該当するConfigurationElement</returns>
        public startServiceElement Get(string name)
        {
            return BaseGet(name) as startServiceElement;
        }

        /// <summary>
        /// ConfigurationElementCollection 内に指定したキーの ConfigurationElement があるか調査する
        /// It is investigated whether there is ConfigurationElement of the key specified in ConfigurationElementCollection.
        /// </summary>
        /// <param name="name">調査するConfigurationElementのキー名 | The key name of ConfigurationElement to investigate </param>
        /// <returns>調査結果:true=あり,false=なし</returns>
        public bool Contains(string name)
        {
            return BaseGet(name) != null;
        }

        /// <summary>
        /// 新しい ConfigurationElement を作成
        /// Create a new ConfigurationElement
        /// </summary>
        /// <returns>作成したConfigurationElement | created element</returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new startServiceElement();
        }

        /// <summary>
        /// 指定した構成要素の要素キーを取得
        /// Acquires the element key of the specified component
        /// </summary>
        /// <param name="element">キーを取得する要素 | The element which acquires a key  </param>
        /// <returns>ConfigurationElementのキー</returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            startServiceElement childElement = element as startServiceElement;
            return childElement.type;
        }
    }

    #endregion
}
