using System;
using System.Threading;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using GcDevicePc.Common;
using GcDevicePc.ProThread;


namespace GcDevicePc
{
    static class Program
    {
        private static 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 || adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
                            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;
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e.Message);
                }

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

                Thread.Sleep(1000);
            }
        }

        static public bool Ping(string ip)
        {
            Ping p;
            PingOptions options;
            PingReply reply;
           
            string data;
            int timeout;
            try
            {
                p = new Ping();
                options = new PingOptions();
                options.DontFragment = true;
                data = "Test Data!";
                byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data);
                timeout = 1000;
                reply = p.Send(ip, timeout, buffer, options);

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

            }
            return false;  
        }

        static string ip;
        private static void CheckHmi()
        {
            HMISearch mysearch = new HMISearch(null);
            int hmicount = 0;
            bool ret = false;

            while (true)
            {
                ret = mysearch.ThStart();

                if (ret)
                {
                    Thread.Sleep(3000);
                    hmicount = mysearch.HMICount();
                    if (hmicount >= 1)
                    {
                        ip = (((globaldata.DeviceInfo)(mysearch.myHMIList[0])).IpAddr);
                        globaldata.connection_ip = ip;
                        mysearch.ThStop();
                        break;
                    }
                    mysearch.ThStop();
                    ret = false;
                }
                else
                {
                    mysearch.ReGetHmiIp();
                    Thread.Sleep(2000);
                }
               
                if (globaldata.OffLinkMode)
                {
                    globaldata.connection_ip = "";
                    break;
                }
            }
        }
   
        private static void RunStartWin()
        {
            DialogResult dr = new StartWin().ShowDialog();
        }

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

            bool createNew;

            Thread tStartWin = new Thread(RunStartWin)
            {
                IsBackground = true
            };
            tStartWin.Start();

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

            
            
        }
    }
}
