using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using SystemX.Product.TRA.DataManager;
using static SystemX.Product.CP.TRA.MainForm;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraPrinting;
using SystemX.Product.ALIS.Interface;
using static SystemX.Product.CP.TRA.Commons;

namespace SystemX.Product.TRA.UIControl
{
    public partial class UcTRAProdHistHost : UcTRABaseView
    {
        public enum eColHide
        {
            TestListNo,
            //TestID,
            LogNo,
            LogCount,
            AccessStart,
            AccessEnd,
            TestlistFile
        }

        DMTestHistory DataManager { get; set; }
        TestHistorySearchOption Option { get; set; }
        SelectedDataCollection SelectedData { get; set; }

        public DataTable DataResult { get; private set; }

        public delegate void evtDataDetailViewHandler(DetailTestDataCollection data);
        public event evtDataDetailViewHandler OnDetailSelect;

        private string GridViewGetActiveFilterText;

        public UcTRAProdHistHost()
        {
            ContentsType = eContents.ProductHistory;

            InitializeComponent();
        }

        public void SetSQLConnection(eSelectDataView SelectView, IDataBaseController getDBController)
        {
            DataManager = new DMTestHistory(SelectView, getDBController);
        }

        public void ViewData(SelectedDataCollection data)
        {
            if (data == null)
                return;

            SelectedData = data;

            DataTable dtResult = DataManager.SearchTestHistory(data, Option);

            GridViewGetActiveFilterText = gridViewMain.ActiveFilterString;

            DataResult = dtResult;

            gridControlMain.BeginInit();

            gridControlMain.DataSource = dtResult;

            gridControlMain.Update();                        
            gridControlMain.RefreshDataSource();
            gridControlMain.ForceInitialize();
            gridViewMain.PopulateColumns(dtResult);
            
            foreach (string strColName in Enum.GetNames(typeof(eColHide)))
                gridViewMain.Columns[strColName].Visible = false;

            gridControlMain.EndInit();

            gridViewMain.ActiveFilterString = GridViewGetActiveFilterText;
        }

        public DetailTestDataCollection GetSearchInfo(DataRow dtRow)
        {
            DetailTestDataCollection drDetail = new DetailTestDataCollection();

            //drDetail.TestID = dtRow[DMTestHistory.eColList.TestID.ToString()].ToString();
            drDetail.StartTime = DateTime.Parse(dtRow[DMTestHistory.eColList.TestDateTime.ToString()].ToString());

            string strGetLogNo = dtRow[DMTestHistory.eColList.LogNo.ToString()].ToString();
            if (strGetLogNo.Length > 0)
                drDetail.LogNo = Convert.ToUInt64(dtRow[DMTestHistory.eColList.LogNo.ToString()]);
            else
                drDetail.LogNo = ulong.MinValue;

            drDetail.LogCount = Convert.ToInt32(dtRow[DMTestHistory.eColList.LogCount.ToString()]);
            drDetail.AccessStart = Convert.ToUInt64(dtRow[DMTestHistory.eColList.AccessStart.ToString()]);
            drDetail.AccessEnd = Convert.ToUInt64(dtRow[DMTestHistory.eColList.AccessEnd.ToString()]);
            drDetail.Duration =  Convert.ToInt64(dtRow[DMTestHistory.eColList.Duration.ToString()]);
            drDetail.StationName = dtRow[DMTestHistory.eColList.StationName.ToString()].ToString();
            drDetail.HostID = dtRow[DMTestHistory.eColList.Host.ToString()].ToString();
            drDetail.SectionID = dtRow[DMTestHistory.eColList.Section.ToString()].ToString();
            drDetail.TestType = dtRow[DMTestHistory.eColList.TestType.ToString()].ToString();
            drDetail.ProductID = dtRow[DMTestHistory.eColList.ProductID.ToString()].ToString();
            drDetail.ProductNo = dtRow[DMTestHistory.eColList.ProductNo.ToString()].ToString();
            drDetail.TestCode = dtRow[DMTestHistory.eColList.TestCode.ToString()].ToString();
            drDetail.ParentNo = dtRow[DMTestHistory.eColList.ParentNo.ToString()].ToString();
            drDetail.FileCode = dtRow[DMTestHistory.eColList.FileCode.ToString()].ToString();
            drDetail.FileVersion = dtRow[DMTestHistory.eColList.FileVersion.ToString()].ToString();
            drDetail.TestlistFileName = dtRow[DMTestHistory.eColList.TestlistFileName.ToString()].ToString();
            drDetail.TestListFileNo = dtRow[DMTestHistory.eColList.TestListFileNo.ToString()].ToString();
            drDetail.TestListVariantNo = dtRow[DMTestHistory.eColList.TestListVariantNo.ToString()].ToString();
            drDetail.StepVersion = dtRow[DMTestHistory.eColList.StepVersion.ToString()].ToString();
            drDetail.TestRequestID = dtRow[DMTestHistory.eColList.TestlistReqID.ToString()].ToString();
            drDetail.TestResult = dtRow[DMTestHistory.eColList.TestResult.ToString()].ToString();

            return drDetail;
        }

        private void UcTRATestHistHost_Load(object sender, EventArgs e)
        {
            Option = new TestHistorySearchOption();

            FrmOptionStationSearch dlgSearch = new FrmOptionStationSearch();

            dlgSearch.ShowDialog();

            if (dlgSearch.DialogResult == DialogResult.OK)
                Option = dlgSearch.GetSelectedResult();
        }

        private void gridViewMain_DoubleClick(object sender, EventArgs e)
        {
            DXMouseEventArgs ea = e as DXMouseEventArgs;
            GridView view = sender as GridView;
            GridHitInfo info = view.CalcHitInfo(ea.Location);

            if (info.InRow || info.InRowCell)
            {
                int nRow = int.MinValue;
                nRow = info.RowHandle;

                if (nRow < 0)
                    return;

                var dvRow = view.GetRow(nRow);
                DataRow dtRow = (dvRow as DataRowView).Row;
                DetailTestDataCollection drDetail = GetSearchInfo(dtRow);

                OnDetailSelect(drDetail);
            }
        }

        public List<DetailTestDataCollection> GetResultContents()
        {
            List<DetailTestDataCollection> vDetailData = new List<DetailTestDataCollection>();
            GridView view = gridViewMain as GridView;

            for(int i=0; i<view.RowCount; i++)
            {
                DataRow dtRow = (view.GetRow(i) as DataRowView).Row;
                DetailTestDataCollection dtContent = GetSearchInfo(dtRow);

                vDetailData.Add(dtContent);
            }

            return vDetailData;
        }
        
        public string CreateExportFileName()
        {
            string strFileName = $"{this.ContentsType.ToString()}_{SelectedData.ProductNo}_{SelectedData.TestCode}_{SelectedData.StationName}";

            strFileName += $"_{SelectedData.StartTime.ToString("yyyyMMddHHmmss")}-{SelectedData.EndTime.ToString("yyyyMMddHHmmss")}";

            return strFileName;
        }

        public override void ExportData()
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {

                string strFileName = CreateExportFileName();
                string strExpPath = dlg.SelectedPath + "\\" + strFileName;
                XlsxExportOptionsEx option = new XlsxExportOptionsEx();
                string strFile = strExpPath + ".xlsx";

                option.ShowGridLines = true;
                option.ExportType = DevExpress.Export.ExportType.WYSIWYG;
                option.ExportMode = XlsxExportMode.SingleFile;
                option.SheetName = "Test History";
                gridControlMain.ExportToXlsx(strFile, option);
                
            }
        }
    }
}
