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 OrderSheetSystem.lib;
using OrderSheetSystem.model;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace OrderSheetSystem
{
    public partial class FrmCustomer : Form
    {
        //private List<List<string>> Customer = new List<List<string>>();
        
        
        public FrmCustomer()
        {
            InitializeComponent();

            
        }

        

        private void FrmCustomer_Load(object sender, EventArgs e)
        {
            List<string> tmp = null;            
            List<CustomerMaster> Customer = new List<CustomerMaster>();
            Int32 i = 0;
            tmp = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\Customer.csv"))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    //Customer.Add(tmp);

                    if (tmp[0] != "")
                    {
                        //    Customer.Add(new CustomerMaster() { Id = i + 1, Customer = tmp[0] });

                        //    i++;

                        tmp.Sort();

                        for (int i1 = 0; i1 < tmp.Count(); i1++)
                        {
                            Customer.Add(new CustomerMaster() { No = i + 1, Customer = tmp[i] });
                            i++;
                        }
                    }

                }



            }//using end



            dgvCustomerList.DataSource = Customer;
            dgvCustomerList.Columns[1].Width = 600;
            dgvCustomerList.Columns[0].HeaderText = "No.";
            dgvCustomerList.Columns[1].SortMode = DataGridViewColumnSortMode.Automatic;

            //dgvCustomerList.Sort(dgvCustomerList.Columns[1], ListSortDirection.Ascending);

            if (dgvCustomerList.RowCount > 0)
            {
                string data = (string)dgvCustomerList[1, 0].Value;

                txtCustomer.Text = data;
            }



        }

        private void dgvCustomerList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgvCustomerList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                txtCustomer.Text = dgvCustomerList[1, e.RowIndex].Value.ToString();
            }
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            List<string> tmp = null;
            //Customer = new List<List<string>>();
            List<CustomerMaster> Customer = new List<CustomerMaster>();
            List<string> tmp11 = new List<string>();
            Int32 i = 0;
            //string[] tmpCustomer;
            tmp = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\Customer.csv"))
            {
                while ((tmp = csv.ReadRow()) != null)
                {

                    //Customer.Add(tmp);

                    //if (tmp[0] != "")
                    //{
                    //tmpCustomer = tmp[i].Split(',');

                    tmp.Sort();

                    for (int i1 = 0; i1 < tmp.Count(); i1++)
                    {
                        Customer.Add(new CustomerMaster() { No = i1 + 1, Customer = tmp[i1] });
                        tmp11.Add(tmp[i1]);
                        i++;
                    }



                    //}


                }

            }//using end

            if (!tmp11.Contains(txtCustomer.Text))
            {
                Customer.Add(new CustomerMaster() { No = i + 1, Customer = txtCustomer.Text });
            }
            else
            {
                MessageBox.Show("เพิ่ม Customer ซ้ำ","คำเตือน",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            

            List<string> tmp1 = new List<string>();
            List<List<string>> tmpList = new List<List<string>>();

            using (var csv = new CsvWriter(System.IO.Directory.GetCurrentDirectory() + "\\Customer.csv"))
            {
                foreach (var item in Customer)
                {
                    tmp1.Add(item.Customer.ToString());
                    //csv.Write(item.Customer.ToString());


                }

                tmpList.Add(tmp1);

                csv.Write(tmpList);

            }

            //var newList = Customer.OrderBy(m => m.Customer).ToList();


            List<string> tmp2 = null;
            List<CustomerMaster> Customer2 = new List<CustomerMaster>();
            Int32 i2 = 0;
            tmp2 = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\Customer.csv"))
            {
                while ((tmp2 = csv.ReadRow()) != null)
                {
                    //Customer.Add(tmp);

                    if (tmp2[0] != "")
                    {
                        //    Customer.Add(new CustomerMaster() { Id = i + 1, Customer = tmp[0] });

                        //    i++;

                        tmp2.Sort();

                        for (int i1 = 0; i1 < tmp2.Count(); i1++)
                        {
                            Customer2.Add(new CustomerMaster() { No = i2 + 1, Customer = tmp2[i2] });
                            i2++;
                        }
                    }

                }



            }//using end


            dgvCustomerList.DataSource = Customer2;

            
            //dgvCustomerList.Sort(dgvCustomerList.Columns["Customer"], ListSortDirection.Ascending);

            //int nRowIndex = dgvCustomerList.Rows.Count - 1;
            //int nColumnIndex = 0;

            dgvCustomerList.ClearSelection();

            foreach (DataGridViewRow row in dgvCustomerList.Rows)
            {
                // 1 is the column index
                if (row.Cells[1].Value.ToString().Equals(txtCustomer.Text.Trim()))
                {
                    row.Selected = true;
                    break;
                }
            }

            //dgvCustomerList.Rows[nRowIndex].Selected = true;
            //dgvCustomerList.Rows[nRowIndex].Cells[nColumnIndex].Selected = true;

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> tmp = null;
            //Customer = new List<List<string>>();
            List<CustomerMaster> Customer = new List<CustomerMaster>();
            Int32 i = 0;
            Int32 n = 0;
            //string[] tmpCustomer;
            tmp = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\Customer.csv"))
            {
                while ((tmp = csv.ReadRow()) != null)
                {

                    //Customer.Add(tmp);

                    //if (tmp[0] != "")
                    //{
                    //tmpCustomer = tmp[i].Split(',');

                    n = 0;

                    tmp.Sort();

                    for (int i1 = 0; i1 < tmp.Count(); i1++)
                    {
                        if (tmp[i1] != txtCustomer.Text)
                        {
                            Customer.Add(new CustomerMaster() { No = n + 1, Customer = tmp[i1] });
                            n++;
                            i++;
                        }
                        
                    }



                    //}


                }

            }//using end

            //Customer.Add(new CustomerMaster() { Id = i + 1, Customer = txtCustomer.Text });

            List<string> tmp1 = new List<string>();
            List<List<string>> tmpList = new List<List<string>>();

            using (var csv = new CsvWriter(System.IO.Directory.GetCurrentDirectory() + "\\Customer.csv"))
            {
                foreach (var item in Customer)
                {
                    tmp1.Add(item.Customer.ToString());
                    //csv.Write(item.Customer.ToString());



                }

                tmpList.Add(tmp1);

                if (tmp1.Count != 0)
                {
                    csv.Write(tmpList);
                }
                

            }

            dgvCustomerList.DataSource = Customer;

            //dgvCustomerList.Sort(dgvCustomerList.Columns["Customer"], ListSortDirection.Ascending);

            int nRowIndex = dgvCustomerList.Rows.Count - 1;
            int nColumnIndex = 0;

            txtCustomer.Text = string.Empty;

            dgvCustomerList.ClearSelection();

            //dgvCustomerList.Rows[nRowIndex].Selected = true;
            //dgvCustomerList.Rows[nRowIndex].Cells[nColumnIndex].Selected = true;
        }
    }
}
