using lib;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Packing.lib.datasource
{
    public class LeakTestCodeDs
    {

        public event FinishedGetRelatedTraceabilityDataCallback OnFinishedGetRelatedTraceabilityData;

        public delegate void FinishedGetRelatedTraceabilityDataCallback(LeakTestCodeDs LeakTestCodeDs);

        public LeakTestCodeDs(string Box_Id, string LeakTestCode, string PackingTime = null, string SerialNo = null, bool isGetTraceabilityDataInBackground = false)
        {
            this.Box_Id = Box_Id;
            this.LeakTestCode = LeakTestCode;
            if (PackingTime != null && PackingTime != string.Empty)
            {
                this.PackingTime = DateTime.ParseExact(PackingTime, "yyyy-MM-dd HH:mm:ss",
                                       System.Globalization.CultureInfo.InvariantCulture);
            } else
            {
                this.PackingTime = DateTime.Now;
            }
            this.SerialNo = SerialNo;

            if (isGetTraceabilityDataInBackground)
                new Action(
                    async () => this.RelatedTraceabilityDataList = await GetRelatedTraceabilityDataAsync(LeakTestCode)
                )();
        }

        public static List<LeakTestCodeDs> list = new List<LeakTestCodeDs>();

        public string Box_Id { get; set; }
        public DateTime PackingTime { get; set; }
        public string LeakTestCode { get; set; }
        public string SerialNo { get; set; }
        public string PartName { get; set; }
        public string PartCode { get; set; }
        public string Abbreviation { get; set; }
        public string PartNo { get; set; }
        public static string ReprintPalletSerialNo { get; set; }
        public static string ReprintPrintDate { get; set; }
        public static string ReprintPartNo { get; set; }
        public static string ReprintPartCode { get; set; }
        public static string ReprintPartName { get; set; }
        public static string ReprintAbbreviation { get; set; }
        public static string ReprintSerialNo { get; set; }
        public static string ReprintInspectionDate { get; set; }
        public static string ReprintQty { get; set; }
        public static string ReprintUserName { get; set; }
        public static string PrintPalletSerialNo { get; set; }
        public static string PrintInspectionDate { get; set; }
        public static string PrintQty { get; set; }
        public List<Object[]> RelatedTraceabilityDataList { get; set; }
        public List<Object[]> RelatedMstDataList { get; set; }
        public List<Object[]> RelateReprintDataList { get; set; }

        public List<Object[]> RelatePrintDataList { get; set; }

        public override bool Equals(System.Object obj)
        {
            return obj.GetHashCode() == this.GetHashCode();
        }

        public override int GetHashCode()
        {
            return LeakTestCode.GetHashCode();
        }



        private async Task<List<Object[]>> GetRelatedTraceabilityDataAsync(string LeakTestCode)
        {
            DbTable dbTable = new DbTable();

            string sql = @"
                SELECT Serial_No, Process_No FROM tblTraceabilityData
                WHERE Serial_No IN (
                 SELECT top 1 Serial_No
                   FROM tblTraceabilityData
                  WHERE 1=1
                    AND LeakTest_Code = '" + LeakTestCode + @"'
                )
                ORDER BY Process_No
                ";

            List<Object[]> rows = await dbTable.executeQueryAsync(sql);
            if (rows.Count > 0)
            {
                this.SerialNo = rows[0][0].ToString();
                RelatedTraceabilityDataList = rows;
            }
            else
            {
                this.SerialNo = null;
                RelatedTraceabilityDataList = null;
            }

            OnFinishedGetRelatedTraceabilityData(this);

            return rows;
        }
        public List<Object[]> GetRelatedTraceabilityData()
        {
            DbTable dbTable = new DbTable();

            string sql = @"
                SELECT Serial_No, Process_No FROM tblTraceabilityData
                WHERE Serial_No IN (
                 SELECT top 1 Serial_No
                   FROM tblTraceabilityData
                  WHERE 1=1
                    AND LeakTest_Code = '" + this.LeakTestCode + @"'
                )
                ORDER BY Process_No
                ";

            List<Object[]> rows = dbTable.executeQuery(sql);
            if (rows.Count > 0)
            {
                this.SerialNo = rows[0][0].ToString();
                RelatedTraceabilityDataList = rows;
            }
            else
            {
                this.SerialNo = null;
                RelatedTraceabilityDataList = null;
            }

            return rows;
        }

        public List<Object[]> GetRelatedMstData(string partNo)
        {
            DbTable dbTable = new DbTable();

            string sql = @"
                SELECT Part_Name, Part_Code, Abbreviation, Part_No FROM tblPartMst
                WHERE Part_No = '" + partNo + @"'               
                ";

            List<Object[]> rows = dbTable.executeQuery(sql);
            if (rows.Count > 0)
            {
                this.PartName = rows[0][0].ToString();
                this.PartCode = rows[0][1].ToString();
                this.Abbreviation = rows[0][2].ToString();
                this.PartNo = rows[0][3].ToString();
                RelatedMstDataList = rows;
            }
            else
            {
                this.PartName = null;
                this.PartCode = null;
                this.Abbreviation = null;
                this.PartNo = null;
                RelatedMstDataList = null;
            }

            return rows;
        }


        public static List<Object[]> GetRelatedSerialPrintData()
        {
            DbTable dbTable = new DbTable();

            string sql = @"
                SELECT Max(Pallet_Serial_No) FROM tblHistoryPrint 
                ";

            List<Object[]> rows = dbTable.executeQuery(sql);
            
            return rows;
        }



        public static List<Object[]> GetRelatedReprintDataSelect(string PalletSerialNo)
        {
            DbTable dbTable = new DbTable();

            string sql = @"
                SELECT [Pallet_Serial_No], [Print_Date], [Part_No], [Part_Code], [Part_Name], [Abbreviation], [Serial_No], [Inspection_Date], [Qty], [User_Name] FROM [tblHistoryPrint]
                WHERE Pallet_Serial_No = '" + PalletSerialNo + @"'               
                ";

            List<Object[]> rows = dbTable.executeQuery(sql);
            //if (rows.Count > 0)
            //{
            //    this.ReprintPalletSerialNo = rows[0][0].ToString();
            //    this.ReprintPrintDate = rows[0][1].ToString();
            //    this.ReprintPartNo = rows[0][2].ToString();
            //    this.ReprintPartCode = rows[0][3].ToString();
            //    this.ReprintPartName = rows[0][4].ToString();
            //    this.ReprintAbbreviation = rows[0][5].ToString();
            //    this.ReprintSerialNo = rows[0][6].ToString();
            //    this.ReprintInspectionDate = rows[0][7].ToString();
            //    this.ReprintQty = rows[0][8].ToString();                
            //    RelateReprintDataList = rows;
            //}
            //else
            //{
            //    this.ReprintPalletSerialNo = null;
            //    this.ReprintPrintDate = null;
            //    this.ReprintPartNo = null;
            //    this.ReprintPartCode = null;
            //    this.ReprintPartName = null;
            //    this.ReprintAbbreviation = null;
            //    this.ReprintSerialNo = null;
            //    this.ReprintInspectionDate = null;
            //    this.ReprintQty = null;                
            //    RelateReprintDataList = null;
            //}

            return rows;
        }

        public static List<Object[]> GetRelatedReprintData()
        {
            DbTable dbTable = new DbTable();

            string sql = @"
                SELECT [Pallet_Serial_No] , [Inspection_Date] FROM(
                SELECT top 10 [Pallet_Serial_No] , [Inspection_Date], [Print_Date]  FROM [tblHistoryPrint]
                order by [Print_Date] desc) as Data  order by [Print_Date]                             
                ";

            List<Object[]> rows = dbTable.executeQuery(sql);
            
            return rows;
        }






        //public static List<Object[]> InsertReprintData(string Pallet_Serial_No, string Print_Date, Int32 Part_No, string Part_Code, 
        //    string Part_Name, string Abbreviation, string Serial_No, string Inspection_Date, string Qty)
        //{
        //    DbTable dbTable = new DbTable();

        //    string sql = @"
        //        INSERT INTO tblHistoryPrint (Pallet_Serial_No, Print_Date, Part_No, Part_Code, Part_Name, Abbreviation, Serial_No, Inspection_Date, Qty) 
        //        VALUES ('" + Pallet_Serial_No + @"' , '" + Print_Date + @"' , " + Part_No + @" , '" + Part_Code + @"' , '" 
        //                + Part_Name + @"' , '" + Serial_No + @"' , '" + Inspection_Date + @"' , '" + Qty + @"')                       
        //        ";

        //    List<Object[]> rows = dbTable.executeQuery(sql);

        //    return rows;
        //}

    }
}
