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

using System.IO;    // File
using System.Text.RegularExpressions;   // Regex

namespace Post
{
    public class ConfigConn : AllFile
    {
        public string IP { get; set; }
        public int PORT { get; set; }
        public string DBNAME { get; set; }
        public string USERNAME { get; set; }
        public string PASSWORD { get; set; }

        public ConfigConn()
        {
            IP = "";
            PORT = 0;
            DBNAME = "";
            USERNAME = "";
            PASSWORD = "";
        }

        public ConfigConn LoadConn(string alias)
        {
            ConfigConn configConn = new ConfigConn();
            ConstFunc constFunc = new ConstFunc();
            ConstValues constValues = new ConstValues();
            ConstValuesConfig constValuesConfig = new ConstValuesConfig();

            try
            {
                string path = constFunc.GetDirectoryPath(constValues.CONST_CONFIG_CONN) + alias + ".conn";
                Console.WriteLine("conn path : " + path);
                string[] arrData = LoadFile(path);

                // validation check
                for (int i = 0; i < arrData.Length; i++)
                {
                    arrData[i] = arrData[i].Replace('"', ' ').Replace(';', ' ');
                    Console.WriteLine(">> " + arrData[i]);
                    string[] arr = Regex.Split(arrData[i], "=");
                    string[] arrTemp = new string[2];

                    if (arr.Length > 1)
                    {
                        arrTemp[0] = arr[0];
                        arrTemp[1] = arr[1].Trim();
                    }
                    else
                    {
                        arrTemp[0] = arr[0];
                        arrTemp[1] = "";
                    }

                    if (arrTemp[0].Length > 0 && !arrTemp[0].Substring(0, 1).Equals("#"))
                    {
                        if (arrTemp[0].Equals(constValuesConfig.CONST_CONN_IP, StringComparison.OrdinalIgnoreCase))
                            configConn.IP = arrTemp[1];
                        else if (arrTemp[0].Equals(constValuesConfig.CONST_CONN_PORT, StringComparison.OrdinalIgnoreCase))
                            configConn.PORT = Int32.Parse(arrTemp[1]);
                        else if (arrTemp[0].Equals(constValuesConfig.CONST_CONN_DBNAME, StringComparison.OrdinalIgnoreCase))
                        {
                            if (arrTemp[1].Length == 0)
                            {
                                Console.WriteLine(arrTemp[0] + " 값 누락");
                                return null;
                            }
                            configConn.DBNAME = arrTemp[1];
                        }
                        else if (arrTemp[0].Equals(constValuesConfig.CONST_CONN_USERNAME, StringComparison.OrdinalIgnoreCase))
                        {

                            if (arrTemp[1].Length == 0)
                            {
                                Console.WriteLine(arrTemp[0] + " 값 누락");
                                return null;
                            }
                            configConn.USERNAME = arrTemp[1];
                        }
                        else if (arrTemp[0].Equals(constValuesConfig.CONST_CONN_PASSWORD, StringComparison.OrdinalIgnoreCase))
                        {

                            if (arrTemp[1].Length == 0)
                            {
                                Console.WriteLine(arrTemp[0] + " 값 누락");
                                return null;
                            }
                            configConn.PASSWORD = arrTemp[1];
                        }
                    }
                }

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

            return configConn;
        }
    }
}
