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;

namespace OrderSheetSystem
{
    public partial class FrmGateway : Form
    {
        public FrmGateway()
        {
            InitializeComponent();
        }

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


                    if (tmp[0] != "")
                    {


                        tmp.Sort();

                        for (int i1 = 0; i1 < tmp.Count(); i1++)
                        {
                            string[] tmpGateway = tmp[i].Split('|');
                            Gateway.Add(new GatewayMaster() { No = i + 1, Gateway = tmpGateway[0], Code = tmpGateway[1] });
                            i++;
                        }
                    }

                }



            }//using end



            dgvGatewayList.DataSource = Gateway;
            dgvGatewayList.Columns[1].Width = 400;
            dgvGatewayList.Columns[0].HeaderText = "No.";
            dgvGatewayList.Columns[1].SortMode = DataGridViewColumnSortMode.Automatic;


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

                txtGateway.Text = data;
                txtCode.Text = data1;
            }

        }

        private void dgvGatewayList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgvGatewayList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                txtGateway.Text = dgvGatewayList[1, e.RowIndex].Value.ToString();
                txtCode.Text = dgvGatewayList[2, e.RowIndex].Value.ToString();
            }
        }

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

                 
                    tmp.Sort();

                    for (int i1 = 0; i1 < tmp.Count(); i1++)
                    {
                        string[] tmpGateway = tmp[i1].Split('|');                       
                        if (tmpGateway.Length == 2)
                        {                                                  
                        Gateway.Add(new GatewayMaster() { No = i1 + 1, Gateway = tmpGateway[0], Code = tmpGateway[1] });
                        tmp11.Add(tmp[i1]);
                        i++;
                        }
                    }



                }

            }//using end

            if (!tmp11.Contains(txtGateway.Text))
            {
                Gateway.Add(new GatewayMaster() { No = i + 1, Gateway = txtGateway.Text, Code = txtCode.Text });
            }
            else
            {
                MessageBox.Show("เพิ่ม Gateway ซ้ำ", "คำเตือน", 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() + "\\Gateway.csv"))
            {
                foreach (var item in Gateway)
                {
                    tmp1.Add(item.Gateway.ToString() + "|" + item.Code.ToString());


                }

                tmpList.Add(tmp1);

                csv.Write(tmpList);

            }


            List<string> tmp2 = null;
            List<GatewayMaster> Gateway2 = new List<GatewayMaster>();
            Int32 i2 = 0;
            tmp2 = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\Gateway.csv"))
            {
                while ((tmp2 = csv.ReadRow()) != null)
                {

                    if (tmp2[0] != "")
                    {

                        tmp2.Sort();

                        for (int i1 = 0; i1 < tmp2.Count(); i1++)
                        {
                            string[] tmpGateway = tmp2[i2].Split('|');
                            Gateway2.Add(new GatewayMaster() { No = i2 + 1, Gateway = tmpGateway[0], Code = tmpGateway[1] });
                            i2++;
                        }
                    }

                }



            }//using end


            dgvGatewayList.DataSource = Gateway2;
                     
            dgvGatewayList.ClearSelection();

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

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

                    tmp.Sort();

                    for (int i1 = 0; i1 < tmp.Count(); i1++)
                    {
                        string[] tmpGateway = tmp[i1].Split('|');
                        //if (tmp[i1] != txtGateway.Text)
                            if (tmpGateway[0] != txtGateway.Text)
                            {
                            //string[] tmpGateway = tmp[i1].Split('|');
                            Gateway.Add(new GatewayMaster() { No = n + 1, Gateway = tmpGateway[0], Code = tmpGateway[1] });
                            n++;
                            i++;
                        }

                    }



                    //}


                }

            }//using end


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

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



                }

                tmpList.Add(tmp1);

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


            }

            dgvGatewayList.DataSource = Gateway;


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

            txtGateway.Text = string.Empty;
            txtCode.Text = string.Empty;

            dgvGatewayList.ClearSelection();

        }
    }
}
