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

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

namespace WSPOSInitiative.Common
{
    class Program
    {
        static void Main(string[] args)
        {
            string executingAssemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
            Uri executingAssemblyDirectoryUri = new Uri(executingAssemblyDirectory);
            Directory.SetCurrentDirectory(executingAssemblyDirectoryUri.AbsolutePath);
            Console.WriteLine("Service Current Directory:" + System.Environment.CurrentDirectory);

            IList<ServiceHost> activatedServiceList = new List<ServiceHost>();

            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)
                        {
                            Console.WriteLine(string.Format("Can't Delete Temporary File:{0}\n{1}", file, ex));
                        }
                    }
                }
                else
                {
                    try
                    {
                        Directory.CreateDirectory(temporaryFolder);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(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);

                        Console.WriteLine(serviceType.FullName + " Ready.");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.WriteLine("Press Enter to End.");
            Console.ReadLine();

            foreach (ServiceHost serviceHost in activatedServiceList)
            {
                serviceHost.Close();
            }

        }
    }
}
