using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.Interface.Models.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Script.Serialization;
using System.Web.Security;

namespace CallCenterApi.Interface
{
    public class AuthorityAttribute : AuthorizeAttribute
    {
        //private BLL.T_Sys_RoleFunction roleFunctionBLL = new BLL.T_Sys_RoleFunction();
        /// <summary>
        /// 权限验证
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            //bool isAuth = false;

            if (filterContext.RequestContext.HttpContext.Request.IsAuthenticated)
            {
                var actionDescriptor = filterContext.ActionDescriptor;
                var controllerDescriptor = actionDescriptor.ControllerDescriptor;
                var controller = controllerDescriptor.ControllerName;
                var action = actionDescriptor.ActionName;
                var token = filterContext.HttpContext.Request["token"];
                if (!string.IsNullOrEmpty(token))
                {
                    //var userData = CacheHelper.Get<Cache.Models.CurrentUserInfo>(token);
                    //var roleId = userData.F_RoleId;
                    //var role = new BLL.T_Sys_RoleInfo().GetModel(roleId);
                    //if (role != null)
                    //{
                    //    var roleFunctionList = roleFunctionBLL.DataTableToList(roleFunctionBLL.GetList(" F_RoleId=" + role.F_RoleId).Tables[0]);
                    //    //var str = string.Join(",", roleFunctionList.Select(x => x.F_FunctionId).ToArray());
                    //    var moduleFunction = new BLL.T_Sys_ModuleFunctions().GetModel(controller, action);
                    //    if (moduleFunction != null)
                    //    {
                    //        var single = roleFunctionList.SingleOrDefault(x => x.F_FunctionId == moduleFunction.F_FunctionId);
                    //        if (single != null)
                    //        {
                    //            isAuth = true;
                    //        }
                    //    }
                    //}
                }
                else
                {
                    filterContext.HttpContext.Response.StatusCode = 200;
                    filterContext.Result = new ContentResult { Content = new AjaxResult { state = ResultTypes.notoken.ToString(), message = "未传入token，请重新登录。" }.ToJson() };
                    WriteLog(filterContext);
                    return;
                }
            }
            else
            {
                filterContext.HttpContext.Response.StatusCode = 200;
                filterContext.Result = new ContentResult { Content = new AjaxResult { state = ResultTypes.notoken.ToString(), message = "未知错误，请重新登录" }.ToJson() };
                WriteLog(filterContext);
                return;
            }

            //if (!isAuth)
            //{
            //    filterContext.HttpContext.Response.StatusCode = 200;
            //    filterContext.Result = new ContentResult { Content = new AjaxResult { state = ResultTypes.unauthorized.ToString(), message = "对不起，您无权访问！" }.ToJson() };
            //    WriteLog(filterContext);
            //}
            //else { base.OnAuthorization(filterContext); }
            base.OnAuthorization(filterContext);
        }
        private void WriteLog(AuthorizationContext filterContext)
        {
            if (filterContext == null)
                return;
            var log = LogFactory.GetLogger(filterContext.Controller.ToString());
            log.Error(filterContext.Result);
        }

    }
}