using System;
using System.Collections.Generic;
using System.Text;

namespace Nirvana.Common
{
    public class OperatorProvider
    {
        public static int ExpiresMin = 10080;//60*24*7 ，登录过期时间，单位为分钟
        public static OperatorProvider Provider
        {
            get { return new OperatorProvider(); }
        }
        private static readonly string  LoginUserKey = Configs.GetString("LoginProviderKey");
        private static readonly string LoginProvider = Configs.GetString("LoginProvider");

        public OperatorModel GetCurrent()
        {
            try
            {
                OperatorModel operatorModel = new OperatorModel();
                if (LoginProvider == "Cookie")
                {
                    operatorModel = DESEncrypt.Decrypt(WebHelper.GetCookie(LoginUserKey).ToString()).ToObject<OperatorModel>();
                }
                else if (LoginProvider == "CookieAndSession")
                {
                    var user1 = WebHelper.GetSession(LoginUserKey);
                    if (!string.IsNullOrWhiteSpace(user1))
                    {
                        operatorModel = DESEncrypt.Decrypt(user1).ToObject<OperatorModel>();
                    }
                    else
                    {
                        user1 = WebHelper.GetCookie(LoginUserKey);
                        operatorModel = DESEncrypt.Decrypt(user1).ToObject<OperatorModel>();
                    }
                }
                else
                {
                    operatorModel = DESEncrypt.Decrypt(WebHelper.GetSession(LoginUserKey).ToString()).ToObject<OperatorModel>();
                }
                return operatorModel;

            }
            catch
            {
                return null;
            }
        }
        public void AddCurrent(OperatorModel operatorModel)
        {
            if (LoginProvider == "Cookie")
            {
                WebHelper.WriteCookie(LoginUserKey, DESEncrypt.Encrypt(operatorModel.ToJson()), ExpiresMin);
            }
            else if (LoginProvider == "CookieAndSession")
            {
                WebHelper.WriteCookie(LoginUserKey, DESEncrypt.Encrypt(operatorModel.ToJson()), ExpiresMin);
                WebHelper.WriteSession(LoginUserKey, DESEncrypt.Encrypt(operatorModel.ToJson()));
            }
            else
            {
                WebHelper.WriteSession(LoginUserKey, DESEncrypt.Encrypt(operatorModel.ToJson()));
            }
        }
        public void RemoveCurrent()
        {
            if (LoginProvider == "Cookie")
            {
                WebHelper.RemoveCookie(LoginUserKey.Trim());
            }
            else if (LoginProvider == "CookieAndSession")
            {
                WebHelper.RemoveCookie(LoginUserKey.Trim());
                WebHelper.RemoveSession(LoginUserKey.Trim());
            }
            else
            {
                WebHelper.RemoveSession(LoginUserKey.Trim());
            }
        }
        /// <summary>
        /// 系统内置管理员以便维护
        /// </summary>
        /// <param name="usernmae"></param>
        /// <param name="password"></param>
        /// <param name="issupper"></param>
        public bool IsSupperAdmin(string usernmae, string password)
        {
            return usernmae.Equals("liuzl") && password.Equals("18638204898");
        }

        public void BuildLogin()
        {
            var current = GetCurrent();
            if (current != null)
                AddCurrent(current);
        }
    }
}
