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;

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"];

                //var userData = CacheHelper.Get<Dictionary<string, string>>(token);
                var userDatastr = RedisHelper.StringGet(token);
                if (userDatastr != null)
                {
                    Dictionary<string, string> userData = new Dictionary<string, string>();
                    userData = userDatastr.ToString().ToObject<Dictionary<string, string>>();
                    var roleId = Utils.StrToInt(userData["F_RoleID"], 0);

                    //var roleId = Utils.StrToInt(userData["F_RoleID"], 0);
                    //var role = new BLL.T_Sys_RoleInfo().GetModel(roleId);
                    //if (role != null)
                    //{
                    //    isAuth = true;
                    //    //var roleFunctionList = roleFunctionBLL.GetModelList(" F_RoleId=" + role.F_RoleId);
                    //    ////var str = string.Join(",", roleFunctionList.Select(x => x.F_FunctionId).ToArray());
                    //    //var moduleFunction = new BLL.T_Sys_Function().GetModel(roleId);
                    //    //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;
            }

            //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);
            //}
        }
        private void WriteLog(AuthorizationContext filterContext)
        {
            var log = LogFactory.GetLogger(filterContext.Controller.ToString()+"/"+ filterContext.ActionDescriptor.ActionName);
            log.Error("token无效或过期，请重新登录。");
        }

    }
}