using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net.NetworkInformation;

namespace ChuPiao.Common.Utils
{
    /// <summary>
    /// 应用程序工具类
    /// </summary>
    public class ApplicationUtil
    {
        /// <summary>
        /// 检测当前系统中是否已经运行了一个实例程序
        /// </summary>
        /// <param name="mutex">全局的互斥体对象引用</param>
        /// <param name="productionName">应用程序产品名</param>
        ///  <param name="executeMethodWhenAlreadyRunning">当已运行了实例时，执行的方法</param>
        /// <param name="args">方法的参数</param>
        public static void IsAlreadyRunning(out Mutex mutex,string productionName,Delegate executeMethodWhenAlreadyRunning,object[] args)
        {
            try
            {
                bool isCreatedFirstly = false;
                mutex = new Mutex(false,"Global\\"+ productionName, out isCreatedFirstly);
                if (!isCreatedFirstly)
                {
                    if (executeMethodWhenAlreadyRunning != null)
                    {
                        executeMethodWhenAlreadyRunning.DynamicInvoke(args);
                    }
                }
            }
            catch(Exception e)
            {
                mutex = null;
            }
        }

        /// <summary>
        ///  检查并等待本地连接正常
        /// </summary>
        /// <param name="totalTime">等待的时间(单位:秒)</param>
        /// <param name="timeSpan">检查本地连接的间隔(单位:毫秒)</param>
        /// <param name="spareSeconds">离检查本地连接超时的时间(单位:秒)</param>
        /// <param name="doSomethingWaitting">等待下一次检查本地连接执行的操作(可为null)</param>
        /// <param name="waittingArgs">等待下一次检查本地连接执行的操作的参数</param>
        /// <param name="doSomethingRuturning">检查本地连接超时后执行的操作(可为null)</param>
        /// <param name="returningArgs">检查本地连接超时后执行的操作的参数</param>
        public static void CheckLocalNetwork(int totalTime, int timeSpan, out int spareSeconds, Delegate doSomethingWaitting, object[] waittingArgs, Delegate doSomethingRuturning, object[] returningArgs)
        {
            long start = DateTime.Now.Ticks;
            spareSeconds = 0;
            while (!NetworkInterface.GetIsNetworkAvailable())
            {
                long pass = (DateTime.Now.Ticks - start) * 100 / (1000 * 1000 * 1000);
                spareSeconds = (int)(totalTime - pass);
                if (spareSeconds == 0)
                {
                    if (doSomethingRuturning != null)
                    {
                        doSomethingRuturning.DynamicInvoke(returningArgs);
                    }
                }
                if (doSomethingWaitting != null)
                {
                    doSomethingWaitting.DynamicInvoke(waittingArgs);
                }
                Thread.Sleep(timeSpan);
            }
        }

       /// <summary>
       /// 程序因为异常关闭的方法
       /// </summary>
        public static void CloseForException()
        {
            try
            {
                Thread.Sleep(1000);
                Environment.Exit(0);
            }
            catch (Exception e) { return; }
        }

        //C#判断操作系统是否为Windows7
        public static bool IsWindows7
        {
            get
            {
                return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major == 6) && (Environment.OSVersion.Version.Minor == 1);
            }
        }
    }
}
