using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.Xml;

namespace UtmConvert {
    class IpcService {

		public delegate void updateErrorEventHandler(Exception error);
		public event updateErrorEventHandler updateErrorEvent;

        ServiceHost serviceHost;
        string _status = "ok";
        ~IpcService() {
            serviceHost.Close();
        }

        public IpcService() {
            Uri baseAddress = new Uri("http://localhost/UtmConvert/UtmConvertService");
            string address = "net.pipe://localhost/UtmConvert/UtmConvertService";
            serviceHost = new ServiceHost(typeof(UtmConvertService), baseAddress);
            try {
                // Create a ServiceHost for the CalculatorService type and provide the base address.
                NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
                serviceHost.AddServiceEndpoint(typeof(IUtmConvertService), binding, address);

                // Add a mex endpoint
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.HttpGetUrl = new Uri("http://localhost:8001/UtmConvert");
                serviceHost.Description.Behaviors.Add(smb);
                long maxBufferPoolSize = binding.MaxBufferPoolSize;
                int maxBufferSize = binding.MaxBufferSize;
                int maxConnections = binding.MaxConnections;
                long maxReceivedMessageSize = binding.MaxReceivedMessageSize;
                NetNamedPipeSecurity security = binding.Security;
                string scheme = binding.Scheme;
                XmlDictionaryReaderQuotas readerQuotas = binding.ReaderQuotas;
                BindingElementCollection bCollection = binding.CreateBindingElements();
                HostNameComparisonMode hostNameComparisonMode = binding.HostNameComparisonMode;
                bool TransactionFlow = binding.TransactionFlow;
                TransactionProtocol transactionProtocol = binding.TransactionProtocol;
                EnvelopeVersion envelopeVersion = binding.EnvelopeVersion;
                TransferMode transferMode = binding.TransferMode;
                serviceHost.Open();
            } catch (CommunicationException ce) {
				if (updateErrorEvent != null)
					updateErrorEvent.Invoke(ce);
                _status = ce.Message;
                serviceHost.Abort();
            }
        }


    }
}
