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

namespace PageAdmin.Web.Areas.E.Controllers
{

    /// <summary>
    /// 验证码
    /// </summary>
    public class VerificationCodeController : Controller
    {

        [HttpGet]
        public ActionResult Index()
        {
            string referer = Request.ServerVariables["Http_Referer"];
            if (string.IsNullOrEmpty(referer)) //直接打开则不生成cookie
            {
                return Content("请通过Img的src标签调用！");
            }
            int codeLength = 4;
            int width = 100;
            int height = 30;
            int fontsize = 20;
            string code = string.Empty;
            byte[] bytes = Utils.ImageHelper.VerificationCode(codeLength, width, height, fontsize, out code);
            VerificationCodeBLL verificationCodeBLL = new VerificationCodeBLL();
            verificationCodeBLL.AddCookie(code);
            return File(bytes, @"image/jpeg"); //mvc
        }

        [AjaxOnly]
        [HttpPost]
        /// <summary>
        /// Ajax端检测验证码
        /// </summary>
        /// <param name="filterContext"></param>
        public JsonResult Validate(FormCollection form)
        {
            string validateName = StringHelper.Format<string>(form["_validateName"]);
            string code = StringHelper.Format<string>(form[validateName]);
            VerificationCodeBLL verificationCodeBLL = new VerificationCodeBLL();
            var tipsInfo = verificationCodeBLL.Validate(code);
            return base.Json(tipsInfo);
        }



    }
}