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

namespace UtmConvert {
    class HttpService {

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

        ServiceHost selfHost;
        string _status = "ok";

        public HttpService(string uri, string address) {
            Uri baseAddress = new Uri(uri);
            selfHost = new ServiceHost(typeof(UtmConvertService), baseAddress);

            try {
                selfHost.AddServiceEndpoint(typeof(IUtmConvertService),
                    new WSHttpBinding(), address);
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);
                selfHost.Open();
            } catch (CommunicationException ce) {
				if (updateErrorEvent != null)
					updateErrorEvent.Invoke(ce);
                _status = ce.Message;
                selfHost.Abort();
            }
        }

         ~HttpService() {
            selfHost.Close();
        }

        public HttpService() {
            Uri baseAddress = new Uri("http://localhost:8000/UtmConvert/UtmConvertService");
            selfHost = new ServiceHost(typeof(UtmConvertService), baseAddress);

            try {
                selfHost.AddServiceEndpoint(typeof(IUtmConvertService),
                    new WSHttpBinding(), "UtmConvertService");
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);
                selfHost.Open();
            } catch (CommunicationException ce) {
				if (updateErrorEvent != null)
					updateErrorEvent.Invoke(ce);
                _status = ce.Message;
                selfHost.Abort();
            }

        }
    }
}
