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

namespace SystemX.Product.TRA.UIControl
{
    public class OverviewSearchOption
    {
        public bool DisplayByStation { get; set; } = true;

        public bool ViewOptionPanel { get; set; } = false;

        public bool FullScanData { get; set; } = false;

        public string ProductID { get; set; } = string.Empty;

        public OverviewSearchOption()
        {
            ;//
        }

        public OverviewSearchOption(bool bViewOpPanel,
            bool bFullScanOp,
            string strSetProductID)
        {
            ViewOptionPanel = bViewOpPanel;

            FullScanData = bFullScanOp;

            ProductID = strSetProductID;
        }
    }

    public class TestHistorySearchOption
    {
        public bool Time_SelectedTest { get; set; } = true;
        public bool Time_Day { get; set; } = false;
        public bool Time_WholeRange { get; set; } = false;
        public bool Station { get; set; } = true;
        public bool Host { get; set; } = true;
        public bool Section { get; set; } = true;
        public bool TestType { get; set; } = true;
        public bool ProductNo { get; set; } = true;
        public bool TestCode { get; set; } = false;
        public bool ParentNo { get; set; } = false;
        public bool ProductionCode { get; set; } = false;
        public bool FileVersion { get; set; } = false;
        public bool StepVersion { get; set; } = false;        
    }

    public class SelectedDataCollection : ICloneable
    {
        public DateTime SearchRangeStart { get; set; }
        public DateTime SearchRangeEnd { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public string StationName { get; set; }
        public string HostID { get; set; }
        public string SectionID { get; set; }
        public string TestType { get; set; }
        public string ProductNo { get; set; }
        public string ProductID { get; set; }
        public string TestCode { get; set; }
        public string ParentNo { get; set; }
        public string TestListFileNo { get; set; }
        public string TestListVariantNo { get; set; }
        public string ProductionCode { get; set; }
        public string FileVersion { get; set; }
        public string StepVersion { get; set; }
        public string[] TestRequestID { get; set; }

        private int nUseReqCnt { get; set; }

        public int GetCountRequestID()
        {
            return nUseReqCnt;
        }

        public void SetCountRequestID(int nCnt)
        {
            nUseReqCnt = nCnt;

            TestRequestID = new string[nUseReqCnt];
        }

        public void CopyReqIDs(string[] strReqId)
        {
            Array.Copy(strReqId, 0, TestRequestID, 0, nUseReqCnt);
        }

        public void SetProdcutID(string strProductID)
        {
            ProductID = strProductID;
        }

        public SelectedDataCollection(DateTime dtSRStart, DateTime dtSREnd, DateTime dtStart, DateTime dtEnd, string strStnName, string strHostID, string strSectionID, string strTestType,
                                        string strProdNo, string strTestCode, string strParentNo, string strProdCode,
                                        string strTestListFileNo, string strTestListVariantNo, string strFileVer, string strStepVer, string strReqID)
        {
            nUseReqCnt = 1;

            TestRequestID = new string[nUseReqCnt];

            SearchRangeStart = dtSRStart;
            SearchRangeEnd = dtSREnd;
            StartTime = dtStart;
            EndTime = dtEnd;
            StationName = strStnName;
            HostID = strHostID;
            SectionID = strSectionID;
            TestType = strTestType;
            ProductNo = strProdNo;
            ProductID = string.Empty;
            TestCode = strTestCode;
            ParentNo = strParentNo;
            TestListFileNo = strTestListFileNo;
            TestListVariantNo = strTestListVariantNo;
            ProductionCode = strProdCode;
            FileVersion = strFileVer;
            StepVersion = strStepVer;

            TestRequestID[0] = strReqID;
        }

        public SelectedDataCollection() { }

        public object Clone()
        {
            SelectedDataCollection newSelectData = new SelectedDataCollection();

            newSelectData.SearchRangeStart = this.SearchRangeStart;
            newSelectData.SearchRangeEnd = this.SearchRangeEnd;
            newSelectData.StartTime = this.StartTime;
            newSelectData.EndTime = this.EndTime;
            newSelectData.StationName = this.StationName;
            newSelectData.HostID = this.HostID;
            newSelectData.SectionID = this.SectionID;
            newSelectData.TestType = this.TestType;
            newSelectData.ProductNo = this.ProductNo;
            newSelectData.ProductID = this.ProductID;
            newSelectData.TestCode = this.TestCode;
            newSelectData.ParentNo = this.ParentNo;
            newSelectData.TestListFileNo = this.TestListFileNo;
            newSelectData.TestListVariantNo = this.TestListVariantNo;
            newSelectData.ProductionCode = this.ProductionCode;
            newSelectData.FileVersion = this.FileVersion;
            newSelectData.StepVersion = this.StepVersion;
            newSelectData.TestRequestID = this.TestRequestID;

            return newSelectData;
        }
    }


    public class DetailTestDataCollection
    {
        public long No { get; set; }
        //public string TestID { get; set; }      
        public DateTime StartTime { get; set; }
        public long Duration { get; set; }
        public string StationName { get; set; }
        public string HostID { get; set; }
        public string SectionID { get; set; }
        public string TestType { get; set; }
        public string ProductID { get; set; }
        public string ProductNo { get; set; }
        public string TestCode { get; set; }
        public string ParentNo { get; set; }
        public string FileCode { get; set; }
        public string FileVersion { get; set; }
        public string TestlistFileName { get; set; }
        public string TestListFileNo { get; set; }
        public string TestListVariantNo { get; set; }
        public string StepVersion { get; set; }
        public string TestRequestID { get; set; }
        public string TestResult { get; set; }
    }


    public class TestTrendDataCollection
    {
        public SelectedDataCollection SelectedData { get; set; }
        public DataTable DTTestLogList { get; set; }
        public DataTable DTTestlistSrc { get; set; } 
        public string StepNo { get; set; }
        public string Variant { get; set; }
        public string Gate { get; set; }
        public string MO { get; set; }
        public string Function { get; set; }
        public string Total { get; set; }
        public string OK { get; set; }
        public string NG { get; set; }//NOK { get; set; }
    }

    public class TestHistoryDataCollection
    {

    }


    public static class MyListExtensions
    {
        const int CG_DEV = 6; // the number of standard deviations you wish to include(default is 6)
        public static double Cg(this List<double> values, double dAllowance)
        {
            double dCg = (0.2 * dAllowance) / (CG_DEV * values.StandardDeviation());

            return dCg;
        }

        const int CGK_DEV = 3; // the number of standard deviations you wish to include(default is 3)
        public static double Cgk(this List<double> values, double dAllowance, double dStdVal)
        {
            double dCgk = ((0.1 * dAllowance) - Math.Abs(values.Mean() - dStdVal)) / (CGK_DEV * values.StandardDeviation());

            return dCgk;
        }

        const int CP_DEV = 6; // the number of standard deviations you wish to include(default is 6)
        public static double Cp(this List<double> values, double dAllowance)
        {
            double dCp = (dAllowance) / (CP_DEV * values.StandardDeviation());

            return dCp;
        }

        const int CPK_DEV = 3; // the number of standard deviations you wish to include(default is 3)
        public static double Cpk(this List<double> values, double dUpperLimit, double dLowerLimit)
        {
            double dCpkL = (Math.Abs(values.Mean() - dLowerLimit)) / (CPK_DEV * values.StandardDeviation());
            double dCpkU = (Math.Abs(values.Mean() - dUpperLimit)) / (CPK_DEV * values.StandardDeviation());

            return Math.Min(dCpkL, dCpkU);
        }

        public static double Mean(this List<double> values)
        {
            return values.Count == 0 ? 0 : values.Mean(0, values.Count);
        }

        public static double Mean(this List<double> values, int start, int end)
        {
            double s = 0;

            for (int i = start; i < end; i++)
            {
                s += values[i];
            }

            return s / (end - start);
        }

        public static double Variance(this List<double> values)
        {
            return values.Variance(values.Mean(), 0, values.Count);
        }

        public static double Variance(this List<double> values, double mean)
        {
            return values.Variance(mean, 0, values.Count);
        }

        public static double Variance(this List<double> values, double mean, int start, int end)
        {
            double variance = 0;

            for (int i = start; i < end; i++)
            {
                variance += Math.Pow((values[i] - mean), 2);
            }

            int n = end - start;
            if (start > 0) n -= 1;

            return variance / (n);
        }

        public static double StandardDeviation(this List<double> values)
        {
            return values.Count == 0 ? 0 : values.StandardDeviation(0, values.Count);
        }

        public static double StandardDeviation(this List<double> values, int start, int end)
        {
            double mean = values.Mean(start, end);
            double variance = values.Variance(mean, start, end);

            return Math.Sqrt(variance);
        }
    }
}
