using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net.Sockets;


namespace GcDevicePc
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        //static public string GetGW()
        //{
        //    string strGateway = "192.168.10.1";

        //    try
        //    {
        //        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        //        //遍历数组
        //        foreach (NetworkInterface adapter in nics)
        //        {
        //            if (adapter.Name == "本地连接")
        //            {
        //                bool Pd1 = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet); //判断是否是以太网连接
        //                if (Pd1)
        //                {
        //                    IPInterfaceProperties ip = adapter.GetIPProperties();
        //                    GatewayIPAddressInformationCollection gateways = ip.GatewayAddresses;

        //                    foreach (var gateWay in gateways)
        //                    {
        //                        strGateway = gateWay.Address.ToString();
        //                        if (strGateway.Length > 0)
        //                        {
        //                            break;
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        return strGateway;
        //    }
        //    finally
        //    {
               
        //    }

        //    return strGateway;
        //}

        static public void isnetworkok()
        {
            string localip = "";

            while(true)
            {
                try
                {
                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                    foreach (NetworkInterface adapter in nics)
                    {
                        if (adapter.Name == "本地连接")
                        {
                            bool Pd1 = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet); //判断是否是以太网连接
                            if (Pd1)
                            {
                                IPInterfaceProperties ip = adapter.GetIPProperties();
                                UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                                foreach (UnicastIPAddressInformation ipadd in ipCollection)
                                {
                                    if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
                                        localip = ipadd.Address.ToString();
                                }

                                if (localip != "")
                                {
                                    globaldata.StartNetwork = true;
                                }
                            }
                        }
                    }

                }
                finally
                {

                }

                if (globaldata.OffLinkMode || globaldata.StartNetwork)
                {
                    break;
                }

                Thread.Sleep(1000);
            }
        }

        static public bool Ping(string ip)
        {
            System.Net.NetworkInformation.Ping p;
            System.Net.NetworkInformation.PingOptions options;
            System.Net.NetworkInformation.PingReply reply;
           
            string data;
            int timeout;
            try
            {
                p = new System.Net.NetworkInformation.Ping();
                options = new System.Net.NetworkInformation.PingOptions();
                options.DontFragment = true;
                data = "Test Data!";
                byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data);
                timeout = 1000; // Timeout 时间，单位：毫秒
                reply = p.Send(ip, timeout, buffer, options);

                if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
                {
                    globaldata.StartNetwork = true;
                    return true;
                }
                    
                else
                    return false;
            }
            catch
            {

            }
            return false;  
        }

        static string ip;
        static private void CheckHmi()
        {
            ProThread.HMISearch mysearch = new ProThread.HMISearch();
            int hmicount = 0;

            while (true)
            {
                mysearch.ThStart();
                Thread.Sleep(3000);
                hmicount = mysearch.HMICount();

                if (hmicount >= 1)
                {
                    ip = (((globaldata.DeviceInfo)(mysearch.myHMIList[0])).IpAddr);
                    globaldata.connection_ip = ip;
                    break;
                }    
                mysearch.ThStop();

                if (globaldata.OffLinkMode)
                {
                    globaldata.connection_ip = "";
                    break;
                }
            }
        }

        static private System.Windows.Forms.DialogResult ShowStart()
        {
            return new StartWin().ShowDialog();
        }

        static private void runStartWin()
        {
            DialogResult dr;
            dr = ShowStart();
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //bool pingok = false;
            bool createNew;
            //string ipgw = "";
            Thread t_StartWin;

            t_StartWin = new Thread(runStartWin);
            t_StartWin.IsBackground = true;
            t_StartWin.Start();

            //while (true)
            //{
            //    ipgw = GetGW();
            //    pingok = Ping(ipgw);

            //    if (pingok)
            //    {
            //        break;
            //    }

               

            //    if(globaldata.OffLinkMode)
            //    {
            //        break;
            //    }
            //    Thread.Sleep(1000);
            //}
            isnetworkok();
            CheckHmi();
            
            {
                using (System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out createNew))
                {
                    if (createNew)
                    {
                        Application.Run(new StartForm());
                    }
                    else
                    {
                        MessageBox.Show("应用程序已经在运行中...");

                        System.Threading.Thread.Sleep(1000);
                        System.Environment.Exit(1);
                    }
                }
            }

            
            
        }
    }
}
