using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;    // SqlConnection
using Oracle.ManagedDataAccess.Client;
using System.Data;              // ConnectionState, DataTable

namespace Post
{
    public class ProcessDatabase
    {
        public ExecuteOracleDatabase executeOracle;
        public ExecuteSqlserverDatabase executeSqlserver;

        public string DB_TYPE { get; set; }

        public ProcessDatabase(string type)
        {
            DB_TYPE = type;
        }

        #region ConnectionDB() : db connection
        //public bool ConnectionDB(string ip, int port, string dbname, string username, string password)
        public Object ConnectionDB(string ip, int port, string dbname, string username, string password)
        {
            ConstValues constValues = new ConstValues();
            ConstValuesConfig constValuesConfig = new ConstValuesConfig();
            Object conn = new Object();
            //bool result = true;

            Console.WriteLine("DB_TYPE : " + DB_TYPE);

            try
            {

                // conn validation check
                if (ip.Length == 0)
                {
                    ip = Environment.GetEnvironmentVariable("localhost");
                    Console.WriteLine("localhost : " + ip);
                } else if (port == 0)
                {
                    if (DB_TYPE.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                    {
                        port = constValuesConfig.CONST_DEFAULT_CONN_PORT_ORACLE;
                    } else if (DB_TYPE.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                    {
                        port = constValuesConfig.CONST_DEFAULT_CONN_PORT_SQLSERVER;
                    }
                }

                if (DB_TYPE.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                {
                    conn = ConnectOracle(ip, port, dbname, username, password);
                }
                else if (DB_TYPE.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                {
                    conn = ConnectSqlserver(ip, port, dbname, username, password);
                }


            } catch(Exception e)
            {
                Console.WriteLine("connectiondb : " + e.ToString());
                return null;
            }

            return conn;
        }
        #endregion

        #region ConnectSqlserver() : check sqlserver connection
        public SqlConnection ConnectSqlserver(string ip, int port, string dbname, string username, string password)
        {
            Console.WriteLine("ConnectSqlserver");
            SqlConnection connection = new SqlConnection();

            string dataSource = String.Format(@"{0},{1}", ip, port);
            connection.ConnectionString = String.Format("DATA SOURCE={0};Initial Catalog={1};User id={2};Password={3};Pooling={4};Connection TImeout={5};Min Pool Size={6};Max pool Size={7}", dataSource, dbname, username, password, "true", 120, 1, 10);
            connection.Open();
            Console.WriteLine("sqlserver state : " + connection.State);

            if (connection.State == ConnectionState.Broken || connection.State == ConnectionState.Closed)
            {
                return null;
            }

            executeSqlserver = new ExecuteSqlserverDatabase(connection);
            return connection;
        }
        #endregion

        #region ConnectOracle() : check oracle connection
        public OracleConnection ConnectOracle(string ip, int port, string dbname, string username, string password)
        {
            Console.WriteLine("ConnectOracle");
            OracleConnection connection = new OracleConnection();
            string dataSource = String.Format("{0}:{1}/{2}", ip, port, dbname);
            connection.ConnectionString = String.Format("DATA SOURCE={0};USER ID={1};Password={2};Pooling={3};Connection Timeout={4};Min Pool Size={5};Max pool Size={6}", dataSource, username, password, "true", 120, 1, 10);
            connection.Open();

            Console.WriteLine("oracle state : " + connection.State);

            executeOracle = new ExecuteOracleDatabase(connection);
            return connection;
        }
        #endregion

        #region CheckConnState() : check db connection
        public  bool CheckConnState()
        {
            bool result = true;
            ConstValues constValues = new ConstValues();

            try
            {
                ConnectionState state = new ConnectionState();
                if (DB_TYPE.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                {
                    state = executeOracle.orclConn.State;
                } else if (DB_TYPE.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("== : " + executeSqlserver.sqlConn.State);
                    state = executeSqlserver.sqlConn.State;
                }

                if (state == ConnectionState.Broken || state == ConnectionState.Closed)
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("checkConn : " + e.ToString());
                result = false;
            }

            return result;
        }
        #endregion

        #region DisconnectDB() : db disconnection
        public void DisconnectDB(String type, Object conn)
        {
            Console.WriteLine("database 종료");
            ConstValues constValues = new ConstValues();
            try
            {
                if (type.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                {
                    OracleConnection oracleconn = (OracleConnection)conn;
                    oracleconn.Close();
                }
                else if (type.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                {
                    SqlConnection sqlconn = (SqlConnection)conn;
                    sqlconn.Close();
                }
            } catch(Exception e)
            {
                Console.WriteLine("disconn : " + e.ToString());
            }
        }
        #endregion
    }
}
