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.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManualSoftwareSystem
{
    public partial class FrmOperationManualInput : 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 csvPartFileOperationManualInput = System.IO.Directory.GetCurrentDirectory() + "\\OperationManualInput.sys";
        public dynamic objLanguage;
        private string type = string.Empty;

        public FrmOperationManualInput(string pathFile, string login, string type)
        {
            InitializeComponent();
            this.csvPartFile = pathFile;
            this.loginuser = login;
            this.type = type;
            this.objLanguage = FrmOperationManualOperator.objLanguage;
        }

        private void FrmOperationManualInput_Load(object sender, EventArgs e)
        {
            if (!File.Exists(csvPartFileOperationManualInput))
            {
                using (StreamWriter sw = File.CreateText(csvPartFileOperationManualInput))
                {
                    sw.WriteLine("");
                    sw.WriteLine("");
                    sw.WriteLine("");
                }
            }

            string strInputInformation = "Input information";
            string strName = "Name";
            string strInformation = "Information";
            string strRemark = "Remark";
            string strAdd = "Add";
            string strDel = "Del";
            string strOK = "OK";
            string strCancel = "Cancel";


            lblInputInformation.Text = objLanguage.Translate(strInputInformation);
            lblName.Text = objLanguage.Translate(strName);
            lblInformation.Text = objLanguage.Translate(strInformation);
            lblRemark.Text = objLanguage.Translate(strRemark);
            btnOK.Text = objLanguage.Translate(strOK);
            btnCancel.Text = objLanguage.Translate(strCancel);
            btnAddName.Text = objLanguage.Translate(strAdd);
            btnDelName.Text = objLanguage.Translate(strDel);
            btnAddInformation.Text = objLanguage.Translate(strAdd);
            btnDelInformation.Text = objLanguage.Translate(strDel);
            btnAddRemark.Text = objLanguage.Translate(strAdd);
            btnDelRemark.Text = objLanguage.Translate(strDel);




            cmbName.Items.Clear();
            cmbInformation.Items.Clear();
            cmbRemark.Items.Clear();

            List<string> tmp = null;
            Data = new List<List<string>>();
            tmp = null;
            Int32 i = 0;
            using (var csv = new CsvReader(csvPartFileOperationManualInput))
            {
                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)
                            {
                                cmbName.Items.Add(this.Data[i][j]);
                            }

                        }
                    }

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

                        }
                    }

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

                        }
                    }


                    i = i + 1;
                }

            }//using end

        }

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


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

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

                string strTemp = string.Empty;

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

                }

                lineChanger(strTemp, csvPartFileOperationManualInput, 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 btnDelName_Click(object sender, EventArgs e)
        {
            if (cmbName.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Name is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

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

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

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

                string strTemp = string.Empty;

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

                }

                lineChanger(strTemp, csvPartFileOperationManualInput, 1);
            }
        }

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

        private void btnOrderZtoAName_Click(object sender, EventArgs e)
        {
            cmbName.Sorted = false;

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

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

            }

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

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


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

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

                string strTemp = string.Empty;

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

                }

                lineChanger(strTemp, csvPartFileOperationManualInput, 2);

            }
        }

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

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

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

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

                string strTemp = string.Empty;

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

                }

                lineChanger(strTemp, csvPartFileOperationManualInput, 2);
            }
        }

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

        private void btnOrderZtoAInformation_Click(object sender, EventArgs e)
        {
            cmbInformation.Sorted = false;

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

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

            }

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

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


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

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

                string strTemp = string.Empty;

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

                }

                lineChanger(strTemp, csvPartFileOperationManualInput, 3);

            }
        }

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

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

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

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

                string strTemp = string.Empty;

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

                }

                lineChanger(strTemp, csvPartFileOperationManualInput, 3);
            }
        }

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

        private void btnOrderZtoARemark_Click(object sender, EventArgs e)
        {
            cmbRemark.Sorted = false;

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

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

            }

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

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

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

            }
        }
    }
}
