using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Caching;

namespace Stat.Cache
{
    class Config : IConfig
    {


        #region IConfig 成员

        public int GetWebConfig_int(string wName)
        {
            int hour = 0;

            if (HttpContext.Current != null)
            {

                if (HttpContext.Current.Cache[wName] == null)
                {
                    //判断是否存在
                    if (ConfigurationManager.AppSettings.Get(wName) != null)//存在
                    {
                        hour = int.Parse(ConfigurationManager.AppSettings.Get(wName));

                    }
                    HttpContext.Current.Cache.Insert(wName, hour);
                }
                return int.Parse(HttpContext.Current.Cache[wName].ToString());
            }
            else
            {
                return int.Parse(ConfigurationManager.AppSettings.Get(wName));
            }
        }

        public string GetWebConfig_Str(string wName)
        {
            string _str = "";
            if (HttpContext.Current != null)
            {
                if (HttpContext.Current.Cache[wName] == null)
                {
                    //判断是否存在
                    if (ConfigurationManager.AppSettings.Get(wName) != null)//存在
                    {
                        _str = ConfigurationManager.AppSettings.Get(wName);

                    }
                    HttpContext.Current.Cache.Insert(wName, _str);
                }
                return HttpContext.Current.Cache[wName].ToString();
            }
            else
            {
                return ConfigurationManager.AppSettings.Get(wName);
            }
        }

        #endregion

        /// <summary>
        /// 获取 网站配置
        /// </summary>
        /// <returns></returns>
        private IList<Stat.Model.System> GetConfig()
        {
            if (HttpContext.Current.Cache.Get("Config") == null)
            {
                // 建立缓存依赖项，如果文件发生变化，删除缓存
                string _Ctxt = @"\CacheTxt\Sys.txt";
                Methods.CreatFile(HttpContext.Current.Server.MapPath(_Ctxt));
                CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath(_Ctxt));
                IList<Stat.Model.System> model = BLL.Systems.Get();
                if (model != null && model.Count > 0)
                {
                    HttpContext.Current.Cache.Insert("Config", model, dep, System.DateTime.MaxValue, TimeSpan.FromDays(1.0), CacheItemPriority.NotRemovable, null);
                    return model;
                }
                else
                {
                    return null;
                }

            }
            else
            {
                return (List<Stat.Model.System>)HttpContext.Current.Cache.Get("Config");
            }
        }

        /// <summary>
        /// 是否允许用户登录
        /// </summary>
        /// <returns></returns>
        public bool IsAllowUserLogin()
        {
            if (GetConfig() != null)
            {
                return GetConfig()[0].IsUse == 1;
            }
            return false;
        }
        /// <summary>
        /// 是否允许用户注册
        /// </summary>
        /// <returns></returns>
        public bool IsAllowRegister()
        {
            if (GetConfig() != null)
            {
                return GetConfig()[0].IsReg == 1;
            }
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public String WebName()
        {
            if (GetConfig() != null)
            {
                return GetConfig()[0].WebName;
            }
            return "";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public String WebUrl()
        {
            if (GetConfig() != null)
            {
                return GetConfig()[0].WebUrl;
            }
            return "";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int ShowID()
        {
            if (GetConfig() != null)
            {
                return GetConfig()[0].ShowID;
            }
            return 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public String WebLogo()
        {
            if (GetConfig() != null)
            {
                return GetConfig()[0].WebLogo;
            }
            return "";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public String WebFoot()
        {
            if (GetConfig() != null)
            {
                return GetConfig()[0].WebFoot;
            }
            return "";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public String StatCode()
        {
            if (GetConfig() != null)
            {
                return GetConfig()[0].Stat;
            }
            return "";
        }
        public Int32 Deferred()
        {
            if (GetConfig() != null)
            {
                int def = GetConfig()[0].Deferred;
                def = def < 1000 ? 1000 : def;
                return def;
            }
            return 5000;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string WebCopy()
        {
            if (GetConfig() != null)
            {
                return GetConfig()[0].WebCopy;
            }
            return "";
        }
        /// <summary>
        /// 小时段 推迟 数
        /// </summary>
        /// <returns></returns>
        public int GetStatHourPostpone()
        {
            int hour = 0;
            string configName = "StatHourPostpone";
            if (HttpContext.Current != null)
            {
                if (HttpContext.Current.Cache[configName] == null)
                {

                    //判断是否存在
                    if (ConfigurationManager.AppSettings.Get(configName) != null)//存在
                    {
                        hour = int.Parse(ConfigurationManager.AppSettings.Get(configName));

                    }
                    HttpContext.Current.Cache.Insert(configName, hour);

                }
                return int.Parse(HttpContext.Current.Cache[configName].ToString());
            }
            else
            {
                return int.Parse(ConfigurationManager.AppSettings.Get(configName));
            }
        }

        /// <summary>
        /// 用户总数
        /// </summary>
        /// <returns></returns>
        public int GetUserTotal()
        {
            return BLL.Users.GetUserSum();
        }
        /// <summary>
        /// 站点总数
        /// </summary>
        /// <returns></returns>
        public int GetWebsiteTotal()
        {
            return BLL.WebSites.GetWebSum();
        }





        #region IConfig 成员


        public bool SetupIpTaskMode()
        {
            if (GetConfig() != null)
            {
                //“键名”已经强约定
                HttpContext.Current.Application["IpTaskMode"] = GetConfig()[0].IpTaskMode;
            }
            return HttpContext.Current.Application["IpTaskMode"].ToString() == "1" ? true : false;
        }

        #endregion

        #region IConfig 成员


        public bool IsUserPwdMd5()
        {
            if (GetConfig() != null)
            {
                return GetConfig()[0].IsUserPwdMD5;
            }
            return false;
        }

        #endregion
    }
}
