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 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.MainForm;
using static SystemX.Product.CP.TRA.Commons;

namespace SystemX.Product.TRA.UIControl
{
    public partial class UcTRAProductHist : UcTRABaseView
    {
        public enum eColHide
        {
            TestlistNo,          
            TestlistFile
        }

        DMProductHistory DataManager { get; set; }
        TestHistorySearchOption Option { get; set; }
        SelectedDataCollection SelectedData { get; set; }
        public DataTable DataResult { get; private set; }

        public delegate void evtStepTrendViewHandler(TestTrendDataCollection data);
        public event evtStepTrendViewHandler OnHistorySelect;

        private string GridViewGetActiveFilterText;
        public UcTRAProductHist()
        {
            ContentsType = eContents.ProductHistory;

            InitializeComponent();
        }

        public void SetSQLConnection(eSelectDataView SelectView, IDataBaseController getDBController)
        {
            DataManager = new DMProductHistory(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;
        }       
        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;

                //GetSearchInfo(dtRow);
            }
        }
        
        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);
                
            }
        }
    }
}
