using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using SystemX.Common.Archive;
using static SystemX.Net.Platform.Common.Util.LogMessage;

namespace SystemX.Net.XAdaptor
{
    public class QueryGetInfo
    {
        public string ProdNo_C;
        public string ProdNo_P;
        public string TestCode;
        public string Gate1;
        public string Gate2;
        public string FileName;
        public string RegUserComment;
        public string Description;
        public string GroupName;
        public string ModelName;
        public string VariantNo;
        public string TestListNo;
        public string Config;
        public string TestType;
        public string Version;
        public string ProdCode;

        public QueryGetInfo()
        {
            ProdNo_C = "";
            ProdNo_P = "";
            TestCode = "";
            Gate1 = "";
            Gate2 = "";
            FileName = "";
            RegUserComment = "";
            Description = "";
            GroupName = "";
            ModelName = "";
            VariantNo = "";
            TestListNo = "";
            Config = "";
            TestType = "";
            Version = "";
            ProdCode = "";
        }
    }

    public interface IControlTestListDataSet
    {
        QueryGetInfo ReadInfo();

        DataSet getTestListDataSet();

        int getTestListCount();

        string GetTestListCntID();

        DataRow[] GetPRODTestList(string strProdP_No, string strProdC_No,
            string strTestType, 
            string strTestCode, 
            string strVersion, 
            string strProdCode);

        DataRow[] GetCurrentTestListRow();

        byte[] getTestListFileBytes();

        string getTestListFileFullName();

        string getTestListFileName();

        string getTestListFileExtension();

        string MakeTestListFile(string strMakePath, 
            bool bCreateDirectory = true, 
            bool bDuplicateFileDelete = true);
    }

    internal class TProdTestListInfo : IControlTestListDataSet
    {
        private QueryGetInfo tslInfo;

        private DataSet dsQueryTestLists;

        /*
         * Caution : Row 0 Index 고정
         */
        private DataRow[] drFindInfos;

        private byte[] ucGetTestListData;

        private string strGetFileFullName;
        private string strGetFileName;
        private string strGetVersion;
        private string strGetProdCode;
        private string strGetExtension;

        private string strGetTestListCntID;

        public string GetTestListCntID()
        {
            return strGetTestListCntID;
        }

        public void SetTestListCntID(string strSetValue)
        {
            strGetTestListCntID = strSetValue;
        }

        public void SetTestListDataSet(DataSet ds)
        {
            dsQueryTestLists = new DataSet();
            dsQueryTestLists = ds.Copy();

            if (dsQueryTestLists.Tables[0].Rows.Count == 1)
            {
                ucGetTestListData = dsQueryTestLists.Tables[0].Rows[0]["TestListData"] as byte[];
                ucGetTestListData = XDataArchive.DecompressGZipByteToByte(ucGetTestListData);

                strGetFileName = dsQueryTestLists.Tables[0].Rows[0]["FileName"].ToString();
                strGetVersion = dsQueryTestLists.Tables[0].Rows[0]["Version"].ToString();
                strGetProdCode = dsQueryTestLists.Tables[0].Rows[0]["ProdCode"].ToString();
                strGetExtension = ".CpXv" + strGetVersion + strGetProdCode;

                strGetFileFullName = strGetFileName + strGetExtension;

                setQueryInfo(dsQueryTestLists.Tables[0].Rows[0]);
            }
        }

        public DataSet getTestListDataSet()
        {
            return dsQueryTestLists;
        }

        public int getTestListCount()
        {
            return dsQueryTestLists != null ? dsQueryTestLists.Tables[0].Rows.Count : 0;
        }

        public TProdTestListInfo()
        {
            tslInfo = new QueryGetInfo();

            dsQueryTestLists = new DataSet();
            dsQueryTestLists.Clear();

            ClearVar();
        }

        public DataRow[] GetCurrentTestListRow()
        {
            return drFindInfos;
        }

        public void ClearVar()
        {
            ucGetTestListData = null;

            strGetFileName = "";
            strGetVersion = "";
            strGetProdCode = "";
            strGetExtension = "";

            strGetTestListCntID = "";
        }

        public QueryGetInfo ReadInfo()
        {
            return tslInfo;
        }

        virtual public void setQueryInfo(DataRow dr)
        {    
            tslInfo.ProdNo_C = dr["ProdNo_C"].ToString();
            tslInfo.ProdNo_P = dr["ProdNo_P"].ToString();
            tslInfo.TestCode = dr["TestCode"].ToString();
            tslInfo.Gate1 = dr["Gate1"].ToString();
            tslInfo.Gate2 = dr["Gate2"].ToString();
            tslInfo.FileName = dr["FileName"].ToString();
            tslInfo.RegUserComment = dr["RegUserComment"].ToString();
            tslInfo.Description = dr["Description"].ToString();
            tslInfo.GroupName = dr["GroupName"].ToString();
            tslInfo.ModelName = dr["ModelName"].ToString();
            tslInfo.VariantNo = dr["VariantNo"].ToString();
            tslInfo.TestListNo = dr["TestListNo"].ToString();
            tslInfo.Config = dr["Config"].ToString();
            tslInfo.TestType = dr["TestType"].ToString();
            tslInfo.Version = dr["Version"].ToString();
            tslInfo.ProdCode = dr["ProdCode"].ToString();
        }

        private void setDataRowInsertInfo()
        {
            ucGetTestListData = drFindInfos[0]["TestListData"] as byte[];
            ucGetTestListData = XDataArchive.DecompressGZipByteToByte(ucGetTestListData);

            strGetFileName = drFindInfos[0]["FileName"].ToString();
            strGetVersion = drFindInfos[0]["Version"].ToString();
            strGetProdCode = drFindInfos[0]["ProdCode"].ToString();
            strGetExtension = ".CpXv" + strGetVersion + strGetProdCode;

            strGetFileFullName = strGetFileName + strGetExtension;
        }

        public DataRow[] GetPRODTestList(string strProdP_No, string strProdC_No,
            string strTestType, 
            string strTestCode, 
            string strVersion, 
            string strProdCode)
        {
            drFindInfos = null;

            if (dsQueryTestLists.Tables.Count <= 0)
                return null;

            try
            {
                if (dsQueryTestLists.Tables[0].Rows.Count == 1)
                {
                    drFindInfos = dsQueryTestLists.Tables[0].Select("");

                    if (drFindInfos.Count() > 0)
                    {
                        setDataRowInsertInfo();

                        setQueryInfo(drFindInfos[0]);
                    }
                    else
                        ClearVar();

                    return drFindInfos;
                }
            }
            catch (Exception ex)
            {
                MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @" Failed to test-list info column check. check the dll-version. [SystemX.Net.XAdaptor : TestListInfo.GetPRODTestList]\r\n" + ex.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG);

                drFindInfos = null;
            }

            if (drFindInfos == null)
                return drFindInfos;

            string[] strParameters = new string[5];

            strParameters[0] = strProdP_No;
            strParameters[1] = strTestType;
            strParameters[2] = strVersion;
            strParameters[3] = strProdCode;
            strParameters[4] = strTestCode;

            string strSetSelectText = "";
            int iParameterCnt = 0;

            for (int i = 0; i < strParameters.Length; i++)
            {
                if (strParameters[i].Length > 0)
                {
                    iParameterCnt++;

                    if (iParameterCnt > 1)
                        strSetSelectText += "AND ";

                    switch(i)
                    {
                        case 0: strSetSelectText += "ProdNo_P = '" + strParameters[i] + "' "; break;
                        case 1: strSetSelectText += "TestType = '" + strParameters[i] + "' "; break;
                        case 2: strSetSelectText += "Version = '" + strParameters[i] + "' "; break;
                        case 3: strSetSelectText += "ProdCode = '" + strParameters[i] + "' "; break;
                        case 4: strSetSelectText += "TestCode = '" + strParameters[i] + "' "; break;
                    }
                }
            }
         
            if(strSetSelectText.Length > 0)
                drFindInfos = dsQueryTestLists.Tables[0].Select(strSetSelectText);

            if (drFindInfos != null && drFindInfos.Count() > 0)
            {
                setDataRowInsertInfo();

                setQueryInfo(drFindInfos[0]);
            }
            else
                ClearVar();
               
            return drFindInfos;
        }

        public byte[] getTestListFileBytes()
        {
            return ucGetTestListData;
        }

        public string getTestListFileFullName()
        {
            return strGetFileFullName;
        }

        public string getTestListFileName()
        {
            return strGetFileName;
        }

        public string getTestListFileExtension()
        {
            return strGetExtension;
        }

        public string MakeTestListFile(string @strMakePath, 
            bool bCreateDirectory = true, 
            bool bDuplicateFileDelete = true)
        {
            string strSetMakePath = "";

            if (Directory.Exists(@strMakePath) == false)
            {
                if (bCreateDirectory == false)
                {
                    strSetMakePath = "The specified location does not exist.";
                    return strSetMakePath;
                }
                else
                    Directory.CreateDirectory(@strMakePath);
            }

            if (strGetFileFullName.Length > 0)
            {
                if (Base.CheckPath(strMakePath) == false)
                {
                    strSetMakePath = "Enter only the path excluding the file name and extension as parameters.";
                    return strSetMakePath;
                }

                string strSetFilePos = strMakePath + strGetFileFullName;

                if (File.Exists(strSetFilePos))
                {
                    try
                    {
                        if (bDuplicateFileDelete)
                            File.Delete(strSetFilePos);
                    }
                    catch (Exception ex)
                    {
                        MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @" Failed to delete duplicate test-list file. [SystemX.Net.XAdaptor : TestListInfo.MakeTestListFile]\r\n" + ex.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG);
                    }
                }

                strSetMakePath = strSetFilePos;

                try
                {
                    File.WriteAllBytes(@strSetFilePos, ucGetTestListData);
                }
                catch (Exception ex)
                {
                    MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @" Failed to create test list file. [SystemX.Net.XAdaptor : TestListInfo.MakeTestListFile]\r\n" + ex.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG);

                    strSetMakePath = "";
                }
            }
            
            return strSetMakePath;
        }
    }
}
