using System.Net;
using System.Security.Cryptography.X509Certificates;
using GenHTTP.Api.Infrastructure;
using GenHTTP.Engine;
using GenHTTP.Modules.Practices;

namespace InABox.API
{
    public static class RestListener
    {
        private static IServerHost? _host;

        public static X509Certificate2? Certificate { get; private set; }

        public static void Start()
        {
            _host?.Start();
        }

        public static void Stop()
        {
            _host?.Stop();
        }

        public static void Init(ushort port, X509Certificate2? cert)
        {
            _host = Host.Create();
            
            _host.Handler(new RestHandlerBuilder()).Defaults().Backlog(1024);
            
            Certificate = cert;
            RestService.IsHTTPS = cert != null;
            if (cert != null)
                _host?.Bind(IPAddress.Any, port, cert);
            else
                _host?.Bind(IPAddress.Any, port);
        }
    }
}