using System;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Text;
using System.Configuration;
using System.Web;


namespace ICaiBan.Framework.Core
{
    public class App
    {


        /// <summary>
        /// ÏµÍ³ÔËÐÐ×´Ì¬
        /// </summary>
        public enum SysRunStats
        {
            /// <summary>
            /// Õý³£Ö´ÐÐ
            /// </summary>
            Play,
            /// <summary>
            /// µ÷ÊÔ½×¶Î
            /// </summary>
            Debug
        }


        /// <summary>
        /// µ±Ç°ÏµÍ³ÔËÐÐ×´Ì¬
        /// </summary>
        public static SysRunStats SysRunStatus
        {

            get
            {
                return (SysRunStats)System.Enum.Parse(typeof(SysRunStats), Config.GetConfig().SysRunStatus, false);
            }
        }



        /// <summary>
        /// ÏµÍ³ÔËÐÐµÄ»·¾³
        /// </summary>
        public enum SysRunTypes
        {
            /// <summary>
            /// web»·¾³
            /// </summary>
            WebForm,
            /// <summary>
            /// windows»·¾³
            /// </summary>
            WinForm,
            /// <summary>
            /// µ¥Ôª²âÊÔ
            /// </summary>
            UnitTest
        }

        /// <summary>
        /// µ±Ç°.net ÔËÐÐ»·¾³
        /// </summary>
        public static SysRunTypes SysRunType
        {
            get
            {

                return (SysRunTypes)System.Enum.Parse(typeof(SysRunTypes), Config.GetConfig().SysRunType, false);
            }
        }


        /// <summary>
        /// µ±Ç°.net ÔËÐÐÏµÍ³¸ùÄ¿Â¼(ÓÅÏÈ¸ù¾ÝÔËÐÐ»·¾³½øÐÐÅÐ¶Ï)
        /// </summary>
        public static string PhysicalApplicationPath
        {
            get
            {
                string path =  Config.GetConfig().PhysicalApplicationPath;

                if (string.IsNullOrEmpty(path))
                {
                    if (SysRunType == SysRunTypes.WinForm)
                        path = System.Windows.Forms.Application.StartupPath;
                    else
                    {
                        if (System.Web.HttpContext.Current != null)
                            path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
                        else if (!string.IsNullOrEmpty(System.Web.HttpRuntime.AppDomainAppPath))
                            path = System.Web.HttpRuntime.AppDomainAppPath;

                    }
                }
                return path;
            }

        }



        #region  »ñÈ¡Î¨Ò»Êý×Ö
        private static int randomMinValue
        {
            get { return Config.GetConfig().RandomMinValue; }
        }

        private static long LastUniqueNumeric = 0;
        private static int LastUniqueIndex = 1;

        /// <summary>
        /// »ñÈ¡Î¨Ò»Êý×Ö
        /// </summary>
        /// <returns></returns>
        public static int GetUniqueNumeric()
        {

            Random random = new Random();
            int reNumeric = random.Next(randomMinValue, 9999999);
            if (reNumeric == LastUniqueNumeric)
            {
                reNumeric = System.Convert.ToInt32(string.Concat(reNumeric.ToString(), LastUniqueIndex.ToString()));
                LastUniqueIndex++;
            }
            else
            {
                LastUniqueIndex = 1;
                LastUniqueNumeric = reNumeric;
            }
            return reNumeric;
        }

        #endregion






        /// <summary>
        /// »ñÈ¡Ö¸¶¨¼üÖµ
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSettionsValue(string key, string defValue)
        {
            try
            {
                if (ConfigurationManager.AppSettings[key] != null)
                    return ConfigurationManager.AppSettings[key].ToString();
            }
            catch
            {
                //
            }

            return defValue;

        }



        /// <summary>
        ///ÈÕÖ¾ÎÄ¼þ´æ·ÅÄ¿Â¼
        /// </summary>
        public static string LogFilesPath
        {
            get
            {
                string path = Config.GetConfig().LogFilesPath;
                if (!string.IsNullOrEmpty(path))
                {
                    if (App.SysRunType == SysRunTypes.UnitTest)
                    {
                        string stmp = Assembly.GetExecutingAssembly ( ).Location;
                               stmp = stmp.Substring ( 0 , stmp.LastIndexOf ("assembly"));//É¾³ýÎÄ¼þÃû
                        return stmp + path;
                    }
                    else
                    {
                        if (path.IndexOf("/") >= 0 && App.SysRunType == SysRunTypes.WebForm)
                            return System.Web.HttpContext.Current.Server.MapPath(path);
                        else
                            if (path.IndexOf(@"\\") < 0 && path.IndexOf(@":\") < 0 && path.IndexOf(@"\") >= 0 && App.SysRunType == SysRunTypes.WebForm)
                                return System.Web.HttpContext.Current.Server.MapPath(path);
                    }
                    return path;
                }
                else
                {
                    path = App.PhysicalApplicationPath;
                    if (!string.IsNullOrEmpty(path))
                        return string.Format("{0}\\SysLog", path);
                    return string.Empty;
                }

            }
        }


    


    }

}
