using C4IT.DataHistoryProvider;
using C4IT.Logging;
using C4IT.Security;
using C4IT.XML;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using static C4IT.Logging.cLogManager;

namespace C4IT_DataHistoryProvider
{
    public class cDataHistoryConfigCitrix : IConfigNodeValidation
    {
        public bool IsValid { get; private set; } = false;

        public string Domain { get; private set; } = "";

        public string TenantID { get; private set; } = String.Empty;

        public cCredential Credential { get; private set; } = null;
        public cDataHistoryScanTiming ScanTiming { get; private set; } = null;
        public cDataHistoryConfigCitrix(XmlElement XNode, Dictionary<string, cCredential> Credentials, cXmlParser Parser)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {

                var XNode2 = XNode.SelectSingleNode("Citrix");
                if (!(XNode2 is XmlElement XCitrix))
                {
                    return;
                }

                Parser.EnterElement("Citrix");

                try
                {
                    ScanTiming = new cDataHistoryScanTiming(XCitrix, TimeSpan.FromHours(24), TimeSpan.FromMinutes(10), Parser);
                    IsValid = true;
                }
                finally
                {
                    Parser.LeaveElement("Citrix");
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
            }
        }


        public class cDataHistoryCitrixTenant : IConfigNodeValidation
        {
            public bool IsValid { get; private set; } = false;

            public string Domain { get; private set; } = "";

            public string TenantID { get; private set; } = String.Empty;
            public string InstanceID { get; private set; } = String.Empty;

            public cCredential Credential { get; private set; } = null;

            internal cDataHistoryCitrixTenant(XmlElement XNode, Dictionary<string, cCredential> Credentials, cXmlParser Parser)
            {
                MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
                try
                {
                    Domain = cXmlParser.GetStringFromXmlAttribute(XNode, "Domain");
                    if (string.IsNullOrWhiteSpace(Domain))
                    {
                        Parser.AddInvalidAttribute(XNode, null, "Domain");
                        return;
                    }
                    TenantID = cXmlParser.GetStringFromXmlAttribute(XNode, "TenantID");
                    if (TenantID == String.Empty)
                    {
                        Parser.AddInvalidAttribute(XNode, Domain, "TenantID");
                        return;
                    }

                    InstanceID = cXmlParser.GetStringFromXmlAttribute(XNode, "InstanceID");
                    if (InstanceID == String.Empty)
                    {
                        Parser.AddInvalidAttribute(XNode, Domain, "InstanceID");
                        return;
                    }

                    var strCreds = cXmlParser.GetStringFromXmlAttribute(XNode, "Credential");
                    if (!string.IsNullOrWhiteSpace(strCreds))
                    {
                        if (Credentials.TryGetValue(strCreds, out var Cred))
                            Credential = Cred;
                    }

                    if (Credential == null)
                    {
                        Parser.AddInvalidAttribute(XNode, Domain, "Credential");
                        return;
                    }

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

            internal static Dictionary<string, cDataHistoryCitrixTenant> LoadFromXml(XmlElement XNode, Dictionary<string, cCredential> Credentials, cXmlParser Parser)
            {
                MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

                var RetVal = new Dictionary<string, cDataHistoryCitrixTenant>();

                try
                {
                    var XRoot = XNode.SelectSingleNode("Citrix");
                    if (XRoot == null)
                    {
                        return null;
                    }

                    Parser.EnterElement("Citrix");
                    try
                    {

                        var XList = XRoot.SelectNodes("Citrix-Tenant");
                        if (XList != null && XList.Count > 0)
                        {
                            Parser.EnterElement("Citrix-Tenant");
                            try
                            {
                                foreach (var Entry in XList)
                                {
                                    if (!(Entry is XmlElement XEntry))
                                        continue;

                                    var Node = new cDataHistoryCitrixTenant(XEntry, Credentials, Parser);
                                    if (Node.IsValid)
                                    {
                                        if (RetVal.ContainsKey(Node.Domain))
                                        {
                                            Parser.AddDuplicateName(XEntry, Node.Domain, LogLevels.Warning);
                                        }
                                        else
                                            RetVal.Add(Node.Domain, Node);
                                    }
                                    Parser.SelectElementNext();
                                }
                            }
                            finally
                            {
                                Parser.LeaveElement("Citrix-Tenant");
                            }
                        }
                    }
                    finally
                    {
                        Parser.LeaveElement("Citrix");
                    }
                }
                catch (Exception E)
                {
                    LogException(E);
                }
                finally
                {
                    if (CM != null) LogMethodEnd(CM);
                }

                return RetVal;
            }

        }


    }
}
