using ManualSoftwareSystem.lib;
using ManualSoftwareSystem.Operation_Manual;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManualSoftwareSystem.Work_History
{
    public partial class FrmWorkHistoryViewer : Form
    {
        private List<List<string>> Master = new List<List<string>>();
        private string csvPartFile = string.Empty;
        public dynamic objLanguage;

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



        private void FrmWorkHistoryViewer_Load(object sender, EventArgs e)
        {
            string strWorkHistoryViewer = "Work History Viewer";

            lblWorkHistoryViewer.Text = objLanguage.Translate(strWorkHistoryViewer);

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

                }

                Int32 j = 0;
                Int32 k = 0;
                for (int i = 0; i < colMax; i++)
                {
                    switch (i)
                    {
                        case 0:
                            //dt.Columns.Add(objLanguage.Translate("Execution name"));
                            dt.Columns.Add(objLanguage.Translate("Inspection results file name"));
                            break;
                        case 1:
                            dt.Columns.Add(objLanguage.Translate("Start item") + " (Sor1~)");
                            break;
                        case 2:
                            dt.Columns.Add(objLanguage.Translate("End item") + " (Eor1~)");
                            break;
                        case 3:
                            dt.Columns.Add(objLanguage.Translate("Information 1"));
                            break;
                        case 4:
                            dt.Columns.Add(objLanguage.Translate("Information 2"));
                            break;
                        case 5:
                            dt.Columns.Add(objLanguage.Translate("Information 3"));
                            break;
                        case 6:
                            dt.Columns.Add(objLanguage.Translate("Start date"));
                            break;
                        case 7:
                            dt.Columns.Add(objLanguage.Translate("Start time"));
                            break;
                        case 8:
                            dt.Columns.Add(objLanguage.Translate("End date"));
                            break;
                        case 9:
                            dt.Columns.Add(objLanguage.Translate("End time"));
                            break;
                        case 10:
                            dt.Columns.Add(objLanguage.Translate("All"));
                            break;
                        default:
                            if (i % 2 == 1)
                            {
                                dt.Columns.Add("S" + (i - 10 - j).ToString());
                                j++;
                                break;
                            }
                            else
                            {
                                dt.Columns.Add("S" + (i - 11 - k).ToString() + "SBNEF");
                                k++;
                                break;
                            }
                    }

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

                StreamReader sr1 = new StreamReader(filePath);
                while (sr1.Peek() >= 0)
                {
                    strLine = sr1.ReadLine();
                    strArray = strLine.Split(',');
                    dt.Rows.Add(strArray);
                }
            }
            return dt;
        }

        private void dataGridView1_RowPostPaint_1(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);
        }
    }
}
