using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

using DevExpress.CodeParser;

using SystemX.Net.Platform.Common.Util;
using static SystemX.Net.Platform.Common.Util.LogMessage;

namespace DataBaseConnection.Control
{
    public class DatabaseConnControl
    {
        public static string CatalogName = "CPXV2";

        public const int CatalogConnPort = 1433;

        public const string CatalogConnUID = "Alis";

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

        //SqlConnection
        //OdbcConnection
        public static SqlConnection conn = null;

        //public static OdbcConnection connTest = null;

        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 (conn != null)
            {
                if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
            }
        }

        public static SqlConnection ConnectDB()
        {
            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;*/

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

                return null;
            }

            return conn;
        }

        public static SqlConnection ConnectDB(string strGetCatalogName)
        {
            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;*/

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

                return null;
            }

            return conn;
        }

        public static SqlConnection ConnectDB(string strGetConnIPAddress, 
            string strGetCatalogName,
            string strGetUID,
            string strGetPW,
            int nGetConnPort = CatalogConnPort) //IPAddress getConnIPAddress)
        {
            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(); //9000 50001 50002

                /*
                builder.UserID = "Alis";
                builder.Password = "Kefico!@34";
                builder.InitialCatalog = CatalogName;
                */

                builder.UserID = strGetUID;
                builder.Password = strGetPW;
                builder.InitialCatalog = strGetCatalogName;

                conn = new SqlConnection(builder.ConnectionString);

                //Kefico!@34
                //KEFICO!@34
                //conn = new SqlConnection("Data Source = " + getConnIPAddress.ToString() + ",1433; Initial Catalog = UIM_MacAddress; User ID=Alis; Password=Kefico!@34;"); // MultipleActiveResultSets=True;");
                //conn = new SqlConnection("Data Source = " + getConnIPAddress.ToString() + ",1433; Initial Catalog = Smart_Inhibitor_1th_ALIS; User ID=Alis; Password=KEFICO!@34;"); // MultipleActiveResultSets=True;");

                conn.Open();
            }
            catch (SqlException sqlEx)
            {
                MessageOutput.ConsoleWrite($"DB Connection Error: {sqlEx.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);

                return null;
            }

            return conn;
        }
    }
}