using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemX.Product.ALIS.Interface;
using SystemX.Product.TRA.UIControl;

using static SystemX.Product.TRA.DataManager.DMOverview;
using static SystemX.Product.CP.TRA.Commons;
using DataBaseConnection.Control;

namespace SystemX.Product.TRA.DataManager
{
    public class DMStepTrend
    {

        public SqlConnection DBConn { get; private set; }
        public SqlConnection ShortTermDBConn { get; private set; }
        public SqlConnection LongTermDBConn { get; private set; }

        private SqlCommand SQLCmd { get; set; }

        eSelectDataView TrendSelectView { get; set; }

        IDataBaseController GetDBController;

        public DMStepTrend(eSelectDataView SelectView, IDataBaseController getDBController, DateTime dtStart)
        {
            GetDBController = getDBController;
            DBConn = getDBController.GetMainConn();

            if (SelectView == eSelectDataView.DataDocumentViewC1)
            {
                TrendSelectView = eSelectDataView.DataDocumentViewC1;
                ShortTermDBConn = getDBController.GetShortTermConn1();

                LongTermDBConn = getDBController.GetLongTermConn1().Where(x=>x.Key.Contains(dtStart.Year.ToString())).First().Value;
            }
            else if (SelectView == eSelectDataView.DataDocumentViewC2)
            {
                TrendSelectView = eSelectDataView.DataDocumentViewC2;
                ShortTermDBConn = getDBController.GetShortTermConn2();

                LongTermDBConn = getDBController.GetLongTermConn2().Where(x => x.Key.Contains(dtStart.Year.ToString())).First().Value;
            }
        }

        public DataTable SearchTestResult(DateTime dtTest)
        {
            if (TrendSelectView == eSelectDataView.DataDocumentViewC1)
            {
                TrendSelectView = eSelectDataView.DataDocumentViewC1;
                ShortTermDBConn = GetDBController.GetShortTermConn1();

                LongTermDBConn = GetDBController.GetLongTermConn1().Where(x => x.Key.Contains(dtTest.Year.ToString())).First().Value;
            }
            else if (TrendSelectView == eSelectDataView.DataDocumentViewC2)
            {
                TrendSelectView = eSelectDataView.DataDocumentViewC2;
                ShortTermDBConn = GetDBController.GetShortTermConn2();

                LongTermDBConn = GetDBController.GetLongTermConn2().Where(x => x.Key.Contains(dtTest.Year.ToString())).First().Value;
            }

            DataTable dtResult = new DataTable();
            string strQuery = string.Empty;
            //string strLongTerm = "HIST_TestResult";
            //string strShortTerm = "HIST_TestResult";

            //DateTime dtNow = DateTime.Now;

            //int nDiffMonth = 12 * (dtNow.Year - dtTest.Year) + (dtNow.Month - dtTest.Month);

            //string strTableTerm = nDiffMonth > 3 ? strLongTerm : strShortTerm;            

            strQuery += $"SELECT ";
            strQuery += $"*";
            strQuery += " ";
            strQuery += $"from {DMCommon.ResultLogTable} ";
            strQuery += $"WITH(NOLOCK) ";           
            strQuery += $"order by ";
            strQuery += $"[No]";
            strQuery += ";";

            if (DatabaseConnControl.ScanLongTermLog) SQLCmd = new SqlCommand(strQuery, LongTermDBConn);
            else SQLCmd = new SqlCommand(strQuery, ShortTermDBConn);
            SQLCmd.CommandTimeout = DMCommon.nDefaultScanTime;

            DbDataReader dtReader = SQLCmd.ExecuteReader();

            dtResult.Load(dtReader);

            dtReader.Close();

            return dtResult;
        }

        public DataTable GetTestStepResults(DateTime dtTest, List<ulong> vnData)
        {

            DataTable dtResult = new DataTable();
            string strQuery = string.Empty;
            //string strLongTerm = "HIST_TestResult";
            //string strShortTerm = "HIST_TestResult";

            //DateTime dtNow = DateTime.Now;

            //int nDiffMonth = 12 * (dtNow.Year - dtTest.Year) + (dtNow.Month - dtTest.Month);

            //string strTableTerm = nDiffMonth > 3 ? strLongTerm : strShortTerm;          

            strQuery += $"SELECT ";
            strQuery += $"*";
            strQuery += " ";
            strQuery += $"from {DMCommon.ResultLogTable} ";
            strQuery += $"WITH(NOLOCK) ";
            strQuery += $"where ";

            int nIdx = 0;

            foreach (ulong no in vnData)
            {
                strQuery += $"No = {no}";

                if (nIdx < vnData.Count - 1)
                    strQuery += " or ";

                nIdx++;
            }

            strQuery += " ";
            strQuery += $"order by ";
            strQuery += $"[No]";
            strQuery += ";";

            if (DatabaseConnControl.ScanLongTermLog) SQLCmd = new SqlCommand(strQuery, LongTermDBConn);
            else SQLCmd = new SqlCommand(strQuery, ShortTermDBConn);
            SQLCmd.CommandTimeout = DMCommon.nDefaultScanTime;

            DbDataReader dtReader = SQLCmd.ExecuteReader();

            dtResult.Load(dtReader);

            dtReader.Close();

            return dtResult;
        }
        
        DataTable GetRawResult(DetailTestDataCollection data)
        {
            DataTable dtResult = new DataTable();
            string strQuery = string.Empty;

            if (data == null)
                return dtResult;
                        

            SQLCmd = new SqlCommand(strQuery, DBConn);
            SQLCmd.CommandTimeout = DMCommon.nDefaultScanTime;

            DbDataReader dtReader = SQLCmd.ExecuteReader();

            dtResult.Load(dtReader);

            dtReader.Close();

            return dtResult;
        }
    }
}
