using System;
using System.Data;
using System.IO;
using iMESCore.Settings;
using Microsoft.VisualBasic;
using static iMESCore.DataBase.iMESSql;

namespace AutoLoaderLib_C
{

    public abstract class EPIParseTemplate
    {

        // 設定暫存資料表的欄位
        internal abstract bool AddColumns(DataTable dt);

        // 解析資料, 並寫入暫存資料表
        internal abstract bool ParsingFile(DataTable dt, string file);

        // 將暫存資料表寫入資料庫
        internal abstract bool InsertTable(DataTable dt);

        internal IDbConnection objConnection;

        public enum FileTypeEnum
        {
            csv = 0,
            xls = 1,
            dat = 2
        }

        private string[] wildcards = new string[] { "*.csv", "*.xls", "*.dat" };
        private string strDataBaseType;                 // DataBase Type:oracle, mysql, access
        private string strConnectionString;             // Connection string
        private AppSettings objSetting = new AppSettings();
        private DataTable objDataTable;
        private string SourcePath;
        private string DestinationPath;
        private string QueuePath;
        private string FailPath;

        /// <summary>
    /// 建構子
    /// </summary>
    /// <param name="paramSourcePath"></param>
    /// <param name="paramDestinationPath"></param>
    /// <param name="paramQueuePath"></param>
    /// <param name="paramFailPath"></param>
    /// <remarks></remarks>
        public EPIParseTemplate(string paramSourcePath, string paramDestinationPath, string paramQueuePath, string paramFailPath)
        {

            objSetting = new AppSettings();

            // //Get database type
            strDataBaseType = objSetting.GetDataBaseType();

            // //Get connection string
            strConnectionString = objSetting.GetConnectionString(strDataBaseType);

            objConnection = CreateConnection(strConnectionString);

            if (Strings.Right(paramSourcePath, 1) != @"\")
            {
                paramSourcePath += @"\";
            }

            SourcePath = paramSourcePath;

            if (Strings.Right(paramDestinationPath, 1) != @"\")
            {
                paramDestinationPath += @"\";
            }

            DestinationPath = paramDestinationPath;

            if (Strings.Right(paramQueuePath, 1) != @"\")
            {
                paramQueuePath += @"\";
            }

            QueuePath = paramQueuePath;

            if (Strings.Right(paramFailPath, 1) != @"\")
            {
                paramFailPath += @"\";
            }

            FailPath = paramFailPath;

        }

        ~EPIParseTemplate()
        {

            if (objConnection != null)
            {
                objConnection = null;
            }

            if (objSetting != null)
            {
                objSetting = null;
            }

            if (objDataTable != null)
            {
                objDataTable = null;
            }

        }

        /// <summary>
    /// 解析資料
    /// </summary>
    /// <param name="paramFileType"></param>
    /// <returns></returns>
    /// <remarks></remarks>
        public string ParsingData(FileTypeEnum paramFileType)
        {

            string strResult = string.Empty;
            string QueueFile = "";
            string DestinationFile = "";
            string FailFile = "";

            try
            {

                objDataTable = new DataTable("RawData");

                // 設定暫存資料表的欄位
                if (AddColumns(objDataTable))
                {

                    foreach (var SourceFile in Directory.GetFiles(SourcePath, wildcards[(int)paramFileType], SearchOption.TopDirectoryOnly))
                    {

                        if (File.Exists(SourceFile))
                        {

                            QueueFile = QueuePath + Path.GetFileName(SourceFile);
                            DestinationFile = DestinationPath + Path.GetFileName(SourceFile);
                            FailFile = FailPath + Path.GetFileName(SourceFile);

                            try
                            {

                                // 將檔案從 Source 移至 Queue
                                MoveFile(SourceFile, QueueFile);

                                objDataTable.Rows.Clear();

                                // 解析資料, 並寫入暫存資料表
                                if (ParsingFile(objDataTable, QueueFile))
                                {

                                    // 將暫存資料表寫入資料庫
                                    if (InsertTable(objDataTable))
                                    {

                                        // 將檔案從 Queue 移至 Destination
                                        MoveFile(QueueFile, DestinationFile);

                                    }

                                }
                            }

                            catch (Exception ex)
                            {

                                // 將檔案從 Queue 移至 Fail
                                MoveFile(QueueFile, FailFile);

                                throw;

                            }

                        }

                    }

                    strResult = "success";

                }
            }

            catch (Exception ex)
            {

                strResult = "Message:" + ex.Message + ",StackTrace:" + ex.StackTrace;
            }

            finally
            {

            }

            return strResult;

        }

        /// <summary>
    /// 搬移檔案
    /// </summary>
    /// <param name="paramSourceFile">來源檔案</param>
    /// <param name="paramTargetFile">目的檔案</param>
    /// <remarks></remarks>
        private void MoveFile(string paramSourceFile, string paramTargetFile)
        {

            try
            {

                // 判斷目的檔案是否已存在, 若存在必須先刪除
                if (File.Exists(paramTargetFile))
                {
                    File.Delete(paramTargetFile);
                }

                // 移動檔案至目的
                Directory.Move(paramSourceFile, paramTargetFile);
            }

            catch (Exception ex)
            {

                throw;

            }

        }

    }
}