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 Excel = Microsoft.Office.Interop.Excel;

namespace ManualSoftwareSystem.Inspection_Result
{
    public partial class FrmInspectionResult : Form
    {
        public dynamic objLanguage;

        private string csvPartFile = string.Empty;

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

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

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

            if (dataGridView1.Rows.Count == 19)
            {
                if (dataGridView1.Columns.Count == 12)
                {
                    dataGridView1.Rows[18].Cells[11].Style.BackColor = Color.Aqua;
                    dataGridView1.Rows[18].Cells[9].Style.BackColor = Color.Aqua;
                }
            }
        }

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

                }

                for (int i = 0; i < colMax; i++)
                {
                    switch (i)
                    {
                        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);
                Int32 k = 0;
                while (sr1.Peek() >= 0)
                {
                    strLine = sr1.ReadLine();
                    if (strLine != null)
                    {
                        if (strLine.Trim() != string.Empty)
                        {
                            strArray = strLine.Split(',');
                            if (k == 0)
                            {
                                strArray = strArray.SkipLast(1).ToArray();
                            }
                            if (k == 2)
                            {
                                strArray = strArray.SkipLast(1).ToArray();
                            }

                            dt.Rows.Add(strArray);
                        }
                    }
                    k = k + 1;
                }
            }
            return dt;
        }

        private void btnCSV_Click(object sender, EventArgs e)
        {
            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;
        }

    }
}
