using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
namespace FastGithub.Configuration
{
    /// 
    /// 监听器
    /// 
    public static class GlobalListener
    {
        private static readonly IPGlobalProperties global = IPGlobalProperties.GetIPGlobalProperties();
        private static readonly HashSet tcpListenPorts = GetListenPorts(global.GetActiveTcpListeners);
        private static readonly HashSet udpListenPorts = GetListenPorts(global.GetActiveUdpListeners);
        /// 
        /// ssh端口
        /// 
        public static int SshPort { get; } = GetAvailableTcpPort(22);
        /// 
        /// git端口
        /// 
        public static int GitPort { get; } = GetAvailableTcpPort(9418);
        /// 
        /// http端口
        /// 
        public static int HttpPort { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(80) : GetAvailableTcpPort(3880);
        /// 
        /// https端口
        /// 
        public static int HttpsPort { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(38443);
        /// 
        /// 获取已监听的端口
        /// 
        /// 
        /// 
        private static HashSet GetListenPorts(Func func)
        {
            var hashSet = new HashSet();
            try
            {
                foreach (var endpoint in func())
                {
                    hashSet.Add(endpoint.Port);
                }
            }
            catch (Exception)
            {
            }
            return hashSet;
        }
        /// 
        /// 是可以监听TCP
        /// 
        /// 
        /// 
        public static bool CanListenTcp(int port)
        {
            return tcpListenPorts.Contains(port) == false;
        }
        /// 
        /// 是可以监听UDP
        /// 
        /// 
        /// 
        public static bool CanListenUdp(int port)
        {
            return udpListenPorts.Contains(port) == false;
        }
        /// 
        /// 是可以监听TCP和Udp
        /// 
        /// 
        /// 
        public static bool CanListen(int port)
        {
            return CanListenTcp(port) && CanListenUdp(port);
        }
        /// 
        /// 获取可用的随机Tcp端口
        /// 
        ///  
        /// 
        public static int GetAvailableTcpPort(int minPort)
        {
            return GetAvailablePort(CanListenTcp, minPort);
        }
        /// 
        /// 获取可用的随机Udp端口
        /// 
        ///  
        /// 
        public static int GetAvailableUdpPort(int minPort)
        {
            return GetAvailablePort(CanListenUdp, minPort);
        }
        /// 
        /// 获取可用的随机端口
        /// 
        ///  
        /// 
        public static int GetAvailablePort(int minPort)
        {
            return GetAvailablePort(CanListen, minPort);
        }
        /// 
        /// 获取可用端口
        /// 
        /// 
        /// 
        /// 
        /// 
        private static int GetAvailablePort(Func canFunc, int minPort)
        {
            for (var port = minPort; port < IPEndPoint.MaxPort; port++)
            {
                if (canFunc(port) == true)
                {
                    return port;
                }
            }
            throw new FastGithubException("当前无可用的端口");
        }
    }
}