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

using SystemX.Common;
using SystemX.Common.Serialization;
using static SystemX.Net.DB.LogProcess.XLogDBConnManager;

namespace SystemX.Net.DB.LogProcess
{
    public class CMainConnInfo
    {
        public string Type { get; set; }
        public string IP { get; set; }
        public string Port { get; set; }
        public string ID { get; set; }
        public string PW { get; set; }
        public string SCHEMA { get; set; }
        public string SUMMARY_TABLE { get; set; }
        public string VERSION_TABLE { get; set; }
        public string VRFY_TABLE { get; set; }
        public string MAC_ADDR { get; set; }
        public string SSPI { get; set; }

        public CMainConnInfo()
        {
            Type = string.Empty;
            IP = string.Empty;
            Port = string.Empty;
            ID = string.Empty;
            PW = string.Empty;
            SCHEMA = string.Empty;
            MAC_ADDR = string.Empty;
            SSPI = string.Empty;
        }
    }

    public class CShortTermConnInfo
    {
        public string Type { get; set; }
        public string IP { get; set; }
        public string Port { get; set; }
        public string ID { get; set; }
        public string PW { get; set; }
        public string SCHEMA { get; set; }
        //public string KEY_TABLE { get; set; }
        public string SUMMARY_TABLE { get; set; }
        public string LOG_TABLE { get; set; }
        public string MAC_ADDR { get; set; }
        public string SSPI { get; set; }

        public CShortTermConnInfo()
        {
            Type = string.Empty;
            IP = string.Empty;
            Port = string.Empty;
            ID = string.Empty;
            PW = string.Empty;
            SCHEMA = string.Empty;
            MAC_ADDR = string.Empty;
            SSPI = string.Empty;
        }
    }


    public class CLongTermConnInfo
    {
        public string Type { get; set; }
        public string IP { get; set; }
        public string Port { get; set; }
        public string ID { get; set; }
        public string PW { get; set; }
        public string SCHEMA { get; set; }
        //public string KEY_TABLE { get; set; }
        public string SUMMARY_TABLE { get; set; }
        public string LOG_TABLE { get; set; }
        public string MAC_ADDR { get; set; }
        public string SSPI { get; set; }

        public CLongTermConnInfo()
        {
            Type = string.Empty;
            IP = string.Empty;
            Port = string.Empty;
            ID = string.Empty;
            PW = string.Empty;
            SCHEMA = string.Empty;
            MAC_ADDR = string.Empty;
            SSPI = string.Empty;
        }
    }

    public class XDBConnInfo
    {
        public enum INFO_STRUCTURE
        {
            DBConnInfo,
            IP,
            Port,
            ID,
            PW,
            DB_CONN,
            MAC_ADDR,
            SSPI
        }

        public enum eVersion
        {
           CPX,
           CPXV2
        }

        public eVersion ConnVersion;

        public CMainConnInfo ConnMain;

        public CShortTermConnInfo ConnShortTerm;

        public CLongTermConnInfo ConnLongTerm;


        public XDBConnInfo()
        {
            ConnVersion = eVersion.CPX;

            ConnMain = new CMainConnInfo();
            ConnShortTerm = new CShortTermConnInfo();
            ConnLongTerm = new CLongTermConnInfo();
        }

        public bool ReadConfig(string strPath)
        {
            try
            {
                FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);

                XmlDocument xmldoc = new XmlDocument();
                XmlNodeList nodeList;

                Type InfoType = typeof(XDBConnInfo);

                xmldoc.Load(fs);

                nodeList = xmldoc.GetElementsByTagName(INFO_STRUCTURE.DBConnInfo.ToString());

                XmlNode topNode = nodeList.Item(0);

                foreach (XmlNode node in topNode.ChildNodes)
                {
                    string strNodeName = node.Name;

                    if (strNodeName.CompareTo("UseVersion") == 0)
                    {
                        if (Enum.TryParse<eVersion>(node.InnerText, out ConnVersion) == false)
                            ConnVersion = eVersion.CPX;
                    }

                    if (strNodeName.CompareTo("MainConnInfo") == 0)
                    {
                        if (node.HasChildNodes == false)
                            throw new Exception("[MainConnInfo] Has no entries.");
                        if (node.ChildNodes.Count != 11)
                            throw new Exception("[MainConnInfo] Entries do not match.");

                        ConnMain.Type = node.ChildNodes[0].InnerText;
                        ConnMain.IP = node.ChildNodes[1].InnerText;
                        ConnMain.Port = node.ChildNodes[2].InnerText;
                        ConnMain.ID = node.ChildNodes[3].InnerText;
                        ConnMain.PW = node.ChildNodes[4].InnerText;
                        ConnMain.SCHEMA = node.ChildNodes[5].InnerText;
                        ConnMain.SUMMARY_TABLE = node.ChildNodes[6].InnerText;
                        ConnMain.VERSION_TABLE = node.ChildNodes[7].InnerText;
                        ConnMain.VRFY_TABLE = node.ChildNodes[8].InnerText;
                        ConnMain.MAC_ADDR = node.ChildNodes[9].InnerText;
                        ConnMain.SSPI = node.ChildNodes[10].InnerText;
                    }
                    else if (strNodeName.CompareTo("ShortTermInfo") == 0)
                    {
                        if (node.HasChildNodes == false)
                            throw new Exception("[ShortTermInfo] Has no entries.");
                        if (node.ChildNodes.Count != 10)
                            throw new Exception("[ShortTermInfo] Entries do not match.");

                        ConnShortTerm.Type = node.ChildNodes[0].InnerText;
                        ConnShortTerm.IP = node.ChildNodes[1].InnerText;
                        ConnShortTerm.Port = node.ChildNodes[2].InnerText;
                        ConnShortTerm.ID = node.ChildNodes[3].InnerText;
                        ConnShortTerm.PW = node.ChildNodes[4].InnerText;
                        ConnShortTerm.SCHEMA = node.ChildNodes[5].InnerText;
                        //ConnShortTerm.KEY_TABLE = node.ChildNodes[6].InnerText;
                        ConnShortTerm.SUMMARY_TABLE = node.ChildNodes[6].InnerText;
                        ConnShortTerm.LOG_TABLE = node.ChildNodes[7].InnerText;
                        ConnShortTerm.MAC_ADDR = node.ChildNodes[8].InnerText;
                        ConnShortTerm.SSPI = node.ChildNodes[9].InnerText;
                    }
                    else if(strNodeName.CompareTo("LongTermInfo") == 0)
                    {
                        if (node.HasChildNodes == false)
                            throw new Exception("[LongTermInfo] Has no entries.");
                        if (node.ChildNodes.Count != 10)
                            throw new Exception("[LongTermInfo] Entries do not match.");

                        ConnLongTerm.Type = node.ChildNodes[0].InnerText;
                        ConnLongTerm.IP = node.ChildNodes[1].InnerText;
                        ConnLongTerm.Port = node.ChildNodes[2].InnerText;
                        ConnLongTerm.ID = node.ChildNodes[3].InnerText;
                        ConnLongTerm.PW = node.ChildNodes[4].InnerText;
                        ConnLongTerm.SCHEMA = node.ChildNodes[5].InnerText;
                        //ConnLongTerm.KEY_TABLE = node.ChildNodes[6].InnerText;
                        ConnLongTerm.SUMMARY_TABLE = node.ChildNodes[6].InnerText;
                        ConnLongTerm.LOG_TABLE = node.ChildNodes[7].InnerText;
                        ConnLongTerm.MAC_ADDR = node.ChildNodes[8].InnerText;
                        ConnLongTerm.SSPI = node.ChildNodes[9].InnerText;
                    }

                    /*
                    else
                    {
                        PropertyInfo propFound = InfoType.GetProperty(strNodeName);

                        if (propFound != null)
                            propFound.SetValue(this, node.InnerText);
                    }
                    */
                }

                fs.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"DB Connection Info. Reading Error : {ex.Message}");

                return false;
            }

            return true;
        }

        public string GetConnectionStatement(eDbType eType, eConnCategory eConn)
        {
            string strStatement = string.Empty;

            bool bSSPI = true;

            string IP_ADDRESS = string.Empty;
            string PORT_NUMBER = string.Empty;
            string CONN_SCHEMA = string.Empty;
            string IDENTIFIER = string.Empty;
            string PASSWORD = string.Empty;

            switch (eConn)
            {
                case eConnCategory.Main:
                    {
                        if (ConnMain.SSPI.IndexOf("False") >= 0) bSSPI = false;

                        IP_ADDRESS = ConnMain.IP;
                        PORT_NUMBER = ConnMain.Port;
                        CONN_SCHEMA = ConnMain.SCHEMA;
                        IDENTIFIER = ConnMain.ID;
                        PASSWORD = ConnMain.PW;
                    }
                    break;
                case eConnCategory.ShortTerm:
                    {
                        if (ConnShortTerm.SSPI.IndexOf("False") >= 0) bSSPI = false;

                        IP_ADDRESS = ConnShortTerm.IP;
                        PORT_NUMBER = ConnShortTerm.Port;
                        CONN_SCHEMA = ConnShortTerm.SCHEMA;
                        IDENTIFIER = ConnShortTerm.ID;
                        PASSWORD = ConnShortTerm.PW;
                    }
                    break;
                case eConnCategory.LongTerm:
                    {
                        if (ConnLongTerm.SSPI.IndexOf("False") >= 0) bSSPI = false;

                        IP_ADDRESS = ConnLongTerm.IP;
                        PORT_NUMBER = ConnLongTerm.Port;
                        CONN_SCHEMA = ConnLongTerm.SCHEMA;
                        IDENTIFIER = ConnLongTerm.ID;
                        PASSWORD = ConnLongTerm.PW;
                    }
                    break;
            }

            switch (eType)
            {
                case eDbType.MS_SQL:
                    if (bSSPI == false)
                        strStatement = $"Data Source = {IP_ADDRESS},{PORT_NUMBER}; Initial Catalog = {CONN_SCHEMA}; User ID = {IDENTIFIER}; Password = {PASSWORD}; MultipleActiveResultSets=True;";
                    else
                        strStatement = $"Data Source = localhost; Initial Catalog = {CONN_SCHEMA}; Integrated Security = SSPI; MultipleActiveResultSets=True;";
                    break;
                default:
                    break;
            }

            return strStatement;
        }
    }
}
