using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using skyscraper5.Skyscraper.Webserver.Metrics;

namespace skyscraper5.Skyscraper.Webserver
{
    internal class SkyscraperWebserver
    {
        private SkyscraperWebserver()
        {
            resources = new List<ISkyscraperWebserverResource>();
            resources.Add(MetricsResource.GetInstance());
        }

        private static SkyscraperWebserver _instance;

        public static SkyscraperWebserver GetInstance()
        {
            if (_instance == null)
            {
                _instance = new SkyscraperWebserver();
            }
            return _instance;
        }

        private bool _isListening;
        private HttpListener listener;
        private Thread listenerThread;
        private List<ISkyscraperWebserverResource> resources;
        private NotFoundFallbackResource notFoundFallback;

        public void Start()
        {
            if (_isListening)
            {
                throw new AlreadyListeningException();
            }

            _isListening = true;
            if (listener == null)
            {
                listener = new HttpListener();
                //netsh http add urlacl url="http://+:7597/" user=Jeder
                listener.Prefixes.Add("http://+:7597/");
            }

            listenerThread = new Thread(ListenerThreadFunction);
            listenerThread.Priority = ThreadPriority.Lowest;
            listenerThread.Name = "Webserver";
            listenerThread.Start();
        }

        private void ListenerThreadFunction()
        {
            listener.Start();
            while (_isListening)
            {
                HttpListenerContext listenerContext = listener.GetContext();
                Thread clientThread = new Thread(ClientThreadFunction);
                clientThread.Priority = ThreadPriority.Lowest;
                clientThread.Name = String.Format("HTTP Request for {0}", listenerContext.Request.RemoteEndPoint);
                clientThread.Start(listenerContext);
            }
        }

        private void ClientThreadFunction(object packedListenerContext)
        {
            HttpListenerContext listenerContext = packedListenerContext as HttpListenerContext;
            if (listenerContext == null)
                return;

            foreach (var skyscraperWebserverResource in resources)
            {
                if (skyscraperWebserverResource.CanHandle(listenerContext.Request))
                {
                    skyscraperWebserverResource.HandleRequest(listenerContext);
                    listenerContext.Response.Close();
                    return;
                }
            }

            if (notFoundFallback == null)
                notFoundFallback = new NotFoundFallbackResource();

            notFoundFallback.HandleRequest(listenerContext);
        }

        public bool IsListening
        {
            get => _isListening;
        }

        internal void RegisterResource(ISkyscraperWebserverResource resource)
        {
            if (resources.Contains(resource))
                return;

            resources.Add(resource);
        }
    }
}
