using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Web.Administration;

using C4IT.Logging;

using Microsoft.Win32;

using static C4IT.Logging.cLogManager;

namespace C4IT_DataHistoryProvider_Base
{
    public static class IisConfigHelper
    {
        public static cIssConfiguration CheckIISConfiguration()
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                var _res = new cIssConfiguration();
                CheckIISComponents(_res);
                CheckIISConfiuration(_res);

                return _res;
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
            }


            return null;
        }

        private static void CheckIISComponents(cIssConfiguration _issConfig)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                var _regBase = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                var _regKey = _regBase.OpenSubKey("SOFTWARE\\Microsoft\\InetStp\\Components", false);
                if (_regKey != null)
                {
                    var _regVal1 = _regKey.GetValue("WindowsAuthentication")?.ToString();
                    var _regVal2 = _regKey.GetValue("WindowsAuthenticationBinaries")?.ToString();
                    _issConfig.IsIssWindowsAuthenticationInstalled = _regVal1 == "1" && _regVal2 == "1";

                    var _regVal3 = _regKey.GetValue("WebSockets")?.ToString();
                    _issConfig.IsWebSocketInstalled = _regVal3 == "1";
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
            }
        }

        private static void CheckIISConfiuration(cIssConfiguration _issConfig)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                var _serverManager = new ServerManager();

                var _siteCollections = _serverManager.Sites;
                foreach (var _site in _siteCollections)
                {
                    var _applications = _site.Applications;
                    foreach (var _application in _applications)
                    {
                        if (_application.Path.ToLowerInvariant() != "/f4sdcockpit")
                            continue;

                        var _appConfig = _application.GetWebConfiguration();
                        var _cfgWindowsAuthentication = _appConfig.GetSection("system.webServer/security/authentication/windowsAuthentication");
                        if (_cfgWindowsAuthentication != null)
                        {
                            var _attr = _cfgWindowsAuthentication.Attributes.FirstOrDefault(v => v.Name == "enabled");
                            if (_attr != null)
                            {
                                if (_attr.Value is bool _valWindowsAuthentication)
                                    _issConfig.IsIssWindowsAuthenticationEnabled |= _valWindowsAuthentication;
                            }
                        }
                    }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
            }
        }

    }

    public class cIssConfiguration
    {
        public bool IsIssWindowsAuthenticationInstalled = false;
        public bool IsWebSocketInstalled = false;
        public bool IsIssWindowsAuthenticationEnabled = false;
    }
}
