using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars;
using SystemX.Product.ALIS.UI.View;
using static SystemX.Product.ALIS.UI.View.ViewCfg;
using SystemX.Net.Platform.Common.Util;
using DataBaseConnection.Control;
using System.Data.SqlClient;
using SystemX.Product.ALIS.UI.Subs;
using System.Diagnostics;
using static SystemX.Net.Platform.Common.Util.LogMessage;
using SystemX.Product.ALIS.Interface;
using System.Net;
using static SystemX.Product.CP.TRA.MainForm;

namespace SystemX.Product.CP.TRA
{
    public partial class MainForm
    {
        /// <summary>
        /// IDataBaseController
        /// </summary>
        /// <returns></returns>
        public SqlConnection GetMainConn()
        {
            return DatabaseConnControl.GetMainConn();
        }

        public SqlConnection GetShortTermConn1()
        {
            return DatabaseConnControl.GetShortTermConn1();
        }

        public SqlConnection GetLongTermConn1()
        {
            return DatabaseConnControl.GetLongTermConn1();
        }

        public SqlConnection GetShortTermConn2()
        {
            return DatabaseConnControl.GetShortTermConn2();
        }

        public SqlConnection GetLongTermConn2()
        {
            return DatabaseConnControl.GetLongTermConn2();
        }

        //IDataControl
        /*
        object objSQLControl = new object();

        public UserInfo GetLoginInfo()
        {
            return CurrentUser;
        }

        private DataTable QuerySQL(string strQueryText)
        {
            lock (objSQLControl)
            {
                SqlDataReader sqlReader = null;
                DataTable dt = new DataTable();

                try
                {
                    SQLCmd.CommandText = strQueryText;
                    sqlReader = SQLCmd.ExecuteReader();

                    if (sqlReader.HasRows != false)
                        dt.Load(sqlReader);
                }
                catch (Exception ex)
                {
                    MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>> ") + @"Query process fail. [SystemX.Product.ALIS.UI : IDataControl.getDuplicateCheck]\r\n" + ex.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG);
                }
                finally
                {
                    if (sqlReader != null)
                        sqlReader.Close();
                }

                if (Commons.isHasRow(dt))
                    return dt;
                else
                    return null;
            }
        }

        public DataTable GetTable(string strQueryText)
        {
            return QuerySQL(strQueryText);
        }

        /// <summary>
        /// IDataController - GetTable
        /// </summary>
        /// <param name="tblName"></param>
        /// <returns></returns>
        public DataTable GetTable(eDbTableList tblName)
        {
            string strQueryText = string.Empty;
            
            strQueryText = $"SELECT * FROM {tblName.ToString()} ORDER BY No ASC;";

            if (tblName == eDbTableList.PROD_TestList)
            {
                strQueryText = $"SELECT X.No, " +
                    $"X.ProdNo_P AS 'P_ProdNo', " +
                    $"X.TestType, " +
                    $"X.FileName, " +
                    $"X.Version AS 'FileVersion', " +
                    $"X.ProdCode, " +
                    $"X.RegDT, " +
                    $"X.RegUser, " + 
                    $"X.UpdateDT, " +
                    $"X.UpdateUser, " +
                    $"Y.No AS 'GroupNo', " +
                    $"Y.GroupName, " +
                    $"Y.ModelName, " +
                    $"Y.Comment, " +
                    $"X.Description, " +
                    $"X.TestListData FROM {tblName.ToString()} AS X INNER JOIN PROD_Group AS Y ON X.GroupNo = Y.No ORDER BY No ASC;";
            }
            else if (tblName == eDbTableList.PROD_Release)
            {
                strQueryText = $"SELECT X.No, " +
                    $"X.ProdNo_C AS 'C_ProdNo', " +
                    $"Y.No AS 'TestCodeNo', " +
                    $"Y.TestCode, " +
                    $"Z.No AS 'TestListNo', " +
                    $"Z.ProdNo_P AS 'P_ProdNo', " +
                    $"Z.TestType, " +
                    $"Z.Version AS 'FileVersion', " +
                    $"Z.ProdCode, " +
                    $"X.Config, " +
                    $"X.RegDT, " +
                    $"X.RegUser, " +
                    $"X.RegUserComment " +
                    $"FROM {tblName.ToString()} AS X INNER JOIN STAT_TestCode AS Y ON X.TestCodeNo = Y.No INNER JOIN PROD_TestList AS Z ON X.TestListNo = Z.No ORDER BY No ASC;";
            }

            return QuerySQL(strQueryText);
        }

        public DataTable GetTable(eDbTableList tblName, string strColName, string strParameter)
        {
            string strQueryText = $"SELECT * FROM {tblName.ToString()} WHERE {strColName} = {strParameter} ORDER BY No ASC;";

            return QuerySQL(strQueryText);
        }

        public string GetServerDateTimeString()
        {
            string strGetDateTime = string.Empty;

            string strQueryText = "SELECT CONVERT(datetime2, GETDATE(), 121) AS CURRENT_DATETIME;";

            DataTable getDt =  QuerySQL(strQueryText);

            if (Commons.isHasRow(getDt))
                strGetDateTime = getDt.Rows[0].ItemArray[0].ToString();

            return strGetDateTime;
        }

        public DateTime GetServerDateTime()
        {
            string strGetDateTime = string.Empty;

            string strQueryText = "SELECT CONVERT(datetime2, GETDATE(), 121) AS CURRENT_DATETIME;";

            DataTable getDt = QuerySQL(strQueryText);

            if (Commons.isHasRow(getDt))
                strGetDateTime = getDt.Rows[0].ItemArray[0].ToString();

            DateTime dtSetDateTime = new DateTime();
            DateTime.TryParse(strGetDateTime, out dtSetDateTime);

            return dtSetDateTime;
        }

        public string SetConvertDateTime(string strGetConvertText)
        {
            string strQueryConvertText = string.Empty;

            if (strGetConvertText.IndexOf("AM") >= 0 || strGetConvertText.IndexOf("PM") >= 0 ||
               strGetConvertText.IndexOf("오전") >= 0 || strGetConvertText.IndexOf("오후") >= 0)
            {
                if (strGetConvertText.IndexOf("AM") >= 0 || strGetConvertText.IndexOf("PM") >= 0)
                    strQueryConvertText = "SELECT dbo.XConvertDateE('" + strGetConvertText + "') AS CONVERT_DATETIME_E;";
                else if (strGetConvertText.IndexOf("오전") >= 0 || strGetConvertText.IndexOf("오후") >= 0)
                    strQueryConvertText = "SELECT dbo.XConvertDateK('" + strGetConvertText + "') AS CONVERT_DATETIME_K;";
            }

            DataTable getDt = null;

            string strGetResultText1 = string.Empty;
            string strGetResultText2 = string.Empty;

            if (strQueryConvertText != string.Empty)
                getDt = QuerySQL(strQueryConvertText);
            else
                strGetResultText1 = strGetConvertText;

            if (Commons.isHasRow(getDt))
                strGetResultText1 = getDt.Rows[0].ItemArray[0].ToString();

            string strQueryText = "SELECT CONVERT(VARCHAR, '" + strGetResultText1 + "', 121) AS CONVERT_DATETIME;";

            if (strGetResultText1 != string.Empty)
                getDt = QuerySQL(strQueryText);
            
            if (Commons.isHasRow(getDt))
                strGetResultText2 = getDt.Rows[0].ItemArray[0].ToString();

            return strGetResultText2;
        }
        */
    }
}