using CallCenter.Utility;
using CallCenterApi.Interface.Models.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.Base
{
    public class BaseController : Controller
    {
        public new FormsPrincipal<Model.T_Sys_UserAccount> CurrentUser
        {
            get
            {
                try
                {
                    return (FormsPrincipal<Model.T_Sys_UserAccount>)base.User;
                }
                catch
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// Log4net 日志
        /// </summary>
        public Log FileLog
        {
            get { return LogFactory.GetLogger(this.GetType().ToString()); }
        }

        /// <summary>
        /// 成功的消息格式化
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual ActionResult Success(string message)
        {
            var jsonMsg = new AjaxResult { state = ResultTypes.success.ToString(), message = message }.ToJson();
            FileLog.Info(CurrentUser?.UserData.F_UserCode ?? "" + jsonMsg);
            return Content(jsonMsg);
        }

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

        /// <summary>
        /// 跳转的消息格式化
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual ActionResult 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 ActionResult Error(string message)
        {
            var jsonMsg = new AjaxResult { state = ResultTypes.error.ToString(), message = message }.ToJson();
            FileLog.Error(CurrentUser?.UserData.F_UserCode ?? "" + jsonMsg);
            return Content(jsonMsg);
        }

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

        /// <summary>
        /// 未授权
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual ActionResult UnAuthorized(string message)
        {
            var jsonMsg = new AjaxResult { state = ResultTypes.unauthorized.ToString(), message = message }.ToJson();
            FileLog.Error(jsonMsg);
            return Content(jsonMsg);
        }

    }
}