using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace MadRunFabric.Common
{
    public class BaseController : Controller
    {
        /// <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();
            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();

            #region 替换地址 - 替换数据库记录中的旧地址122.114.109.238（新地址122.114.231.155 yunwei.nuodajituan.com） - 临时处理
            if (jsonMsg.Contains("122.114.109.238"))
            {
                jsonMsg = jsonMsg.Replace("122.114.109.238", "yunwei.nuodajituan.com");
            }
            #endregion
            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();
            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();
            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();
            return Content(jsonMsg);
        }

    }
}
