using System.ComponentModel;
using System.DirectoryServices.Protocols;

namespace QYZH.InteractiveMagazine.Models.Dto;

/// <summary>
/// 操作响应基类
/// </summary>
public class BaseResponse
{
    /// <summary>
    ///  构造函数，初始化操作响应基类
    /// </summary>
    public BaseResponse()
    {
        code = ResultCode.SUCCESS;
    }

    #region 公共属性

    /// <summary>
    /// 操作描述
    /// </summary>
    public string message { get; set; }

    /// <summary>
    /// 结果Code
    /// </summary>
    public ResultCode code { get; set; }

    #endregion 公共属性

    #region 公用方法

    /// <summary>
    /// 成功
    /// </summary>
    public static BaseResponse Success()
    {
        return new BaseResponse { code = ResultCode.SUCCESS, message = "操作成功。" };
    }

    /// <summary>
    /// 成功
    /// </summary>
    public static BaseResponse Success(string message = "操作成功。")
    {
        return new BaseResponse { code = ResultCode.SUCCESS, message = message };
    }

    /// <summary>
    /// 失败
    /// </summary>
    public static BaseResponse Fail(string message, ResultCode code = ResultCode.FAIL)
    {
        return new BaseResponse { code = code, message = message };
    }

    /// <summary>
    /// 权限验证失败
    /// </summary>
    public static BaseResponse AuthFail(string message, ResultCode code = ResultCode.FAIL)
    {
        return new BaseResponse { code = code, message = message };
    }

    #endregion 公用方法

    /// <summary>
    ///     操作结果
    /// </summary>
    public bool isSuccess => code == ResultCode.SUCCESS;
}

/// <summary>
/// 响应结果类型
/// </summary>
/// <typeparam name="T">
/// </typeparam>
public class BaseResponse<T> : BaseResponse
{
    /// <summary>
    ///     操作结果
    /// </summary>
    public T? result { get; set; }

    #region 公用方法

    /// <summary>
    /// 成功
    /// </summary>
    public static BaseResponse<T> Success(T result)
    {
        return new BaseResponse<T> { code = ResultCode.SUCCESS, message = "", result = result };
    }

    /// <summary>
    /// 成功
    /// </summary>
    public static BaseResponse<T> Success(T result, string message = "操作成功。")
    {
        return new BaseResponse<T> { code = ResultCode.SUCCESS, message = message, result = result };
    }

    /// <summary>
    /// 失败
    /// </summary>
    public static BaseResponse<T> Fail(string message = "fail")
    {
        return new BaseResponse<T> { code = ResultCode.FAIL, message = message };
    }

    /// <summary>
    /// 失败 自定义结果
    /// </summary>
    public static BaseResponse<T> Fail(ResultCode code, string message = "fail")
    {
        return new BaseResponse<T> { code = code, message = message };
    }

    public static BaseResponse<T> Fail(T result, string message = "操作失败,请稍后再试")
    {
        return new BaseResponse<T> { code = ResultCode.FAIL, message = message, result = result };
    }

    /// <summary>
    /// 权限验证失败
    /// </summary>
    public static BaseResponse<T> AuthFail(string message = "Permission authentication failed")
    {
        return new BaseResponse<T> { code = ResultCode.OAUTH_FAIL, message = message };
    }

    ///// <summary>
    ///// 成功
    ///// </summary>
    //public static BaseResponse<T> Assert(T result, string message = "")
    //{
    //    if (result is bool b)
    //        return b ? BaseResponse<T>.Success(result) : BaseResponse<T>.Fail(message);
    //    if (result is object o)
    //        return (data == null || data.IsNull() == true) ? BaseResponse<T>.Success(result) : BaseResponse<T>.Fail(message);
    //    return new BaseResponse<T> { Code = ResultCode.SUCCESS, Message = message, Result = result };
    //}


    #endregion 公用方法

    /// <summary>
    ///     操作结果
    /// </summary>
    public bool isSuccess => code == ResultCode.SUCCESS;
}


public enum ResultCode
{
    [Description("success")]
    SUCCESS = 200,

    [Description("没有更多数据")]
    NO_DATA = 210,

    [Description("参数错误")]
    PARAM_ERROR = 101,

    [Description("验证码错误")]
    CAPTCHA_ERROR = 103,

    [Description("登录错误")]
    LOGIN_ERROR = 105,

    [Description("操作失败")]
    FAIL = 1,

    [Description("服务端出错啦")]
    GLOBAL_ERROR = 500,

    [Description("自定义异常")]
    CUSTOM_ERROR = 110,

    [Description("非法请求")]
    INVALID_REQUEST = 116,

    [Description("授权失败")]
    OAUTH_FAIL = 201,

    [Description("请先绑定手机号")]
    PHONE_BIND = 202,

    [Description("未授权")]
    DENY = 401,

    [Description("授权访问失败")]
    FORBIDDEN = 403,

    [Description("Bad Request")]
    BAD_REQUEST = 400,

    [Description("资源不存在")]
    NOT_FOUND = 404,

    [Description("冲突/重复提交")]
    CONFLICT = 409,

    [Description("业务规则校验失败")]
    UNPROCESSABLE_ENTITY = 422,

    [Description("请求过于频繁")]
    TOO_MANY_REQUESTS = 429,

    [Description("第三方服务异常")]
    BAD_GATEWAY = 502,

    [Description("服务不可用")]
    SERVICE_UNAVAILABLE = 503
}
