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 PackingControlSystem.lib;

namespace PackingControlSystem
{
    public partial class frmSettingPLC : Form
    {
        private List<List<string>> Settings = new List<List<string>>();
        DataTable dtSetting = new DataTable();
        private string userName = string.Empty;
        private bool blTUse = false;
        private bool blCSPUse = false;
        private bool blCDUse = false;
        public static bool blConfirm = false;
        private bool blReInputUse = false;

        public frmSettingPLC(string systemUserName)
        {
            InitializeComponent();
            userName = systemUserName;
        }

        public string RtnDbCon()
        {
            return "Data Source=" + this.Settings[0][0] + ";Initial Catalog=" + this.Settings[0][1] + ";User Id=sa;Password=" + this.Settings[0][2] + ";";

        }

        private void frmSettingPLC_Load(object sender, EventArgs e)
        {
            blConfirm = false;

            List<string> tmp = null;
            Settings = new List<List<string>>();
            tmp = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\Settings.csv"))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    Settings.Add(tmp);

                }

            }//using end

            txtIPAddress.Focus();

            loadTable();


        }

        private void loadTable()
        {
            dtSetting = new DataTable();
            clsUtil_MSSQL.GetDataFromTBsettingPLC(RtnDbCon(), "TM_SETTING_PLC", ref dtSetting);


            if (dtSetting.Rows.Count > 0)
            {
                txtIPAddress.Text = dtSetting.Rows[0][0].ToString();
                txtPort.Text = dtSetting.Rows[0][1].ToString();
                txtProcessNo.Text = dtSetting.Rows[0][5].ToString();

                if (dtSetting.Rows[0][2].Equals(true))
                {
                    btnTUse.BackColor = Color.LightGreen;
                    btnTNotUse.BackColor = SystemColors.Control;
                }
                else
                {
                    btnTUse.BackColor = SystemColors.Control;
                    btnTNotUse.BackColor = Color.LightCoral;
                }
                if (dtSetting.Rows[0][3].Equals(true))
                {
                    btnCSPUse.BackColor = Color.LightGreen;
                    btnCSPNotUse.BackColor = SystemColors.Control;
                }
                else
                {
                    btnCSPUse.BackColor = SystemColors.Control;
                    btnCSPNotUse.BackColor = Color.LightCoral;
                }
                if (dtSetting.Rows[0][4].Equals(true))
                {
                    btnCDUse.BackColor = Color.LightGreen;
                    btnCDNotUse.BackColor = SystemColors.Control;
                }
                else
                {
                    btnCDUse.BackColor = SystemColors.Control;
                    btnCDNotUse.BackColor = Color.LightCoral;
                }
                if (dtSetting.Rows[0][6].Equals(true))
                {
                    btnReInputUse.BackColor = Color.LightGreen;
                    btnReInputNotUse.BackColor = SystemColors.Control;
                }
                else
                {
                    btnReInputUse.BackColor = SystemColors.Control;
                    btnReInputNotUse.BackColor = Color.LightCoral;
                }


            }

        }

        private void btnTUse_Click(object sender, EventArgs e)
        {
            if (btnTUse.BackColor == SystemColors.Control)
            {
                btnTUse.BackColor = Color.LightGreen;
                btnTNotUse.BackColor = SystemColors.Control;
            }
        }

        private void btnTNotUse_Click(object sender, EventArgs e)
        {
            if (btnTNotUse.BackColor == SystemColors.Control)
            {
                btnTUse.BackColor = SystemColors.Control;
                btnTNotUse.BackColor = Color.LightCoral;
                btnCSPUse.BackColor = SystemColors.Control;
                btnCSPNotUse.BackColor = Color.LightCoral;
                btnCDUse.BackColor = SystemColors.Control;
                btnCDNotUse.BackColor = Color.LightCoral;
            }
        }

        private void btnCSPUse_Click(object sender, EventArgs e)
        {
            if ((btnCSPUse.BackColor == SystemColors.Control) && (btnTNotUse.BackColor == SystemColors.Control))
            {
                btnCSPUse.BackColor = Color.LightGreen;
                btnCSPNotUse.BackColor = SystemColors.Control;
            }
        }

        private void btnCSPNotUse_Click(object sender, EventArgs e)
        {
            if (btnCSPNotUse.BackColor == SystemColors.Control)
            {
                btnCSPUse.BackColor = SystemColors.Control;
                btnCSPNotUse.BackColor = Color.LightCoral;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            frmInputPassword frm = new frmInputPassword("");
            frm.ShowDialog();

            if (blConfirm == false)
            {
                txtIPAddress.Focus();

                loadTable();

                return;
            }


            dtSetting = new DataTable();
            clsUtil_MSSQL.GetDataFromTBsettingPLC(RtnDbCon(), "TM_SETTING_PLC", ref dtSetting);

            if (btnTUse.BackColor == SystemColors.Control)
            {
                blTUse = false;
            }
            else
            {
                blTUse = true;
            }

            if (btnCSPUse.BackColor == SystemColors.Control)
            {
                blCSPUse = false;
            }
            else
            {
                blCSPUse = true;
            }

            if (btnCDUse.BackColor == SystemColors.Control)
            {
                blCDUse = false;
            }
            else
            {
                blCDUse = true;
            }

            if (btnReInputUse.BackColor == SystemColors.Control)
            {
                blReInputUse = false;
            }
            else
            {
                blReInputUse = true;
            }

            if (dtSetting.Rows.Count > 0)
            {
                clsUtil_MSSQL.updateSettingPLC(RtnDbCon(), txtIPAddress.Text, txtPort.Text, blTUse, blCSPUse,
                    blCDUse, userName, DateTime.Now.ToString("yyyyMMdd"),
                    DateTime.Now.ToString("HHmmss"), txtProcessNo.Text, blReInputUse);
            }
            else
            {
                clsUtil_MSSQL.insertSettingPLC(RtnDbCon(), txtIPAddress.Text, txtPort.Text, blTUse, blCSPUse,
                    blCDUse, userName, DateTime.Now.ToString("yyyyMMdd"),
                    DateTime.Now.ToString("HHmmss"), txtProcessNo.Text, blReInputUse);
            }

            MessageBox.Show(this, "แก้ไขข้อมูล Setting PLC เรียบร้อย",
                                   "Setting PLC", MessageBoxButtons.OK,
                                   MessageBoxIcon.Information,
                                   MessageBoxDefaultButton.Button1, 0);
        }

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

        private void btnCDUse_Click(object sender, EventArgs e)
        {
            if ((btnCDUse.BackColor == SystemColors.Control) && (btnTNotUse.BackColor == SystemColors.Control))
            {
                btnCDUse.BackColor = Color.LightGreen;
                btnCDNotUse.BackColor = SystemColors.Control;
            }
        }

        private void btnCDNotUse_Click(object sender, EventArgs e)
        {
            if (btnCDNotUse.BackColor == SystemColors.Control)
            {
                btnCDUse.BackColor = SystemColors.Control;
                btnCDNotUse.BackColor = Color.LightCoral;
            }
        }

        private void btnReInputUse_Click(object sender, EventArgs e)
        {
            if (btnReInputUse.BackColor == SystemColors.Control)
            {
                btnReInputUse.BackColor = Color.LightGreen;
                btnReInputNotUse.BackColor = SystemColors.Control;
            }
        }

        private void btnReInputNotUse_Click(object sender, EventArgs e)
        {
            if (btnReInputNotUse.BackColor == SystemColors.Control)
            {
                btnReInputUse.BackColor = SystemColors.Control;
                btnReInputNotUse.BackColor = Color.LightCoral;                
            }
        }
    }
}
