using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using JtkLocalServer.Managers;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace JtkLocalServer {
    public class Program {
        public static void Main(string[] args) {
            foreach (var arg in args) {
                var serializedOptions = Encoding.UTF8.GetString(Convert.FromBase64String(arg));
                var options = serializedOptions.Split('\n');
                if (options.Length == 4) {
                    StartArgumentsManager.SetValues(options[0], options[1], options[2], options[3] == "1");
                }
            }
            
            BuildWebHost(new string[0]/*args*/).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseKestrel(options => {
                    var configuration = (IConfiguration)options.ApplicationServices.GetService(typeof(IConfiguration));
                    var host = configuration.GetSection("RafHost").Get<Host>();
                    foreach (var endpointKvp in host.Endpoints) {
                        var endpointName = endpointKvp.Key;
                        var endpoint = endpointKvp.Value;
                        if (!endpoint.IsEnabled) {
                            continue;
                        }

                        var address = IPAddress.Parse(endpoint.Address);
                        options.Listen(address, endpoint.Port, opt => {
                            if (endpoint.Certificate != null) {
                                switch (endpoint.Certificate.Source) {
                                    case "File":
                                        opt.UseHttps(endpoint.Certificate.Path, endpoint.Certificate.Password);
                                        break;
                                    default:
                                        throw new NotImplementedException($"The source {endpoint.Certificate.Source} is not yet implemented");
                                }
                                //opt.UseConnectionLogging();
                            }
                        });
                        options.UseSystemd();   //?
                    }
                })
                .UseStartup<Startup>()
                .Build();
    }

    public class Host {
        public Dictionary<string, Endpoint> Endpoints { get; set; }
    }
    public class Endpoint {
        public bool IsEnabled { get; set; }
        public string Address { get; set; }
        public int Port { get; set; }
        public Certificate Certificate { get; set; }
    }
    public class Certificate {
        public string Source { get; set; }
        public string Path { get; set; }
        public string Password { get; set; }
    }
}
