
using PageAdmin.BLL;
using PageAdmin.Utils;
using System;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.Member.Controllers.Reg
{

    /// <summary>
    /// 登录特性
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class ValidateAttribute : ActionFilterAttribute
    {
        private TipsInfo _TipsInfo = null;
        private dynamic _ConfigData = null;
        //检测验证码，判断登录等等
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            _TipsInfo = new TipsInfo();
            ReadConfig();//读取配置文件
            GetSet(filterContext);
            CheckOpen(filterContext);
            ValidateVCode(filterContext);//检测验证码
            ValidateEmailCode(filterContext);//检测邮件验证码
            ValidateSmsCode(filterContext);//检测手机验证码
        }

        /// <summary>
        /// 获取配置文件
        /// </summary>
        private void ReadConfig()
        {
            _ConfigData=XmlHelper.AllNodeValue("configuration","/Config/MemberReg.config",true);
        }


        /// <summary>
        /// 设置某些字段值
        /// </summary>
        /// <param name="filterContext"></param>
        private void GetSet(ActionExecutingContext filterContext)
        {
            filterContext.Controller.ViewBag.DefaultState = _ConfigData.DefaultState;
            byte mobileState = 0, emailState = 0;
            if (_ConfigData.MobileFieldSet == "2")
            {
                mobileState = 1;
            }
            if (_ConfigData.EmailFieldSet == "2")
            {
                emailState = 1;
            }
            filterContext.Controller.ViewBag.EmailState = emailState;
            filterContext.Controller.ViewBag.MobileState = mobileState;
        }


        /// <summary>
        /// 检测开关
        /// </summary>
        /// <param name="filterContext"></param>
        private void CheckOpen(ActionExecutingContext filterContext)
        {
            string open = _ConfigData.Open;
            if (open == "0")
            {
                _TipsInfo.Msg = "对不起，注册功能已关闭，详情请联系管理员！";
                _TipsInfo.State = 0;
                filterContext.Result = new JsonResult()
                {
                    Data = _TipsInfo
                };
            }
        }

        /// <summary>
        /// 检测手机验证码
        /// </summary>
        /// <param name="filterContext"></param>
        private void ValidateSmsCode(ActionExecutingContext filterContext)
        {
            if (_ConfigData.MobileFieldSet!= "2")
            {
                return;
            }
            var request = filterContext.RequestContext.HttpContext.Request;
            string mobile= StringHelper.Format<string>(request.Form["mobile"]);
            string smsCode = StringHelper.Format<string>(request.Form["_SmsCode"]);
            SmsCodeBLL smsCodeBLL = new SmsCodeBLL();
            _TipsInfo = smsCodeBLL.Validate(mobile, smsCode);
            if (_TipsInfo.State != 1)
            {
                filterContext.Result = new JsonResult()
                {
                    Data = _TipsInfo
                };
            }
        }


        /// <summary>
        /// 检测邮件验证
        /// </summary>
        /// <param name="filterContext"></param>
        private void ValidateEmailCode(ActionExecutingContext filterContext)
        {
            if (_ConfigData.EmailFieldSet != "2")
            {
                return;
            }
            var request = filterContext.RequestContext.HttpContext.Request;
            string email = StringHelper.Format<string>(request.Form["email"]);
            string emailCode = StringHelper.Format<string>(request.Form["_EmailCode"]);
            EmailCodeBLL emailCodeBLL = new EmailCodeBLL();
            _TipsInfo = emailCodeBLL.Validate(email, emailCode);
            if (_TipsInfo.State != 1)
            {
                filterContext.Result = new JsonResult()
                {
                    Data = _TipsInfo
                };
            }
        }


        /// <summary>
        /// 检测验证码
        /// </summary>
        /// <param name="filterContext"></param>
        private void ValidateVCode(ActionExecutingContext filterContext)
        {
            if(_ConfigData.VerificationCode=="0")
            {
                return;
            }
            var request = filterContext.RequestContext.HttpContext.Request;
            string validateCode = StringHelper.Format<string>(request.Form["_VerificationCode"]);
            VerificationCodeBLL verificationCodeBLL = new VerificationCodeBLL();
            _TipsInfo = verificationCodeBLL.Validate(validateCode);
            if (_TipsInfo.State!=1)
            {
                filterContext.Result = new JsonResult()
                {
                    Data = _TipsInfo
                };
            }
        }

    }
}
