using ManualSoftwareSystem.Operation_Manual;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;

namespace ManualSoftwareSystem.Search_Function
{
    public partial class FrmSearchViewer : Form
    {
        public dynamic objLanguage;

        private string csvPartFile = string.Empty;

        public FrmSearchViewer(string pathFile)
        {
            InitializeComponent();
            this.csvPartFile = pathFile;
            this.objLanguage = FrmOperationManualOperator.objLanguage;
        }

        private void FrmSearchViewer_Load(object sender, EventArgs e)
        {
            string strInspectionResult = "Inspection Result";

            lblInspectionResult.Text = objLanguage.Translate(strInspectionResult);


            DataTable table = readCSV(csvPartFile);

            dataGridView1.DataSource = table;

            //dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }

        }

        public DataTable readCSV(string filePath)
        {
            DataTable dt = new DataTable();
            using (StreamReader sr = new StreamReader(filePath))
            {
                string strLine;
                string[] strArray;
                Int32 colMax = 0;

                StreamReader srCheck = new StreamReader(filePath);
                strLine = string.Empty;

                while (srCheck.Peek() >= 0)
                {
                    strLine = srCheck.ReadLine();
                    if (strLine != null)
                    {
                        strArray = strLine.Split(',');
                        if (strArray.Length > colMax)
                        {
                            colMax = strArray.Length;
                        }
                    }

                }

                for (int i = 0; i < colMax; i++)
                {
                    switch (i)
                    {
                        //case 0:
                        //    dt.Columns.Add(objLanguage.Translate("File"));
                        //    break;
                        //case 1:
                        //    dt.Columns.Add(objLanguage.Translate("Page"));
                        //    break;
                        //case 2:
                        //    dt.Columns.Add(objLanguage.Translate("User"));
                        //    break;
                        //case 3:
                        //    dt.Columns.Add(objLanguage.Translate("Date"));
                        //    break;
                        //case 4:
                        //    dt.Columns.Add(objLanguage.Translate("Time"));
                        //    break;
                        //case 5:
                        //    dt.Columns.Add(objLanguage.Translate("Detailed comment"));
                        //    break;
                        //case 6:
                        //    dt.Columns.Add(objLanguage.Translate("Action content"));
                        //    break;
                        //case 7:
                        //    dt.Columns.Add(objLanguage.Translate("Weather"));
                        //    break;
                        //case 8:
                        //    dt.Columns.Add(objLanguage.Translate("Temperature"));
                        //    break;
                        //case 9:
                        //    dt.Columns.Add(objLanguage.Translate("Humidity"));
                        //    break;
                        //case 10:
                        //    dt.Columns.Add(objLanguage.Translate("Remarks"));
                        //    break;
                        //default:
                        //    dt.Columns.Add(objLanguage.Translate(i.ToString()));
                        //    break;


                        case 0:
                            dt.Columns.Add("0");
                            break;
                        case 1:
                            dt.Columns.Add("1");
                            break;
                        case 2:
                            dt.Columns.Add("2");
                            break;
                        case 3:
                            dt.Columns.Add("3");
                            break;
                        case 4:
                            dt.Columns.Add("4");
                            break;
                        case 5:
                            dt.Columns.Add("5");
                            break;
                        case 6:
                            dt.Columns.Add("6");
                            break;
                        case 7:
                            dt.Columns.Add("7");
                            break;
                        case 8:
                            dt.Columns.Add("8");
                            break;
                        case 9:
                            dt.Columns.Add("9");
                            break;
                        case 10:
                            dt.Columns.Add("10");
                            break;
                        default:
                            dt.Columns.Add(i.ToString());
                            break;

                    }
                }
                //DataRow dr = dt.NewRow();

                StreamReader sr1 = new StreamReader(filePath);
                while (sr1.Peek() >= 0)
                {
                    strLine = sr1.ReadLine();
                    if (strLine != null)
                    {
                        if (strLine.Trim() != string.Empty)
                        {
                            strArray = strLine.Split(',');
                            dt.Rows.Add(strArray);
                        }
                    }

                }
            }
            return dt;
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var grid = sender as DataGridView;
            var rowIdx = (e.RowIndex + 1).ToString();

            var centerFormat = new StringFormat()
            {
                // right alignment might actually make more sense for numbers
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };

            var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
            e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
        }

        private void btnCSV_Click(object sender, EventArgs e)
        {
            //ListViewToCSV(lvData, csvPath, false);

            OpenCSVWithExcel(csvPartFile);
        }

        static void OpenCSVWithExcel(string path)
        {
            var ExcelApp = new Excel.Application();
            ExcelApp.Workbooks.OpenText(path, Comma: true);

            ExcelApp.WindowState = Excel.XlWindowState.xlMaximized;
            ExcelApp.Visible = true;
        }

        //public static void ListViewToCSV(ListView listView, string filePath, bool includeHidden)
        //{
        //    string csvFileName = filePath + DateTime.Now.ToString("yyyy-MM-dd-hh.mm.ss.ffffff") + ".csv";

        //    //make header string
        //    StringBuilder result = new StringBuilder();
        //    WriteCSVRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listView.Columns[i].Text);

        //    //export data rows
        //    foreach (ListViewItem listItem in listView.Items)
        //    {
        //        WriteCSVRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listItem.SubItems[i].Text);
        //    }

        //    File.WriteAllText(csvFileName, result.ToString());

        //    var openCSVFile = MessageBox.Show("Export Complete. CSV file saved as: " + csvFileName + ". \n\n Open File Now?", "CSV Exported", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
        //    if (openCSVFile == DialogResult.Yes)
        //    {
        //        // NEED TO OPEN THE CSV FILE IN EXCEL....?
        //        File.Open(csvFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        //    }
        //}

        //private static void WriteCSVRow(StringBuilder result, int itemsCount, Func<int, bool> isColumnNeeded, Func<int, string> columnValue)
        //{
        //    bool isFirstTime = true;
        //    for (int i = 0; i < itemsCount; i++)
        //    {
        //        if (!isColumnNeeded(i))
        //            continue;

        //        if (!isFirstTime)
        //            result.Append(",");
        //        isFirstTime = false;

        //        result.Append(String.Format("\"{0}\"", columnValue(i)));
        //    }
        //    result.AppendLine();
        //}

    }
}
