using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;
using System.Net;
using System.Timers;

namespace US.Browser.IE.BLL
{
    public class USIE
    {
        [DllImport("wininet")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

        public USIE()
        {
        }

        public static string GetNavigateAddress()
        {
            //此处需要重数据库获取，如果获取不到使用默认地址
            return "http://www.1473.cn";
        }

        /// <summary>
        /// 捕抓所有的移除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static bool Exception(object sender, Exception e)
        {
            if (isnet())
            {
                try
                {
                    BLL.WindowHWND.freem(); //回收内存
                    BLL.USClientCommunicate.PostPage("http://admin.1473.cn/Admin/Admin.ashx?functionname=Admin.SetLog", "POST", null, "Admin.SetLog=", "", "([500," + Guid.newGuid() + "," + HttpContext.Current.Server.UrlEncode("错误追踪：" + e.StackTrace + "</br>错误提示:" + e.Message) + "," + DateTime.Now + "," + e.Source.ToString() + "])");
                }
                catch (Exception ex) { }
                return true;
            }
            else { return false; }
        }

        /// <summary>
        /// 判断本地是否联网
        /// </summary>
        /// <returns></returns>
        public static bool isnet()
        {
            int i = 0;
            if (InternetGetConnectedState(out i, 0)) { return MyPing("www.1473.cn", out i); }
            return false;
        }

        /// <summary>
        /// ping接网络判断是否连接
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="UER"></param>
        /// <returns></returns>
        public static bool MyPing(string URL, out int UER)
        {
            int i; PingReply _URE; UER = 0;
            using (Ping _UPE = new Ping())
            {
                try
                {
                    for (i = 0; i < 5; i++)
                    {
                        _URE = _UPE.Send(URL);
                        if (_URE.Status != IPStatus.Success) { UER++; }
                    }
                }
                catch { return false; }
            }
            return UER < 4;
        }


        /// <summary>
        /// C#调用cmd命令
        /// </summary>
        /// <param name="UCM"></param>
        public static void CMD(string UCM)
        {
            System.Diagnostics.Process _UPE = new System.Diagnostics.Process();
            _UPE.StartInfo.FileName = "cmd.exe"; _UPE.StartInfo.UseShellExecute = false;
            _UPE.StartInfo.RedirectStandardInput = true; _UPE.StartInfo.RedirectStandardOutput = true; _UPE.StartInfo.RedirectStandardError = true;
            _UPE.StartInfo.CreateNoWindow = true; _UPE.Start(); _UPE.StandardInput.WriteLine(UCM); _UPE.StandardInput.WriteLine("exit");
        }

        /// <summary>
        /// 判断程序是否安装
        /// </summary>
        /// <returns></returns>
        private static bool isIn()
        {
            string _USI; object _UDS; Microsoft.Win32.RegistryKey _UEK;
            Microsoft.Win32.RegistryKey _UKE = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");//获取所有安装的程序
            foreach (string USE in _UKE.GetSubKeyNames())
            {
                _UEK = _UKE.OpenSubKey(USE); _UDS = _UEK.GetValue("DisplayName");
                if (_UDS != null)
                {
                    _USI = (string)_UEK.GetValue("DisplayVersion");
                    if (_UDS.ToString().Contains("云端"))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Timeout计时器
        /// </summary>
        /// <param name="UIE"></param>
        /// <param name="UFN"></param>
        public static void setTimeout(double UIE, Action<ElapsedEventArgs> UFN)
        {
            Timer _UTE = new Timer();
            _UTE.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
            {
                try { _UTE.Enabled = false; _UTE.Dispose(); }
                catch (Exception ex) { } UFN(e);
            };
            _UTE.Enabled = true; _UTE.Start();
        }

        /// <summary>
        /// Interval计时器
        /// </summary>
        /// <param name="UIE"></param>
        /// <param name="UFN"></param>
        public static void setInterval(double UIE, Action<ElapsedEventArgs> UFN)
        {
            Timer _UTE = new Timer();
            _UTE.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
            {
                UFN(e);
            };
            _UTE.Enabled = true; _UTE.Start();
        }

        #region 无用使用

        //Ping ping = new Ping();
        //PingOptions poptions = new PingOptions();
        //poptions.DontFragment = true;
        //string data = string.Empty;
        //byte[] buffer = Encoding.ASCII.GetBytes(data);
        //PingReply reply = ping.Send("www.1473.cn", 1200, buffer, poptions);
        //if (reply.Status == IPStatus.Success) { }
        //else { }

        //System.Net.NetworkInformation.Ping ping;
        //System.Net.NetworkInformation.PingReply res;
        //ping = new System.Net.NetworkInformation.Ping();
        //try
        //{
        //    res = ping.Send("www.1473.cn");

        //    return (res.Status == System.Net.NetworkInformation.IPStatus.Success);
        //}
        //catch (Exception er)
        //{
        //    return false;
        //}

        #endregion
    }
}
