using CallCenterApi.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.BaseController
{
    public class ValidateController : ActionFilterAttribute
    {
        private string _DataType = "";

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            CommonHelper.InsertLog(System.DateTime.Now + " : " + Request.QueryString.ToString());

            if (string.IsNullOrEmpty(Request["Keys"]))
            {
                filterContext.Result = new EmptyResult();
                filterContext.HttpContext.Response.Write("Error:Request Invalid");
                filterContext.HttpContext.Response.End();
            }

            #region 解码
            var keyDomain = "api";
            var Keys = "";
            //keyDomain = Regex.Match(Request.UrlReferrer.Host + "", @"(http://)?((\w+\.)+\w+)").Value.ToLower().Replace("http://", "");
            //keyDomain = "23423423";
            //keyDomain = "232";
            //Keys = new SysConfigHelper.DemoKeys(keyDomain).Keys;
            Keys = "LYYU80DV3465QSF1";
            if (keyDomain == "" || Keys == "")
            {
                filterContext.Result = new EmptyResult();
                filterContext.HttpContext.Response.Write("Error:Key Invalid");
                filterContext.HttpContext.Response.End();
            }
            var keyStr = Regex.Split(CommonHelper.DecryptAES(Request["Keys"], Keys), @"\|URL\|", RegexOptions.IgnoreCase);
            if (keyStr.Length < 2)
            {
                filterContext.Result = new EmptyResult();
                filterContext.HttpContext.Response.Write("Error:Decoding Fail");
                filterContext.HttpContext.Response.End();
            }

            if (keyStr[0] != CommonHelper.EncryptAES(Request["view"], Keys))
            {
                filterContext.Result = new EmptyResult();
                filterContext.HttpContext.Response.Write("Error:Signature Failed");
                filterContext.HttpContext.Response.End();
            }
            //var date = keyStr[1];
            //if (!ValidateString.IsDateTime(date))
            //{
            //    filterContext.Result = new EmptyResult();
            //filterContext.HttpContext.Response.Write("Error:Encryption Failed");
            //    filterContext.HttpContext.Response.End();
            //}
            //if (DateString.DateDiff("S", Convert.ToDateTime(date), DateTime.Now) > new SysConfigHelper.SysConfigPara().ApiTimeOut)
            //{
            //    filterContext.Result = new EmptyResult();
            //    filterContext.HttpContext.Response.Write("Error:Request Timedout");
            //    filterContext.HttpContext.Response.End();
            //}
            #endregion
            var datatype = Request["datatype"] + "";
            _DataType = datatype.ToLower() == "" ? "json" : datatype;
            if (datatype.ToLower() == "xml")
            {
                Response.ContentType = "text/xml";
            }
            base.OnActionExecuting(filterContext);
        }

        /// <summary>
        /// 1:json;0:xml
        /// </summary>
        public int DataType
        {
            get { return _DataType + "" == "json" ? 1 : 0; }
        }
    }
}