using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Claims;
using System.Linq;

namespace System.Common
{
    public class BaseController : ControllerBase
    {
        public Logger LogDefault
        {
            get
            {
                return new Logger(NLog.LogManager.GetLogger(this.GetType().ToString()));
            }
        }
        /// <summary>
        /// 成功的消息格式化
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual IActionResult Success(string message)
        {
            var jsonMsg = new AjaxResult { state = ResultTypes.success.ToString(), message = message }.ToJson();
            LogDefault.Info(jsonMsg);
            return Content(jsonMsg);
        }

        /// <summary>
        /// 成功的消息格式化
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual IActionResult Success(string message, object data)
        {
            var jsonMsg = new AjaxResult { state = ResultTypes.success.ToString(), message = message, data = data }.ToJson();
            LogDefault.Info(jsonMsg);
            return Content(jsonMsg);
        }

        /// <summary>
        /// 跳转的消息格式化
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual IActionResult Redirect(string message, object data)
        {
            return Content(new AjaxResult { state = ResultTypes.redirect.ToString(), message = message, data = data }.ToJson());
        }

        /// <summary>
        /// 错误的消息格式化
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual IActionResult Error(string message)
        {
            var jsonMsg = new AjaxResult { state = ResultTypes.error.ToString(), message = message }.ToJson();
            LogDefault.Error(jsonMsg);
            return Content(jsonMsg);
        }

        /// <summary>
        /// 未登录或者登录已过期
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual IActionResult NoToken(string message)
        {
            var jsonMsg = new AjaxResult { state = ResultTypes.notoken.ToString(), message = message }.ToJson();
            LogDefault.Info(jsonMsg);
            return Content(jsonMsg);
        }

        /// <summary>
        /// 未授权
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual IActionResult UnAuthorized(string message)
        {
            var jsonMsg = new AjaxResult { state = ResultTypes.unauthorized.ToString(), message = message }.ToJson();
            LogDefault.Error(jsonMsg);
            return Content(jsonMsg);
        }
        protected UserModel UserLogin
        {
            get
            {
                try
                {
                    return
                    new UserModel()
                    {
                        UserId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.PrimarySid).Value,
                        UserCode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value,
                        UserName = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value,
                        RoleId = int.Parse(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role).Value),
                        RoleCode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value,
                        WxOpenId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.DenyOnlySid).Value
                    };
                }
                catch { return null; }
            }
        }

    }
}