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

using System.Text.RegularExpressions;   // Regex

namespace Post
{
    public class ConfigPost : AllFile
    {
        public string DBCONN { get; set; }
        public string DB_TYPE { get; set; }
        public string TRACINGFILE_DEST { get; set; }
        public string TRACINGFILE { get; set; }
        public string CREATE_TABLE { get; set; }
        public string LOG_LEVEL { get; set; }
        public string LOG_BACKUP { get; set; }

        public ConfigPost()
        {
            DBCONN = "";
            DB_TYPE = "";
            TRACINGFILE_DEST = "";
            TRACINGFILE = "";
            CREATE_TABLE = "";
            LOG_LEVEL = "";
            LOG_BACKUP = "";
        }

        #region LoadConfig() : configuration file load
        public ConfigPost LoadConfig(string alias)
        {
            ConfigPost config = new ConfigPost();
            ConstValues constValues = new ConstValues();
            ConstFunc constFunc = new ConstFunc();
            ConstValuesConfig constValuesConfig = new ConstValuesConfig();

            string homePath = "";

            try
            {
                string path = constFunc.GetDirectoryPath(constValues.CONST_CONFIG_POST) + alias + ".conf";
                string[] arrData = LoadFile(path);

                homePath = Environment.GetEnvironmentVariable("POST_HOME");

                // validation check
                for (int i = 0; i < arrData.Length; i++)
                {
                    arrData[i] = arrData[i].Replace('"', ' ').Replace(';', ' ');
                    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_POST_DBCONN, StringComparison.OrdinalIgnoreCase))
                        {
                            if (arrTemp[1].Length == 0)
                            {
                                Console.WriteLine(arrTemp[0] + " 값 누락");
                                return null;
                            }
                            config.DBCONN = arrTemp[1];
                        }
                        else if (arrTemp[0].Equals(constValuesConfig.CONST_POST_DB_TYPE, StringComparison.OrdinalIgnoreCase))
                        {
                            if (arrTemp[1].Length == 0)
                            {
                                Console.WriteLine(arrTemp[0] + " 값 누락");
                                return null;
                            }
                            config.DB_TYPE = arrTemp[1];
                        }
                        else if (arrTemp[0].Equals(constValuesConfig.CONST_POST_TRACINGFILE_DEST, StringComparison.OrdinalIgnoreCase))
                            config.TRACINGFILE_DEST = arrTemp[1];
                        else if (arrTemp[0].Equals(constValuesConfig.CONST_POST_TRACINGFILE, StringComparison.OrdinalIgnoreCase))
                        {
                            if (arrTemp[1].Length == 0)
                            {
                                Console.WriteLine(arrTemp[0] + " 값 누락");
                                return null;
                            }
                            config.TRACINGFILE = arrTemp[1];
                        }
                        else if (arrTemp[0].Equals(constValuesConfig.CONST_POST_CREATE_TABLE, StringComparison.OrdinalIgnoreCase))
                        {
                            if (arrTemp[1].Length == 0)
                            {
                                config.CREATE_TABLE = "ON_SCRIPT";
                            }
                            config.CREATE_TABLE = arrTemp[1];
                        }
                        else if (arrTemp[0].Equals(constValuesConfig.CONST_POST_LOG_LEVEL, StringComparison.OrdinalIgnoreCase))
                        {
                            if (arrTemp[1].Length == 0)
                            {
                                config.LOG_LEVEL = "info";
                            }
                            config.LOG_LEVEL = arrTemp[1];
                        }
                        else if (arrTemp[0].Equals(constValuesConfig.CONST_POST_LOG_BACKUP, StringComparison.OrdinalIgnoreCase))
                        {
                            if (arrTemp[1].Length == 0)
                            {
                                config.LOG_BACKUP = "NONE";
                            }
                            config.LOG_BACKUP = arrTemp[1];
                        }
                    }
                }

                Console.WriteLine("--------------------------");


                // dest 값 처리
                if (config.TRACINGFILE_DEST.Length > 0)
                {
                    string strDest = config.TRACINGFILE_DEST;
                    if (strDest.Length > 0)
                    {
                        if (!strDest.StartsWith(homePath))
                        {
                            // 상대경로 입력시 전체 경로 구함
                            config.TRACINGFILE_DEST = homePath + "\\" + strDest;
                        }
                    }
                    else
                    {
                        // trace 빈값이면 /POST_HOME/trace 경로를 지정
                        config.TRACINGFILE_DEST = homePath + "\\" + constValues.CONST_TRACE;
                    }
                }
            } catch(Exception e)
            {
                Console.WriteLine("LoadConfig : " + e.ToString());
                return null;
            }

            return config;
        }
        #endregion
    }
}
