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

using static CpCommon.ExceptionHandler;
using static PsKGaudi.Parser.PsCCS;

using PsKGaudi.Parser.PsCCSSTDFn;
using CpTesterPlatform;
using CpApplication;
using CpApplication.Manager;
using CpCommon;

using CpTesterPlatform.CpLogUtil;
using static PsCommon.PsCommon;
using System.Xml.Linq;
using PsKGaudi.Parser.PsCCSArea;
using PsKGaudi;
using PsKGaudi.Parser.MacroModuleSkel;
using PsKGaudi.Parser.PsCCSSTDFn.Parameters;
using System.IO;
using PsKGaudi.Parser.PsCCSSTDFn;

namespace SystemX.Product.ALIS.UI.View.InfoList
{
    public partial class UcTestListView : UserControl
    {
        public struct psGaudiImformation
        {
            public bool bInfoLoading;
            public bool bInfoNormalLoaded;
            public PsCCSAreaTestListInfo psTestListInfo;
            public PsCCSAreaBasicInfo psBasicInfo;
            public PsCCSAreaVariantTable psVariantTable;
            public PsCCSFileInfo psFileInfo;
        }

        public enum eVRFYControlMode
        {
            New = 0,
            Insert,
            Change,
            Delete
        }

        public enum eSetViewMode
        {
            None = -1,
            MainView = 0,
            SubView
        }


        private int GetProdTestListFileNoFind(CTestListFileInformation item)
        {
            string strQueryText = "SELECT No FROM [" + strSelectTable + "] WHERE " + //Name = '" + item.Name + "' AND " +
                                                            "TestType = '" + item.TestType + "' AND " +
                                                            "Version = '" + item.Version + "' AND " +
                                                            "ProdCode = '" + item.ProdCode + "' AND " +
                                                            "FileName = '" + item.FileName + "';";

            ctrlDB.GetConnSqlCmd().CommandText = strQueryText;

            return Convert.ToInt32(ctrlDB.GetConnSqlCmd().ExecuteScalar());
        }


        /// <summary>
        /// 등록 날짜, 업데이트 날짜 이상의 DateTime 조건 에서 Parent-TestType-Version-ProdCode [GroupNo] 로 찾는다.
        /// 2022/11/08 : 날짜 조건 검색 삭제 -> 검색할 조합 정보가 Unique
        /// </summary>
        /// <param name="strSetRegDT"></param>
        /// <param name="strSetUpdateDT"></param>
        /// <returns></returns>
        private int GetProdTestListVariantNoFind(CVariantInformation item) 
        {
            /*
            string strQueryText = "SELECT No FROM [" + strSelectTable + "] WHERE ProdNo_P = '" + item.ProdNo_P + "' AND " +
                                                            "TestType = '" + item.TestType + "' AND " +
                                                            "Version = '" + item.Version + "' AND " +
                                                            "ProdCode = '" + item.ProdCode + "' AND " +
                                                            "FileName = '" + item.FileName + "' AND " +
                                                            //"RegDT >= '" + strSetRegDT + "' AND " +
                                                            //"RegUser = '" + item.RegUser + "' AND " +
                                                            //"UpdateDT >= '" + strSetUpdateDT + "' AND " +
                                                            //"UpdateUser = '" + item.UpdateUser + "' AND " +
                                                            "GroupNo = " + item.GroupNo + ";";
            */
            string strQueryText = "SELECT No FROM [" + strSelectTable + "] WHERE ProdNo_P = '" + item.ProdNo_P + "' AND " +
                                                "TestListFileNo = " + item.TestListFileNo + " AND " +
                                                "GroupNo = " + item.GroupNo + ";";

            ctrlDB.GetConnSqlCmd().CommandText = strQueryText;

            return Convert.ToInt32(ctrlDB.GetConnSqlCmd().ExecuteScalar());
        }

        private bool getCPxmlInfo(string strfilename, ref psGaudiImformation info)
        {
            info = new psGaudiImformation();

            try
            {
                XDocument xDoc = XDocument.Load(strfilename);

                PsCCSAreaBasicInfo psBasicInfo = PsCCSAreaBasicInfo.LoadXmlData(xDoc.Root);
                PsCCSAreaTestListInfo psTestListInfo = PsCCSAreaTestListInfo.LoadXmlData(xDoc.Root);
                PsCCSAreaVariantTable psVariantTable = PsCCSAreaVariantTable.LoadXmlData(xDoc.Root);
                PsCCSFileInfo psFileInfo = new PsCCSFileInfo(strfilename);

                info.psBasicInfo = psBasicInfo;
                info.psTestListInfo = psTestListInfo;
                info.psVariantTable = psVariantTable;
                info.psFileInfo = psFileInfo;
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        public PsCCSGaudiFile TemporaryTestListLoad(byte[] testListrawData)
        {
            PsCCSGaudiFile readGaudiFile = null;

            string strGetCurrentDir = Directory.GetCurrentDirectory();
            Guid uguid = Guid.NewGuid();
            string strTempFileName = @strGetCurrentDir + @"\TempTestList\";

            if (Directory.Exists(strTempFileName) == false)
                Directory.CreateDirectory(strTempFileName);

            strTempFileName += uguid.ToString("B") + ".CpXv___";

            try
            {

                File.WriteAllBytes(strTempFileName, (byte[])testListrawData);

                psGaudiImformation psGaudiInfo = new psGaudiImformation();

                if (getCPxmlInfo(strTempFileName, ref psGaudiInfo))
                {
                    if (psGaudiInfo.psVariantTable.GetStrVariantTable.Count > 0)
                    {
                        psGaudiInfo.psFileInfo = new PsCCSFileInfo(strTempFileName);

                        string getTTNR = ((KeyValuePair<string, string>)psGaudiInfo.psVariantTable.GetStrVariantTable[0]).Key;

                        readGaudiFile = PsCCSGaudiFile.PsCCSPaseDataApi(strTempFileName, getTTNR);
                    }
                }
            }
            catch
            {
                ;//
            }
            finally
            {
                if (File.Exists(strTempFileName))
                    File.Delete(strTempFileName);
            }

            return readGaudiFile;
        }

        public class PsCCSStdFnBaseDataCompare : IEqualityComparer<PsCCSStdFnBase>
        {
            bool bEqualSelect;

            public PsCCSStdFnBaseDataCompare(bool bSelectEqual = true)
            {
                bEqualSelect = bSelectEqual;
            }

            public bool Equals(PsCCSStdFnBase x, PsCCSStdFnBase y)
            {
                x.GetTestStepSummary();
                y.GetTestStepSummary();

                string MO1 = (x.GetMO() != null) ? x.GetMO() : "";
                string FuncName1 = (x.GetSTDFnNameAsEnum().ToString() != null) ? x.GetSTDFnNameAsEnum().ToString() : "";
                string Dim1 = (x.GetDimension().ToString() != null) ? x.GetDimension().ToString() : "";

                string[] strSummaryTestSteps1 = x.SummaryTestStep;
                string MacroParm1 = (strSummaryTestSteps1[6] != null) ? strSummaryTestSteps1[6] : "";
                string Parm1 = (strSummaryTestSteps1[8] != null) ? strSummaryTestSteps1[8] : "";

                string GetMin1 = (x.PairedParmsMinMax.Min != null) ? x.PairedParmsMinMax.Min : "";
                string GetMax1 = (x.PairedParmsMinMax.Max != null) ? x.PairedParmsMinMax.Max : "";

                string StepNum1 = x.StepNum.ToString();
                string Variant1 = (x.Variant != null) ? x.Variant : "";
                string Gate1 = (x.Gate != null) ? x.Gate : "";
                string Activate1 = (x.Activate.ToString() != null) ? x.Activate.ToString() : "";
                string Position1 = x.Position.ToString();
                //
                string MO2 = (y.GetMO() != null) ? y.GetMO() : "";
                string FuncName2 = (y.GetSTDFnNameAsEnum().ToString() != null) ? y.GetSTDFnNameAsEnum().ToString() : "";
                string Dim2 = (y.GetDimension().ToString() != null) ? y.GetDimension().ToString() : "";

                string[] strSummaryTestSteps2 = y.SummaryTestStep;
                string MacroParm2 = (strSummaryTestSteps2[6] != null) ? strSummaryTestSteps2[6] : "";
                string Parm2 = (strSummaryTestSteps2[8] != null) ? strSummaryTestSteps2[8] : "";

                string GetMin2 = (y.PairedParmsMinMax.Min != null) ? y.PairedParmsMinMax.Min : "";
                string GetMax2 = (y.PairedParmsMinMax.Max != null) ? y.PairedParmsMinMax.Max : "";

                string StepNum2 = y.StepNum.ToString();
                string Variant2 = (y.Variant != null) ? y.Variant : "";
                string Gate2 = (y.Gate != null) ? y.Gate : "";
                string Activate2 = (y.Activate.ToString() != null) ? y.Activate.ToString() : "";
                string Position2 = y.Position.ToString();

                if (string.Equals(MO1, MO2, StringComparison.Ordinal) &&
                   string.Equals(FuncName1, FuncName2, StringComparison.Ordinal) &&
                   string.Equals(Dim1, Dim2, StringComparison.Ordinal) &&
                   string.Equals(MacroParm1, MacroParm2, StringComparison.Ordinal) &&
                   string.Equals(Parm1, Parm2, StringComparison.Ordinal) &&
                   string.Equals(GetMin1, GetMin2, StringComparison.Ordinal) &&
                   string.Equals(GetMax1, GetMax2, StringComparison.Ordinal) &&
                   string.Equals(StepNum1, StepNum2, StringComparison.Ordinal) &&
                   string.Equals(Variant1, Variant2, StringComparison.Ordinal) &&
                   string.Equals(Gate1, Gate2, StringComparison.Ordinal) &&
                   string.Equals(Activate1, Activate2, StringComparison.Ordinal) &&
                   string.Equals(Position1, Position2, StringComparison.Ordinal))
                    return bEqualSelect;

                return !bEqualSelect;
            }

            public int GetHashCode(PsCCSStdFnBase obj)
            {
                return obj.StepNum.GetHashCode();
            }
        }

        public class CTestListFileInformation : ICloneable
        {
            public int TestListFileNo { set; get; }
            //public string Name { set; get; }
            public string TestType { set; get; }
            public string Version { set; get; }
            public string ProdCode { set; get; }
            public string FileName { set; get; }
            public DateTime RegDT { set; get; }
            public string RegUser { set; get; }
            public DateTime UpdateDT { set; get; }
            public string UpdateUser { set; get; }
            public string Comment { set; get; }
            public string Description { set; get; }       
            public List<string> VariantList { set; get; }

            public byte[] TestListFileLoadRawData { set; get; }

            public byte[] TestListFileSelectRawData { set; get; }

            public int? SelectTestListFileNo { set; get; }

            public PsCCSGaudiFile TestListFileLoadGaudiData { set; get; }

            public PsCCSGaudiFile TestListFileSelectGaudiData { set; get; }

            public bool bHasSelectedData { set; get; }

            public CTestListFileInformation()
            {
                TestListFileNo = int.MaxValue;
                //Name = "";
                TestType = "";
                Version = "";
                ProdCode = "";
                FileName = "";
                RegDT = new DateTime();
                RegDT = DateTime.Now;
                RegUser = "";
                UpdateDT = new DateTime();
                UpdateDT = DateTime.Now;
                UpdateUser = "";
                Comment = "";
                Description = "";
                VariantList = new List<string>();

                TestListFileLoadRawData = new byte[1];

                TestListFileSelectRawData = new byte[1];

                SelectTestListFileNo = null;

                TestListFileLoadGaudiData = null;

                TestListFileSelectGaudiData = null;

                bHasSelectedData = false;
            }

            public object Clone()
            {
                return new CTestListFileInformation()
                {
                    TestListFileNo = this.TestListFileNo,
                    //Name = this.Name,
                    TestType = this.TestType,
                    Version = this.Version,
                    ProdCode = this.ProdCode,
                    FileName = this.FileName,
                    RegDT = this.RegDT,
                    RegUser = this.RegUser,
                    UpdateDT = this.UpdateDT,
                    UpdateUser = this.UpdateUser,
                    Comment = this.Comment,
                    Description = this.Description,
                    VariantList = this.VariantList,

                    TestListFileLoadRawData = this.TestListFileLoadRawData,

                    TestListFileSelectRawData = this.TestListFileSelectRawData,

                    SelectTestListFileNo = this.SelectTestListFileNo,

                    TestListFileLoadGaudiData = this.TestListFileLoadGaudiData,

                    TestListFileSelectGaudiData = this.TestListFileSelectGaudiData,

                    bHasSelectedData = this.bHasSelectedData
                };
            }
        }

        public class CVariantInformation : ICloneable
        {
            public int VariantNo { set; get; }
            public string ProdNo_P { set; get; }
            public string TestType { set; get; }
            public string Version { set; get; }
            public string ProdCode { set; get; }
            public string FileName { set; get; }
            public DateTime RegDT { set; get; }
            public string RegUser { set; get; }
            public DateTime UpdateDT { set; get; }
            public string UpdateUser { set; get; }
            public int GroupNo { set; get; }
            public string Comment { set; get; }
            public string Description { set; get; }
            public int TestListFileNo { set; get; }
            public string TestListName { set; get; }

            public int? SelectVariantNo { set; get; }

            public bool bHasSelectedData { set; get; }

            public CVariantInformation()
            {
                VariantNo = int.MaxValue;
                ProdNo_P = "";
                TestType = "";
                Version = "";
                ProdCode = "";
                FileName = "";
                RegDT = new DateTime();
                RegDT = DateTime.Now;
                RegUser = "";
                UpdateDT = new DateTime();
                UpdateDT = DateTime.Now;
                UpdateUser = "";
                GroupNo = -1;
                Comment = "";
                Description = "";
                TestListFileNo = int.MaxValue;
                TestListName = string.Empty;

                SelectVariantNo = null;

                bHasSelectedData = false;
            }

            public object Clone()
            {
                return new CVariantInformation()
                {
                    VariantNo = this.VariantNo,
                    ProdNo_P = this.ProdNo_P,
                    TestType = this.TestType,
                    Version = this.Version,
                    ProdCode = this.ProdCode,
                    FileName = this.FileName,
                    RegDT = this.RegDT,
                    RegUser = this.RegUser,
                    UpdateDT = this.UpdateDT,
                    UpdateUser = this.UpdateUser,
                    GroupNo = this.GroupNo,
                    Comment = this.Comment,
                    Description = this.Description,
                    TestListFileNo = this.TestListFileNo,
                    TestListName = this.TestListName,

                    SelectVariantNo = this.SelectVariantNo,

                    bHasSelectedData = this.bHasSelectedData
                };
            }
        }
    }
}
