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

using NLog;
using System.IO;                // StreamReader
using System.Data.SqlClient;    // SqlConnection
using System.Data;              // ConnectionState, DataTable

namespace ConnDB
{
    public class Connect
    {
        Logger log = LogManager.GetLogger("agent_logger");

        public Struct.ConnStateStc ConnectDB(String file_name, String home_path)
        { 
            bool conn_result = true;
            Struct.ConnInfoStc conn_info_struct = new Struct.ConnInfoStc();     // db 연결 정보
            Struct.ConnStateStc conn_state_struct = new Struct.ConnStateStc();  // db 연결 상태
            SqlConnection connection = new SqlConnection();

            try
            {
                log.Debug("Database Connect Start");
                // read database connect file
                String connPath = home_path + "conf\\" + file_name + ".conn";

                FileInfo fileInfo = new FileInfo(connPath);
                if (!fileInfo.Exists)
                {
                    conn_result = false;
                    Console.WriteLine("non-existent conn file path");
                    log.Error("non-existent recv conn file path");
                }
                else
                {

                    StreamReader reader = new StreamReader(connPath);
                    String readContents = reader.ReadToEnd();
                    reader.Close();

                    // conn 정보 구조체에 저장
                    String[] conn = readContents.Split('"');

                    conn_info_struct.ip = conn[1];
                    conn_info_struct.port = conn[3];
                    conn_info_struct.dbname = conn[5];
                    conn_info_struct.username = conn[7];
                    conn_info_struct.password = conn[9];

                    // db connect
                    String dataSource = String.Format(@"{0},{1}", conn_info_struct.ip, conn_info_struct.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, conn_info_struct.dbname, conn_info_struct.username, conn_info_struct.password, "true", 120, 1, 10);
                    connection.Open();

                    // db connection state check
                    if (connection.State == ConnectionState.Broken || connection.State == ConnectionState.Closed)
                    {
                        conn_result = false;
                    }
                }
            }
            catch (Exception e)
            {
                log.Error(e.ToString);
                conn_result = false;
            }

            conn_state_struct.is_connect = conn_result;     // db connection result
            conn_state_struct.connection = connection;      // db connection info

            return conn_state_struct;
        }
    }
}
