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

namespace ManualSoftwareSystem.Operation_Manual
{
    public partial class FrmInspectionInformation : Form
    {
        private string csvPartFile = string.Empty;
        private string loginuser = string.Empty;
        private List<List<string>> Data = new List<List<string>>();
        private List<List<string>> Master = new List<List<string>>();
        private string csvPartFileOperationInspectionInformation = System.IO.Directory.GetCurrentDirectory() + "\\OperationInspectionInformation.sys";
        private string type = string.Empty;
        public dynamic objLanguage;
        public static string strPathSaveComplete = string.Empty;

        public FrmInspectionInformation(string pathFile, string login, string type)
        {
            InitializeComponent();
            this.csvPartFile = pathFile;
            this.loginuser = login;
            this.type = type;
            this.objLanguage = FrmOperationManualOperator.objLanguage;
            if (csvPartFile != string.Empty)
            {
                strPathSaveComplete = csvPartFile.Substring(0, csvPartFile.Length - 3) + "001.nck";
            }

        }

        private void FrmInspectionInformation_Load(object sender, EventArgs e)
        {

            FrmOperationManualOperator.WeatherShare = string.Empty;
            FrmOperationManualOperator.TemperatureShare = string.Empty;
            FrmOperationManualOperator.HumidityShare = string.Empty;
            FrmOperationManualOperator.RemarksShare = string.Empty;

            if (!File.Exists(csvPartFileOperationInspectionInformation))
            {
                using (StreamWriter sw = File.CreateText(csvPartFileOperationInspectionInformation))
                {
                    sw.WriteLine("");
                    sw.WriteLine("");
                    sw.WriteLine("");
                    sw.WriteLine("");
                }
            }

            string strInspectionInformation = "Inspection information";
            string strPleaseFillOut = "Please fill out inspection information";
            string strWeather = "Weather";
            string strTemperature = "Temperature";
            string strHumidity = "Humidity";
            string strInspectionCommonRemarks = "Inspection common remarks";
            string strAdd = "Add";
            string strDel = "Del";
            string strYes = "Yes";
            string strCancel = "Cancel";


            lblInspectionInformation.Text = objLanguage.Translate(strInspectionInformation);
            lblPleaseFillOut.Text = objLanguage.Translate(strPleaseFillOut);
            lblWeather.Text = objLanguage.Translate(strWeather);
            lblTemperature.Text = objLanguage.Translate(strTemperature);
            lblHumidity.Text = objLanguage.Translate(strHumidity);
            lblInspectionCommonRemarks.Text = objLanguage.Translate(strInspectionCommonRemarks);
            btnYes.Text = objLanguage.Translate(strYes);
            btnCancel.Text = objLanguage.Translate(strCancel);
            btnAddWeather.Text = objLanguage.Translate(strAdd);
            btnDelWeather.Text = objLanguage.Translate(strDel);
            btnAddTemperature.Text = objLanguage.Translate(strAdd);
            btnDelTemperature.Text = objLanguage.Translate(strDel);
            btnAddHumidity.Text = objLanguage.Translate(strAdd);
            btnDelHumidity.Text = objLanguage.Translate(strDel);
            btnAddRemarks.Text = objLanguage.Translate(strAdd);
            btnDelRemarks.Text = objLanguage.Translate(strDel);


            txtLogin.Text = loginuser;

            cmbWeather.Items.Clear();
            cmbTemperature.Items.Clear();
            cmbHumidity.Items.Clear();
            cmbRemarks.Items.Clear();

            List<string> tmp = null;
            Data = new List<List<string>>();
            tmp = null;
            Int32 i = 0;
            using (var csv = new CsvReader(csvPartFileOperationInspectionInformation))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    Data.Add(tmp);

                    if (i == 0)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbWeather.Items.Add(this.Data[i][j]);
                            }

                        }
                    }

                    if (i == 1)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbTemperature.Items.Add(this.Data[i][j]);
                            }

                        }
                    }

                    if (i == 2)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbHumidity.Items.Add(this.Data[i][j]);
                            }

                        }
                    }

                    if (i == 3)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbRemarks.Items.Add(this.Data[i][j]);
                            }

                        }
                    }


                    i = i + 1;
                }

            }//using end

            if (strPathSaveComplete != string.Empty)
            {

                if (File.Exists(strPathSaveComplete))
                {
                    string strLine;
                    string[] strArray;
                    Int32 rowID = 0;
                    StreamReader srCheck = new StreamReader(strPathSaveComplete);
                    strLine = string.Empty;

                    while (srCheck.Peek() >= 0)
                    {
                        strLine = srCheck.ReadLine();
                        if (strLine != null)
                        {
                            strArray = strLine.Split(',');
                            if (strArray.Length > 11)
                            {
                                if (rowID == 1)
                                {
                                    cmbWeather.Text = strArray[11];
                                }
                                if (rowID == 2)
                                {
                                    cmbTemperature.Text = strArray[11];
                                }
                                if (rowID == 3)
                                {
                                    cmbHumidity.Text = strArray[11];
                                    cmbRemarks.Text = strArray[10];
                                }
                            }

                        }
                        rowID = rowID + 1;

                    }

                    srCheck.Close();
                }
            }


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            //FrmMasterMaintenanceDetail frmMasterMaintenanceDetail = new FrmMasterMaintenanceDetail(csvPartFile);
            //frmMasterMaintenanceDetail.ShowDialog();

            FrmOperationManualOperator.WeatherShare = cmbWeather.Text.Trim();
            FrmOperationManualOperator.TemperatureShare = cmbTemperature.Text.Trim();
            FrmOperationManualOperator.HumidityShare = cmbHumidity.Text.Trim();
            FrmOperationManualOperator.RemarksShare = cmbRemarks.Text.Trim();

            if (csvPartFile == string.Empty)
            {
                FrmMain frmMain = new FrmMain(loginuser);

                this.Dispose();

                frmMain.ShowDialog();

                return;
            }

            List<string> tmp = null;
            Master = new List<List<string>>();
            tmp = null;
            Int32 i = 0;
            using (var csv = new CsvReader(csvPartFile))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    Master.Add(tmp);

                    if (tmp.Count >= 14)
                    {
                        //dataGridView1.ColumnCount = 3;
                        //dataGridView1.Columns[0].Name = "Product ID";
                        //dataGridView1.Columns[1].Name = "Product Name";
                        //dataGridView1.Columns[2].Name = "Product Price";
                        //string[] row = new string[] { this.Master[i][0], this.Master[i][1], this.Master[i][2],
                        //this.Master[i][3], this.Master[i][4], this.Master[i][5], this.Master[i][6],
                        //this.Master[i][7], this.Master[i][8], this.Master[i][9], this.Master[i][10],
                        //this.Master[i][11], this.Master[i][12]};

                        string[] row = new string[] { null , null, null, this.Master[i][0], this.Master[i][1], this.Master[i][2],
                        this.Master[i][3], this.Master[i][4], this.Master[i][5], this.Master[i][6],
                        this.Master[i][7], this.Master[i][8], this.Master[i][9], this.Master[i][10],
                        this.Master[i][11] , this.Master[i][12] , this.Master[i][13] };


                        dataGridView1.Rows.Add(row);

                    }

                    i++;

                }

            }//using end

            dataGridView1.SelectAll();

            if (FrmOperationManualOperator.HandingOverShare == false)
            {
                FrmOperationManual frmOperationManual = new FrmOperationManual(true, dataGridView1, csvPartFile, type, loginuser);

                this.Dispose();

                frmOperationManual.ShowDialog();
            }
            else if (FrmOperationManualOperator.HandingOverShare == true)
            {
                this.Close();

            }




        }

        private void btnAddWeather_Click(object sender, EventArgs e)
        {
            if (cmbWeather.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Weather is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbWeather.FindStringExact(cmbWeather.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbWeather.Items.Add(cmbWeather.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbWeather.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbWeather.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbWeather.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInspectionInformation, 1);

            }
        }

        static void lineChanger(string newText, string fileName, int line_to_edit)
        {
            string[] arrLine = File.ReadAllLines(fileName);
            arrLine[line_to_edit - 1] = newText;
            File.WriteAllLines(fileName, arrLine);
        }

        private void btnDelWeather_Click(object sender, EventArgs e)
        {
            if (cmbWeather.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Weather is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbWeather.FindStringExact(cmbWeather.Text.Trim());

            if (index > -1)
            {
                cmbWeather.Items.RemoveAt(index);

                if (cmbWeather.Items.Count == 0)
                {
                    cmbWeather.Items.Clear();
                    cmbWeather.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbWeather.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbWeather.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbWeather.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInspectionInformation, 1);
            }
        }

        private void btnOrderAtoZWeather_Click(object sender, EventArgs e)
        {
            cmbWeather.Sorted = true;
        }

        private void btnOrderZtoAWeather_Click(object sender, EventArgs e)
        {
            cmbWeather.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbWeather.Items.Count; i++)
            {

                TempList.Add(cmbWeather.Items[i].ToString());

            }

            List<string> sortedDetailedComment = TempList.OrderByDescending(DetailedComment => DetailedComment).ToList();
            cmbWeather.Items.Clear();
            foreach (string DetailedCommentName in sortedDetailedComment)
                cmbWeather.Items.Add(DetailedCommentName);
        }

        private void btnAddTemperature_Click(object sender, EventArgs e)
        {
            if (cmbTemperature.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Temperature is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbTemperature.FindStringExact(cmbTemperature.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbTemperature.Items.Add(cmbTemperature.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbTemperature.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbTemperature.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbTemperature.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInspectionInformation, 2);

            }
        }

        private void btnDelTemperature_Click(object sender, EventArgs e)
        {
            if (cmbTemperature.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Temperature is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbTemperature.FindStringExact(cmbTemperature.Text.Trim());

            if (index > -1)
            {
                cmbTemperature.Items.RemoveAt(index);

                if (cmbTemperature.Items.Count == 0)
                {
                    cmbTemperature.Items.Clear();
                    cmbTemperature.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbTemperature.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbTemperature.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbTemperature.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInspectionInformation, 2);
            }
        }

        private void btnOrderAtoZTemperature_Click(object sender, EventArgs e)
        {
            cmbTemperature.Sorted = true;
        }

        private void btnOrderZtoATemperature_Click(object sender, EventArgs e)
        {
            cmbTemperature.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbTemperature.Items.Count; i++)
            {

                TempList.Add(cmbTemperature.Items[i].ToString());

            }

            List<string> sortedActionContent = TempList.OrderByDescending(ActionContent => ActionContent).ToList();
            cmbTemperature.Items.Clear();
            foreach (string ActionContentName in sortedActionContent)
                cmbTemperature.Items.Add(ActionContentName);
        }

        private void btnAddHumidity_Click(object sender, EventArgs e)
        {
            if (cmbHumidity.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Humidity is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbHumidity.FindStringExact(cmbHumidity.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbHumidity.Items.Add(cmbHumidity.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbHumidity.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbHumidity.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbHumidity.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInspectionInformation, 3);

            }
        }

        private void btnDelHumidity_Click(object sender, EventArgs e)
        {
            if (cmbHumidity.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Humidity is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbHumidity.FindStringExact(cmbHumidity.Text.Trim());

            if (index > -1)
            {
                cmbHumidity.Items.RemoveAt(index);

                if (cmbHumidity.Items.Count == 0)
                {
                    cmbHumidity.Items.Clear();
                    cmbHumidity.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbHumidity.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbHumidity.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbHumidity.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInspectionInformation, 3);
            }
        }

        private void btnOrderAtoZHumidity_Click(object sender, EventArgs e)
        {
            cmbHumidity.Sorted = true;
        }

        private void btnOrderZtoAHumidity_Click(object sender, EventArgs e)
        {
            cmbHumidity.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbHumidity.Items.Count; i++)
            {

                TempList.Add(cmbHumidity.Items[i].ToString());

            }

            List<string> sortedActionContent = TempList.OrderByDescending(ActionContent => ActionContent).ToList();
            cmbHumidity.Items.Clear();
            foreach (string ActionContentName in sortedActionContent)
                cmbHumidity.Items.Add(ActionContentName);
        }

        private void btnAddRemarks_Click(object sender, EventArgs e)
        {
            if (cmbRemarks.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Inspection common remarks is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbRemarks.FindStringExact(cmbRemarks.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbRemarks.Items.Add(cmbRemarks.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbRemarks.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbRemarks.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbRemarks.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInspectionInformation, 4);

            }
        }

        private void btnDelRemarks_Click(object sender, EventArgs e)
        {
            if (cmbRemarks.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Inspection common remarks is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbRemarks.FindStringExact(cmbRemarks.Text.Trim());

            if (index > -1)
            {
                cmbRemarks.Items.RemoveAt(index);

                if (cmbRemarks.Items.Count == 0)
                {
                    cmbRemarks.Items.Clear();
                    cmbRemarks.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbRemarks.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbRemarks.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbRemarks.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileOperationInspectionInformation, 4);
            }
        }

        private void btnOrderAtoZRemarks_Click(object sender, EventArgs e)
        {
            cmbRemarks.Sorted = true;
        }

        private void btnOrderZtoARemarks_Click(object sender, EventArgs e)
        {
            cmbRemarks.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbRemarks.Items.Count; i++)
            {

                TempList.Add(cmbRemarks.Items[i].ToString());

            }

            List<string> sortedActionContent = TempList.OrderByDescending(ActionContent => ActionContent).ToList();
            cmbRemarks.Items.Clear();
            foreach (string ActionContentName in sortedActionContent)
                cmbRemarks.Items.Add(ActionContentName);
        }

        private void cmbWeather_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbWeather_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbTemperature_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbHumidity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbRemarks_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbWeather_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {

        }

        private void cmbWeather_KeyDown(object sender, KeyEventArgs e)
        {

        }

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