using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static SystemX.Net.Platform.Common.Util.LogMessage;
//using MySql.Data.MySqlClient;

namespace SystemX.Net.DB.DBType
{
    public class MariaXDB
    {
        //private MySqlConnection sqlConnection;
        //private MySqlCommand sqlCommand;
        //private MySqlDataReader sqlDataReader;
        private DataSet sqlDataSet;
        private DataTable sqlDataTable;

        private string m_strConnectionText;

        private bool m_bConnect;

        public bool CONNECT { get { return m_bConnect; } }

        public MariaXDB(string strSetConnectionInfo)
        {
            //sqlConnection = null;
            //sqlCommand = null;
            //sqlDataReader = null;
            sqlDataSet = null;
            sqlDataTable = null;

            m_strConnectionText = strSetConnectionInfo;

            m_bConnect = false;
        }
        ~MariaXDB()
        {
            ChkCloseConnect();
        }
        public void ChkCloseConnect()
        {
            if (m_bConnect)
            {
                m_bConnect = false;

                /*
                if (sqlConnection != null)
                {
                    sqlConnection.Close();
                    sqlConnection.Dispose();
                    sqlConnection = null;
                }
                */
            }
        }
        public bool ConnectDatabase()
        {
            ChkCloseConnect();

            try
            {
                //sqlConnection = new MySqlConnection(m_strConnectionText);
                //sqlCommand.Connection = sqlConnection;

                m_bConnect = true;
            }
            catch (Exception e)
            {
                m_bConnect = false;

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

            return m_bConnect;
        }
        public DataSet Query(string strSetCommand, out int iRecordsAffect)
        {
            iRecordsAffect = 0;

            sqlDataSet = null;

            try
            {
                //sqlCommand.CommandText = strSetCommand;
                //sqlDataReader = sqlCommand.ExecuteReader();

                //iRecordsAffect = sqlDataReader.RecordsAffected;

                sqlDataTable = new DataTable();
                //sqlDataTable.Load(sqlDataReader);

                sqlDataSet = new DataSet();
                sqlDataSet.Tables.Add(sqlDataTable);
            }
            catch (Exception e)
            {
                ;// throw new XDBCommonQueryException("Execution failed for the statement.[SystemX.Net.DB : MariaDB|COMMON.Query]");

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

            return sqlDataSet;
        }
    }
}
