using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SCAN.Data.DataAccess;
using Bt.ScanLib;

using System.Runtime.InteropServices;
using CsvReadWrite;
using SCAN.lib;
using Bt.CommLib;

using Bt;

using System.Net.Sockets;
using System.Net;

namespace SCAN
{
    public partial class Form_SettingsIP : Form
    {
        public static Form_SettingsIP _Form_SettingsIP;

        public Form_SettingsIP()
        {
            InitializeComponent();
        }
        
        private void btnConnect_Click_1(object sender, EventArgs e)
        {
            List<string> tmp = null;
            List<string> row = null;
            
            try
            {
                lblStatus.Text = "waiting";
                lblStatus.BackColor = Color.YellowGreen;
                lblStatus.ForeColor = Color.Black;
                _Form_SettingsIP.Update();

                if (tbxHostIP1.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("please input ip");
                    return;
                }

                if (tbxHostIP2.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("please input ip");
                    return;
                }

                if (tbxHostIP3.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("please input ip");
                    return;
                }

                if (tbxHostIP4.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("please input ip");
                    return;
                }
                if (tbxPort.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("please input port");
                    return;
                }
                if (!IsNumeric(tbxPort.Text.Trim()))
                {
                    MessageBox.Show("please input port is number");
                    return;
                }
                if (!IsNumeric(tbxHostIP1.Text.Trim()))
                {
                    MessageBox.Show("please input ip is number");
                    return;
                }
                if (!IsNumeric(tbxHostIP2.Text.Trim()))
                {
                    MessageBox.Show("please input ip is number");
                    return;
                }
                if (!IsNumeric(tbxHostIP3.Text.Trim()))
                {
                    MessageBox.Show("please input ip is number");
                    return;
                }
                if (!IsNumeric(tbxHostIP4.Text.Trim()))
                {
                    MessageBox.Show("please input ip is number");
                    return;
                }
            

                row = new List<string>();

                    row.Add(tbxHostIP1.Text + "," + tbxHostIP2.Text + "," + tbxHostIP3.Text + "," + tbxHostIP4.Text + "," + tbxPort.Text);

                    using (var csv = new CsvWriter("\\FlashDisk\\BT_FILES\\drv1\\HT_WeightDataToPrint\\in\\SettingsIP.csv"))
                    {

                        csv.WriteRow(row);
                    }

                    Form_Login._Form_LoginInstance.SettingsIP = new List<List<string>>();
                    tmp = null;
                    using (var csv = new CsvReader("\\FlashDisk\\BT_FILES\\drv1\\HT_WeightDataToPrint\\in\\SettingsIP.csv"))
                    {//mysql
                        //setting list comName, WatchCnt, RetryCnt;
                        //SettingList[8]no 0 timer interval, 1 share id, 2, share pass, 3 upload path 4 share dir
                        while ((tmp = csv.ReadRow()) != null)
                        {
                            Form_Login._Form_LoginInstance.SettingsIP.Add(tmp);

                        }

                    }//using end

                    lblStatus.Text = "Save correct";
                    lblStatus.BackColor = Color.Blue;
                    lblStatus.ForeColor = Color.White;

                    //bool Valid = connectPLC(Convert.ToInt32(tbxPort.Text), tbxHostIP1.Text + "." + tbxHostIP2.Text + "." + tbxHostIP3.Text + "." + tbxHostIP4.Text);
                    //if (Valid)
                    //{
                    //    lblStatus.Text = "Connecting";
                    //    lblStatus.BackColor = Color.Blue;
                    //    lblStatus.ForeColor = Color.White;
                    //}
                    //else
                    //{

                    //    lblStatus.Text = "Not connecting";
                    //    lblStatus.BackColor = Color.Silver;
                    //    lblStatus.ForeColor = Color.White;

                    //}

            }
            catch (Exception)
            {
                lblStatus.Text = "Save not correct";
                lblStatus.BackColor = Color.Silver;
                lblStatus.ForeColor = Color.White;
                
            }
        }

        public bool IsNumeric(string value)
        {
            return value.All(char.IsNumber);
        }

        #region "socket"
        private bool connectPLC(Int32 port, string ips)
        {

            IPAddress host = IPAddress.Parse(ips);//  
            IPEndPoint ipendpoint = new IPEndPoint(host, port); // assign host and port                                                               
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {

                socket.Connect(ipendpoint);
                                
            }
            catch (SocketException e)
            {

                socket.Close();
                return false;
            }

            socket.Close();
            return true;
        }
        #endregion

        private void btnMainMenu_Click(object sender, EventArgs e)
        {
            Form_Menu._Form_MenuInstance.Show();
            this.Close();
        }

        private void Form_SettingsIP_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode.ToString())
            {

                case "F1":
                    btnConnect_Click_1(sender, e);

                    break;

                case "F4":
                    btnMainMenu_Click(sender, e);

                    break;

            }
           

        }

        private void Form_SettingsIP_Load(object sender, EventArgs e)
        {
            if (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width == 240)
            {
                resolution.ScreenSize.VGAtoQVGA(this);
            }

            // フォームの最大化・最小化ボタン非表示
            this.MaximizeBox = !this.MaximizeBox;
            this.MinimizeBox = !this.MinimizeBox;
            _Form_SettingsIP = this;

            tbxHostIP1.Text = Form_Login._Form_LoginInstance.SettingsIP[0][0];
            tbxHostIP2.Text = Form_Login._Form_LoginInstance.SettingsIP[0][1];
            tbxHostIP3.Text = Form_Login._Form_LoginInstance.SettingsIP[0][2];
            tbxHostIP4.Text = Form_Login._Form_LoginInstance.SettingsIP[0][3];
            tbxPort.Text = Form_Login._Form_LoginInstance.SettingsIP[0][4];

        }
    }
}