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

namespace SystemX.Net.DB.DBType
{
    public partial class XDBTMSSQL : IDBControl, IDisposable
    {
        public bool ExecuteNonQuery(SqlCommand cmd)
        {
            bool bStatus = true;
            int iRecordsAffected = 0;

            if (cmd == null)
                return false;

            try
            {
                if (cmd.Connection == null)
                    cmd.Connection = dbConnection;

                iRecordsAffected = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {

                iRecordsAffected = 0;

                bStatus = false;

                string strErrorMsg = e.Message;
                string strSetData = DateTime.Now.ToString("yyyy -MM-dd HH:mm:ss>>");
                MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
                MessageOutput.ConsoleWrite(strSetData + @"! Command [" + cmd.CommandText + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
            }

            if (iRecordsAffected > 0)
                bStatus = true;
            else
                bStatus = false;

            return bStatus;
        }

        public bool ExecuteNonCommandQuery(SqlCommand cmd)
        {
            bool bStatus = true;
            int iRecordsAffected = 0;

            if (cmd == null)
                return false;

            try
            {
                if (cmd.Connection == null)
                    cmd.Connection = dbConnection;

                iRecordsAffected = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                string strErrorMsg = e.Message;

                iRecordsAffected = 0;

                bStatus = false;

                string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>");
                MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
                MessageOutput.ConsoleWrite(strSetData + @"! Command [" + cmd.CommandText + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
            }

            if (iRecordsAffected > 0)
                bStatus = true;
            else
                bStatus = false;

            return bStatus;
        }

        public bool ExecuteNonStreamQuery(SqlCommand cmd)
        {
            bool bStatus = true;
            int iRecordsAffected = 0;

            if (cmd == null)
                return false;

            try
            {
                if (cmd.Connection == null)
                    cmd.Connection = dbConnection;

                iRecordsAffected = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                string strErrorMsg = e.Message;

                iRecordsAffected = 0;

                bStatus = false;

                string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>");
                MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
                MessageOutput.ConsoleWrite(strSetData + @"! Command [" + cmd.CommandText + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
            }

            if (iRecordsAffected > 0)
                bStatus = true;
            else
                bStatus = false;

            return bStatus;
        }

        public SqlDataReader QueryDatabase(string strSQL)
        {
            bool bHasRow = false;
            int nCnt = 0;

            SqlDataReader QueryReader = null;

            try
            {
                using (var dbExcuteCommand = new SqlCommand(strSQL, dbConnection))
                {
                    QueryReader = dbExcuteCommand.ExecuteReader();
                }
            }
            catch (Exception e)
            {
                if (QueryReader != null)
                    QueryReader.Close();

                QueryReader = null;

                string strErrorMsg = e.Message;
                string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>");
                MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
                MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSQL + "] QueryDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
            }
            finally
            {
                if (QueryReader != null)
                {
                    bHasRow = QueryReader.HasRows;
                    nCnt = QueryReader.RecordsAffected;
                }
            }

            return QueryReader;
        }

        public SqlDataReader QueryDatabaseSub(string strSQL)
        {
            bool bHasRow = false;
            int nCnt = 0;

            SqlDataReader QueryReader = null;

            try
            {
                using (var dbExcuteCommand = new SqlCommand(strSQL, dbConnection))
                {
                    QueryReader = dbExcuteCommand.ExecuteReader();
                }
            }
            catch (Exception e)
            {
                if (QueryReader != null)
                    QueryReader.Close();

                QueryReader = null;

                string strErrorMsg = e.Message;
                string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>");
                MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryDatabaseSub failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
                MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSQL + "] QueryDatabaseSub failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
            }
            finally
            {
                if (QueryReader != null)
                {
                    bHasRow = QueryReader.HasRows;
                    nCnt = QueryReader.RecordsAffected;
                }
            }

            return QueryReader;
        }

        public SqlDataReader QueryStreamDatabase(string strSQL)
        {
            bool bHasRow = false;
            int nCnt = 0;

            SqlDataReader QueryReader = null;

            try
            {
                using (var dbExcuteCommand = new SqlCommand(strSQL, dbConnection))
                {
                    QueryReader = dbExcuteCommand.ExecuteReader();
                }
            }
            catch (Exception e)
            {
                if (QueryReader != null)
                    QueryReader.Close();

                QueryReader = null;

                string strErrorMsg = e.Message;
                string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>");
                MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryStreamDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
                MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSQL + "] QueryStreamDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
            }
            finally
            {
                if (QueryReader != null)
                {
                    bHasRow = QueryReader.HasRows;
                    nCnt = QueryReader.RecordsAffected;
                }
            }

            return QueryReader;
        }

        public SqlDataReader QueryCommandDatabase(string strSQL, bool bUseTransaction = false)
        {
            bool bHasRow = false;
            int nCnt = 0;

            SqlDataReader QueryReader = null;

            try
            {
                if (bUseTransaction)
                {
                    // Start a local transaction.
                    SqlTransaction sqlTran = dbConnection.BeginTransaction();

                    // Enlist a command in the current transaction.
                    SqlCommand command = dbConnection.CreateCommand();
                    command.Transaction = sqlTran;

                    try
                    {
                        // Execute two separate commands.
                        command.CommandText = strSQL;
                        QueryReader = command.ExecuteReader();

                        // Commit the transaction.
                        sqlTran.Commit();
                    }
                    catch (Exception ex)
                    {
                        // Handle the exception if the transaction fails to commit.
                        string strErrorMsg = ex.Message;
                        string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>");
                        MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryCommandDatabase Transaction failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
                        MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSQL + "] QueryCommandDatabase Transaction failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);

                        try
                        {
                            // Attempt to roll back the transaction.
                            sqlTran.Rollback();
                        }
                        catch (Exception exRollback)
                        {
                            // Throws an InvalidOperationException if the connection
                            // is closed or the transaction has already been rolled
                            // back on the server.
                            strErrorMsg = exRollback.Message;
                            strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>");
                            MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryCommandDatabase Transaction-Rollback failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
                        }
                    }
                }
                else
                {
                    using (var dbExcuteCommand = new SqlCommand(strSQL, dbConnection))
                    {
                        QueryReader = dbExcuteCommand.ExecuteReader();
                    }
                }
            }
            catch (Exception e)
            {
                if (QueryReader != null)
                    QueryReader.Close();

                QueryReader = null;

                string strErrorMsg = e.Message;
                string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>");
                MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryCommandDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
                MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSQL + "] QueryCommandDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG);
            }
            finally
            {
                if (QueryReader != null)
                {
                    bHasRow = QueryReader.HasRows;
                    nCnt = QueryReader.RecordsAffected;
                }
            }

            return QueryReader;
        }

        public DataTable QueryDataTable(string strSQL)
        {
            SqlDataReader Sqldr = null;

            try
            {
                Sqldr = QueryDatabase(strSQL);

                queryDataTable = new DataTable();
                queryDataTable.Load(Sqldr);
            }
            catch(Exception e)
            {
                ;//
            }
            finally
            {
                if (Sqldr != null)
                    Sqldr.Close();
            }

            return queryDataTable;

            //return null;
        }

        public DataTable QueryDataTable(string strSQL, out bool bHasRows)
        {
            bHasRows = false;

            return null;
        }

        public DataTable QueryDataTable(string strSQL, out int iRecordsAffected)
        {
            iRecordsAffected = 0;

            return null;
        }

        public DataTable QueryDataTable(string strSQL, out bool bHasRows, out int iRecordsAffected)
        {
            bHasRows = false;
            iRecordsAffected = 0;

            return null;
        }

        public DataSet QueryDataSet(string strSQL)
        {
            SqlDataReader Sqldr = null;

            try
            {
                Sqldr = QueryDatabase(strSQL);

                queryDataTable = new DataTable();
                queryDataTable.Load(Sqldr);

                queryDataSet = new DataSet();
                queryDataSet.Tables.Add(queryDataTable);
            }
            catch (Exception e)
            {
                ;//
            }
            finally
            {
                if (Sqldr != null)
                    Sqldr.Close();
            }

            return queryDataSet;
        }

        public DataSet QueryDataSet(string strSQL, out bool bHasRows)
        {
            bHasRows = false;

            return null;
        }

        public DataSet QueryDataSet(string strSQL, out int iRecordsAffected)
        {
            iRecordsAffected = 0;

            return null;
        }

        public DataSet QueryDataSet(string strSQL, out bool bHasRows, out int iRecordsAffected)
        {
            bHasRows = false;
            iRecordsAffected = 0;

            return null;
        }

        /*
        public DataSet Query(string strSetCommand, out int iRecordsAffect)
        {
            iRecordsAffect = 0;

            queryDataSet = null;

            try
            {
                dbCommand.CommandText = strSetCommand;
                queryDataReader = dbCommand.ExecuteReader();

                queryDataTable = new DataTable();
                queryDataTable.Load(queryDataReader);

                queryDataSet = new DataSet();
                queryDataSet.Tables.Add(queryDataTable);
            }
            catch (Exception e)
            {
                //throw new CommonQueryException("Execution failed for the statement.[SystemX.Net.DB : MSSQL|COMMON.Query]");
                MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"General packet marshalling failed.[SystemX.Common : COMMON.GetHeaderProtocol]", ConsoleColor.Yellow, LogMessageLevel.DEBUG);
            }

            return queryDataSet;
        }
        */
    }
}

