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;

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; }


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

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

                LongTermDBConn = getDBController.GetLongTermConn1();
            }
            else if (SelectView == eSelectDataView.DataDocumentViewC2)
            {
                ShortTermDBConn = getDBController.GetShortTermConn2();

                LongTermDBConn = getDBController.GetLongTermConn2();
            }
        }

        public DataTable SearchTestResult(DateTime dtTest, ulong nAccStart, ulong nAccEnd)
        {
            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;
            bool bLongTermTableUse = nDiffMonth > 3 ? true : false;

            strQuery += $"SELECT ";
            strQuery += $"*";
            strQuery += " ";
            strQuery += $"from {strTableTerm} ";
            strQuery += $"WITH(NOLOCK) ";
            strQuery += $"where AccessKey >= {nAccStart} and AccessKey <= {nAccEnd}";
            strQuery += " ";
            strQuery += $"order by ";
            strQuery += $"[StepID]";
            strQuery += ";";

            if (bLongTermTableUse) 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;
            bool bLongTermTableUse = nDiffMonth > 3 ? true : false;

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

            int nIdx = 0;

            foreach(ulong nAccKey in vnData)
            {
                strQuery += $"AccessKey = {nAccKey}";

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

                nIdx++;
            }

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

            if (bLongTermTableUse) 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;
        }
    }
}
