using DevExpress.CodeParser;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using SystemX.Net.Platform.Common.Util;
using static SystemX.Net.Platform.Common.Util.LogMessage;

namespace DataBaseConnection.Control
{
    public class DatabaseConnControl
    {
        /*public static string MainCatalogName = "CPX";
        public static string ShortTermCatalogName = "CPX";
        public static string LongTermCatalogName = "CPXLog";*/

        public static string MainCatalogName = "CPXV2";
        public static string ShortTermCatalogName = "CPXV2ShortTermLog";
        public static string LongTermCatalogName = "CPXV2LongTermLog";

        public const int CatalogConnPort = 1433;

        public const string CatalogConnUID = "Alis";

        public const string CatalogConnPW = "Kefico!@34";

        private static SqlConnection connMain = null;

        private static SqlConnection connShortTerm1 = null;
        private static SqlConnection connLongTerm1= null;

        private static SqlConnection connShortTerm2 = null;
        private static SqlConnection connLongTerm2 = null;

        public static SqlConnection GetMainConn()
        {
            return connMain;
        }

        public static SqlConnection GetShortTermConn1()
        {
            return connShortTerm1;
        }

        public static SqlConnection GetLongTermConn1()
        {
            return connLongTerm1;
        }

        public static SqlConnection GetShortTermConn2()
        {
            return connShortTerm2;
        }

        public static SqlConnection GetLongTermConn2()
        {
            return connLongTerm2;
        }

        public DatabaseConnControl()
        {
            //conn = new SqlConnection($"Data Source=200.200.200.48,1433; Initial Catalog={CatalogName}; User ID=Alis; Password=kefico; MultipleActiveResultSets=True;");        
            //conn = new SqlConnection($"Data Source=DESKTOP-5UQ1Q22;Initial Catalog={CatalogName};Integrated Security=True;");     
            //conn = new SqlConnection($"Data Source = 125.130.37.164,1433; Initial Catalog = {CatalogName}; User ID = Alis; Password = kefico; MultipleActiveResultSets=True;");
            //"Data Source = {IP},{Port}; Initial Catalog = {CatalogName}; User ID = Alis; Password = kefico; MultipleActiveResultSets=True;";
            //"Data Source = localhost; Initial Catalog = {CatalogName}; Integrated Security = SSPI; MultipleActiveResultSets=True;";
        }

        public static void DisconnectDB()
        {
            if (connMain != null)
            {
                if (connMain.State == System.Data.ConnectionState.Open)
                    connMain.Close();
            }
            //
            if (connShortTerm1 != null)
            {
                if (connShortTerm1.State == System.Data.ConnectionState.Open)
                    connShortTerm1.Close();
            }
            //
            if (connLongTerm1 != null)
            {
                if (connLongTerm1.State == System.Data.ConnectionState.Open)
                    connLongTerm1.Close();
            }
            //
            if (connShortTerm2 != null)
            {
                if (connShortTerm2.State == System.Data.ConnectionState.Open)
                    connShortTerm2.Close();
            }
            //
            if (connLongTerm2 != null)
            {
                if (connLongTerm2.State == System.Data.ConnectionState.Open)
                    connLongTerm2.Close();
            }
        }

        public static void ConnectTestListDB()
        {
            try
            {
                //conn = new SqlConnection($"Data Source=200.200.200.48,1433; Initial Catalog={CatalogName}; User ID=Alis; Password=kefico; MultipleActiveResultSets=True;");          
                //Data Source = {IP},{Port}; Initial Catalog = {SCHEMA}; User ID = {ID}; Password = {PW}; MultipleActiveResultSets=True;
                //conn = new SqlConnection($"Data Source=DESKTOP-5UQ1Q22;Initial Catalog={CatalogName};Integrated Security=True;");     
                //conn = new SqlConnection($"Data Source = 125.130.37.164,1433; Initial Catalog = {CatalogName}; User ID = Alis; Password = kefico; MultipleActiveResultSets=True;");            
                //conn = new SqlConnection($"Data Source = 127.0.0.1; Initial Catalog = Smart_Inhibitor_1th_ALIS; User ID = Alis; Password = kefico;"); //Integrated Security = SSPI;");

                //connTest = new OdbcConnection("Dsn=UIM_MacAddress; Uid=Alis; Pwd=Kefico!@34;");
                //connTest.Open();

                //OdbcTransaction odbcTransaction = connTest.BeginTransaction();
                //OdbcCommand DD = new OdbcCommand("SELECT TOP (1000) * FROM [UIM_MacAddress].[dbo].[HIST_Summary];", connTest, odbcTransaction);
                //OdbcDataReader ddd = DD.ExecuteReader();
                //int n1 = ddd.FieldCount;

                connMain = new SqlConnection($"Data Source=localhost; Initial Catalog={MainCatalogName}; Integrated Security = SSPI; MultipleActiveResultSets=True;");
                connMain.Open(); ;
            }
            catch (SqlException sqlEx)
            {
                MessageOutput.ConsoleWrite($"TestList DB Connection Error: {sqlEx.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);

                connMain = null;
            }
        }

        public static void ConnectDataDB1()
        {
            try
            {
                connShortTerm1 = new SqlConnection($"Data Source=localhost; Initial Catalog={ShortTermCatalogName}; Integrated Security = SSPI; MultipleActiveResultSets=True;"); // MultipleActiveResultSets=True;");
                connShortTerm1.Open();

                connLongTerm1 = new SqlConnection($"Data Source=localhost; Initial Catalog={LongTermCatalogName}; Integrated Security = SSPI; MultipleActiveResultSets=True;"); // MultipleActiveResultSets=True;");
                connLongTerm1.Open();
            }
            catch (SqlException sqlEx)
            {
                MessageOutput.ConsoleWrite($"Data DB Connection Error: {sqlEx.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);

                connShortTerm1 = null;
                connLongTerm1 = null;
            }
        }

        public static void ConnectDataDB2()
        {
            try
            {
                connShortTerm2 = new SqlConnection($"Data Source=localhost; Initial Catalog={ShortTermCatalogName}; Integrated Security = SSPI; MultipleActiveResultSets=True;"); // MultipleActiveResultSets=True;");
                connShortTerm2.Open();

                connLongTerm2 = new SqlConnection($"Data Source=localhost; Initial Catalog={LongTermCatalogName}; Integrated Security = SSPI; MultipleActiveResultSets=True;"); // MultipleActiveResultSets=True;");
                connLongTerm2.Open();
            }
            catch (SqlException sqlEx)
            {
                MessageOutput.ConsoleWrite($"Data DB Connection Error: {sqlEx.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);

                connShortTerm2 = null;
                connLongTerm2 = null;
            }
        }

        public static void ConnectTestListDB(string strGetConnIPAddress, int nGetConnPort = CatalogConnPort)
        {
            try
            {
                //conn = new SqlConnection("Data Source=200.200.200.48,1433; Initial Catalog=LSU_Trimming_4th_ALIS; User ID=Alis; Password=kefico; MultipleActiveResultSets=True;");          
                //Data Source = {IP},{Port}; Initial Catalog = {SCHEMA}; User ID = {ID}; Password = {PW}; MultipleActiveResultSets=True;
                //conn = new SqlConnection("Data Source=DESKTOP-5UQ1Q22;Initial Catalog=LSU_Trimming_4th_ALIS;Integrated Security=True;");     
                //conn = new SqlConnection("Data Source = 125.130.37.164,1433; Initial Catalog = LSU_Trimming_4th_ALIS; User ID = Alis; Password = kefico; MultipleActiveResultSets=True;");            
                //conn = new SqlConnection("Data Source = 127.0.0.1; Initial Catalog = Smart_Inhibitor_1th_ALIS; User ID = Alis; Password = kefico;"); //Integrated Security = SSPI;");

                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

                if (strGetConnIPAddress.Split(',').Length == 2)
                    builder.DataSource = strGetConnIPAddress;
                else
                    builder.DataSource = strGetConnIPAddress + "," + nGetConnPort.ToString();

                builder.UserID = "Alis";
                builder.Password = "Kefico!@34";
                builder.InitialCatalog = MainCatalogName;
                builder.MultipleActiveResultSets = true;

                connMain = new SqlConnection(builder.ConnectionString);
                connMain.Open();
                //
                if (strGetConnIPAddress.Split(',').Length == 2)
                    builder.DataSource = strGetConnIPAddress;
                else
                    builder.DataSource = strGetConnIPAddress + "," + nGetConnPort.ToString();
            }
            catch (SqlException sqlEx)
            {
                MessageOutput.ConsoleWrite($"TestList DB Connection Error: {sqlEx.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);

                connMain = null;
            }
        }

        public static void ConnectDataDB1(string strGetConnIPAddress, int nGetConnPort = CatalogConnPort)
        {
            try
            {
                //conn = new SqlConnection("Data Source=200.200.200.48,1433; Initial Catalog=LSU_Trimming_4th_ALIS; User ID=Alis; Password=kefico; MultipleActiveResultSets=True;");          
                //Data Source = {IP},{Port}; Initial Catalog = {SCHEMA}; User ID = {ID}; Password = {PW}; MultipleActiveResultSets=True;
                //conn = new SqlConnection("Data Source=DESKTOP-5UQ1Q22;Initial Catalog=LSU_Trimming_4th_ALIS;Integrated Security=True;");     
                //conn = new SqlConnection("Data Source = 125.130.37.164,1433; Initial Catalog = LSU_Trimming_4th_ALIS; User ID = Alis; Password = kefico; MultipleActiveResultSets=True;");            
                //conn = new SqlConnection("Data Source = 127.0.0.1; Initial Catalog = Smart_Inhibitor_1th_ALIS; User ID = Alis; Password = kefico;"); //Integrated Security = SSPI;");

                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

                if (strGetConnIPAddress.Split(',').Length == 2)
                    builder.DataSource = strGetConnIPAddress;
                else
                    builder.DataSource = strGetConnIPAddress + "," + nGetConnPort.ToString();

                builder.UserID = "Alis";
                builder.Password = "Kefico!@34"; //"Kefico!@34";
                //builder.Password = "KEFICO!@34";
                builder.InitialCatalog = ShortTermCatalogName;

                //conn = new SqlConnection(builder.ConnectionString);
                connShortTerm1 = new SqlConnection(builder.ConnectionString);
                //conn = new SqlConnection("Data Source = " + strGetConnIPAddress.ToString() + ",1433; Initial Catalog = Smart_Inhibitor_1th_ALIS; User ID=Alis; Password=KEFICO!@34;"); // MultipleActiveResultSets=True;");
                connShortTerm1.Open();
                //
                builder = new SqlConnectionStringBuilder();

                if (strGetConnIPAddress.Split(',').Length == 2)
                    builder.DataSource = strGetConnIPAddress;
                else
                    builder.DataSource = strGetConnIPAddress + "," + nGetConnPort.ToString(); //9000 50001 50002

                builder.UserID = "Alis";
                builder.Password = "Kefico!@34";
                builder.InitialCatalog = LongTermCatalogName;
                builder.MultipleActiveResultSets = true;

                connLongTerm1 = new SqlConnection(builder.ConnectionString);
                connLongTerm1.Open();
            }
            catch (SqlException sqlEx)
            {
                MessageOutput.ConsoleWrite($"Data DB Connection Error: {sqlEx.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);

                connShortTerm1 = null;
                connLongTerm1 = null;
            }
        }

        public static void ConnectDataDB2(string strGetConnIPAddress, int nGetConnPort = CatalogConnPort)
        {
            try
            {
                //conn = new SqlConnection("Data Source=200.200.200.48,1433; Initial Catalog=LSU_Trimming_4th_ALIS; User ID=Alis; Password=kefico; MultipleActiveResultSets=True;");          
                //Data Source = {IP},{Port}; Initial Catalog = {SCHEMA}; User ID = {ID}; Password = {PW}; MultipleActiveResultSets=True;
                //conn = new SqlConnection("Data Source=DESKTOP-5UQ1Q22;Initial Catalog=LSU_Trimming_4th_ALIS;Integrated Security=True;");     
                //conn = new SqlConnection("Data Source = 125.130.37.164,1433; Initial Catalog = LSU_Trimming_4th_ALIS; User ID = Alis; Password = kefico; MultipleActiveResultSets=True;");            
                //conn = new SqlConnection("Data Source = 127.0.0.1; Initial Catalog = Smart_Inhibitor_1th_ALIS; User ID = Alis; Password = kefico;"); //Integrated Security = SSPI;");

                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

                if (strGetConnIPAddress.Split(',').Length == 2)
                    builder.DataSource = strGetConnIPAddress;
                else
                    builder.DataSource = strGetConnIPAddress + "," + nGetConnPort.ToString();

                builder.UserID = "Alis";
                builder.Password = "Kefico!@34"; //"Kefico!@34";
                //builder.Password = "KEFICO!@34";
                builder.InitialCatalog = ShortTermCatalogName;

                //conn = new SqlConnection(builder.ConnectionString);
                connShortTerm2 = new SqlConnection(builder.ConnectionString);
                //conn = new SqlConnection("Data Source = " + strGetConnIPAddress.ToString() + ",1433; Initial Catalog = Smart_Inhibitor_1th_ALIS; User ID=Alis; Password=KEFICO!@34;"); // MultipleActiveResultSets=True;");
                connShortTerm2.Open();
                //
                builder = new SqlConnectionStringBuilder();

                if (strGetConnIPAddress.Split(',').Length == 2)
                    builder.DataSource = strGetConnIPAddress;
                else
                    builder.DataSource = strGetConnIPAddress + "," + nGetConnPort.ToString(); //9000 50001 50002

                builder.UserID = "Alis";
                builder.Password = "Kefico!@34";
                builder.InitialCatalog = LongTermCatalogName;
                builder.MultipleActiveResultSets = true;

                connLongTerm2 = new SqlConnection(builder.ConnectionString);
                connLongTerm2.Open();
            }
            catch (SqlException sqlEx)
            {
                MessageOutput.ConsoleWrite($"Data DB Connection Error: {sqlEx.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);

                connShortTerm2 = null;
                connLongTerm2 = null;
            }
        }
    }
}