using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HttpServer.HttpModules;
using System.IO;
using System.Net;
using EMDServer.Common;
using System.Diagnostics;

namespace EMDWebserver
{
    public class Webserver
    {
        #region Class variables
        // ----------------------------------------
        private HttpServer.HttpServer server;
        // ----------------------------------------
        #endregion

        #region Public methods
        // ----------------------------------------
        /// <summary>
        /// Creates a new instance of the class.
        /// </summary>
        public Webserver()
        {
            this.EmdConnection = new EmdWebInterface();

            //Debug.WriteLine("WEBSERVER - Instantiated!");
        }

        /// <summary>
        /// Initializes the HTTP server and starts the webserver plugin on port 80.
        /// </summary>
        public void startServer()
        {
            server = new HttpServer.HttpServer();
            server.Add((HttpModule)EmdConnection);

            // Filemodul
            FileModule fileModule = new FileModule("/", Path.Combine(Environment.CurrentDirectory, "http"));
            fileModule.AddDefaultMimeTypes();
            server.Add(fileModule);

            // and start the server.
            server.Start(IPAddress.Any, 80);

            //Debug.WriteLine("WEBSERVER - HTTPServer initialized and listening on port 80!");
        }

        /// <summary>
        /// Stops the webserver and exits the webserver plugin.
        /// </summary>
        public void stopServer()
        {
            if (server != null)
            {
                server.Stop();

                //Debug.WriteLine("WEBSERVER - HTTPServer stopped!");
            }
        }
        // ----------------------------------------
        #endregion

        #region Properties
        // ----------------------------------------
        public iEMDConnection EmdConnection { get; set; }
        // ----------------------------------------
        #endregion
    }
}
