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

using System;
using System.IO;
using System.Collections.Generic;
using System.ServiceProcess;
using System.ServiceModel;
using System.Configuration;
using System.Reflection;

namespace WSPOSInitiative.Common
{
    public partial class WSPOSHostService : ServiceBase
    {
        private IList<ServiceHost> _activatedServiceList = new List<ServiceHost>();
        private ILog _log = new LogServiceEventLogImpl();


        public WSPOSHostService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                string temporaryFolder = UriToTempFileHelper.GetTemporaryDirectory();

                if (Directory.Exists(temporaryFolder))
                {
                    var files = Directory.GetFiles(temporaryFolder);

                    foreach (var file in files)
                    {
                        try
                        {
                            File.Delete(file);
                        }
                        catch (Exception ex)
                        {
                            _log.LogWriteLineMessage(MethodBase.GetCurrentMethod(), string.Format("Can't Delete Temporary File:{0}\n{1}", file, ex));
                        }
                    }
                }
                else
                {
                    try
                    {
                        Directory.CreateDirectory(temporaryFolder);
                    }
                    catch (Exception ex)
                    {
                        _log.LogWriteLineMessage(MethodBase.GetCurrentMethod(), string.Format("Can't Create Temporary File Directory:{0}\n{1}", temporaryFolder, ex));
                    }
                }

                Configuration Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
                ConfigurationSection wsposInfo = Config.GetSection("wsposInfoSection");
                startServiceElementCollection startServiceList = ((wsposInfoSection)wsposInfo).startServiceCollection;
                foreach (startServiceElement startService in startServiceList)
                {
                    Assembly serviceAssembly = null;
                    Type serviceType = null;
                    try
                    {
                        serviceAssembly = Assembly.LoadFrom(startService.assembly);
                        serviceType = serviceAssembly.GetType(startService.type);
                        ServiceHost serviceHost = new ServiceHost(serviceType);
                        serviceHost.Open();
                        _activatedServiceList.Add(serviceHost);

                        _log.LogWriteLineMessage(MethodBase.GetCurrentMethod(), string.Format("Start WS-POS Service Provider : Assembly = {0}, Type = {1}", serviceAssembly.FullName, serviceType.FullName));
                    }
                    catch (Exception ex)
                    {
                        _log.LogWriteLineException(MethodBase.GetCurrentMethod(), ex, string.Format("Start failed WS-POS Service Provider : Assembly = {0}, Type = {1}", serviceAssembly != null ? serviceAssembly.FullName : "(null)", serviceType != null ? serviceType.FullName : "(null)"));

                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.LogWriteLineException(MethodBase.GetCurrentMethod(), ex, "Start failed WS-POS Service Provider.");

                throw;
            }
        }

        protected override void OnStop()
        {
            try
            {
                foreach (ServiceHost serviceHost in _activatedServiceList)
                {
                    serviceHost.Close();
                }
            }
            catch (Exception ex)
            {
                _log.LogWriteLineException(MethodBase.GetCurrentMethod(), ex);
                throw;
            }
        }
    }
}
