using Infrastructure.Constant;
using Newtonsoft.Json;

namespace Infrastructure.Model
{
    /// <summary>
    /// 统一的API返回结果类
    /// </summary>
    public class ApiResult
    {
        /// <summary>
        /// 返回码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 返回信息
        /// </summary>
        public string Msg { get; set; }
        /// <summary>
        /// 如果data值为null，则忽略序列化将不会返回data字段
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public object Data { get; set; }

        /// <summary>
        /// 初始化一个新创建的APIResult对象，使其表示一个空消息
        /// </summary>
        public ApiResult()
        {
        }

        /// <summary>
        /// 初始化一个新创建的 ApiResult 对象
        /// </summary>
        /// <param name="code">返回码</param>
        /// <param name="msg">返回信息</param>
        public ApiResult(int code, string msg)
        {
            Code = code;
            Msg = msg;
        }

        /// <summary>
        /// 初始化一个新创建的 ApiResult 对象
        /// </summary>
        /// <param name="code">返回码</param>
        /// <param name="msg">返回信息</param>
        /// <param name="data">返回数据</param>
        public ApiResult(int code, string msg, object data)
        {
            Code = code;
            Msg = msg;
            if (data != null)
            {
                Data = data;
            }
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <returns></returns>
        public ApiResult Success()
        {
            Code = (int)ResultCode.SUCCESS;
            Msg = "success";
            return this;
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="data">数据对象</param>
        /// <returns>成功消息</returns>
        public static ApiResult Success(object data) 
        { 
            return new ApiResult(HttpStatus.SUCCESS, "success", data); 
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="msg">返回内容</param>
        /// <returns>成功消息</returns>
        public static ApiResult Success(string msg) 
        { 
            return new ApiResult(HttpStatus.SUCCESS, msg, null); 
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="msg">返回内容</param>
        /// <param name="data">返回的数据对象</param>
        /// <returns>成功消息</returns>
        public static ApiResult Success(string msg, object data)
        { 
            return new ApiResult(HttpStatus.SUCCESS, msg, data); 
        }

        /// <summary>
        /// 访问被拒
        /// </summary>
        public ApiResult On401()
        {
            Code = (int)ResultCode.DENY;
            Msg = "access denyed";
            return this;
        }

        /// <summary>
        /// 返回失败消息
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="msg">返回的失败信息</param>
        /// <returns></returns>
        public static ApiResult Error(int code, string msg) 
        { 
            return new ApiResult(code, msg); 
        }

        /// <summary>
        /// 返回失败消息 
        /// 自定义异常
        /// </summary>
        /// <param name="msg">返回的失败信息</param>
        /// <returns></returns>
        public static ApiResult Error(string msg) 
        { 
            return new ApiResult((int)ResultCode.CUSTOM_ERROR, msg); 
        }

        public override string ToString()
        {
            return $"msg={Msg},data={Data}";
        }
    }

    public class ApiResult<T> : ApiResult
    {
        public T Result { get; set; }
    }
}
