using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace JiaZhiQuan.Common
{
    public static class ErrorCodes
    {
        private static Dictionary<int, string> codeDescriptionMapping = new Dictionary<int, string>();

        static ErrorCodes()
        {
            var members = typeof(ErrorCodes).GetFields();
            members.ForEach(e =>
            {
                var attr = Attribute.GetCustomAttribute(e, typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attr != null)
                {
                    codeDescriptionMapping.Add((int)e.GetRawConstantValue(), attr.Description);
                }
            });
        }

        public const int Success = 200;

        [Description("需要登录")]
        public const int NeedLogin = 401;

        [Description("无权限")]
        public const int PermissionDenied = 403;

        [Description("不存在")]
        public const int NotExist = 404;


        [Description("程序处理错误")]
        public const int Exception = 500;

        [Description("参数错误")]
        public const int ParameterError = 501;


        [Description("用户注册时，用户已存在")]
        public const int UserRegisterAlreadyExistError = 601;

        [Description("用户登录时用户名或密码不正确")]
        public const int UserLoginUserNameOrPasswordError = 602;

        [Description("用户被禁用，无法登录")]
        public const int UserDisabledError = 603;
        [Description("用户不存在")]
        public const int UserNotExistError = 604;

        [Description("微信授权失败")]
        public const int WXAuthorizationError = 605;
        [Description("新浪微博授权失败")]
        public const int WeiboAuthorizationError = 606;

        [Description("账号在其他设备登录")]
        public const int LogoutByMultiLogin = 607;


        [Description("上传文件格式不正确")]
        public const int UploadFileFormatError = 611;

        [Description("上传文件太大")]
        public const int UploadFileSizeLimitError = 612;

        [Description("上传到OSS失败")]
        public const int UploadFileToOSSError = 613;

        [Description("上传时未选择图片")]
        public const int UploadFileNoFileError = 614;

        [Description("已经签到了，不能重复签到")]
        public const int AlreadySignOn = 615;

        [Description("粉钻不足")]
        public const int PointsNotEnough = 631;
        [Description("商品库存不足")]
        public const int StockNotEnough = 632;
        [Description("锂币不足")]
        public const int CoinNotEnough = 633;


        [Description("购买商品过期")]
        public const int OrderProductionDisasbled = 640;
        [Description("苹果内购验证失败")]
        public const int OrderApplePayValidationError = 641;
        [Description("PayNo已使用")]
        public const int OrderPayNoHasBeenUsed = 642;
        [Description("订单已支付，不能重复支付")]
        public const int OrderPaied = 643;
        [Description("支付失败（其他错误）")]
        public const int OrderPayFailed = 644;
        [Description("订单不支持此操作")]
        public const int OrderOperationNotAllowed = 645;
        [Description("订单退单金额超限")]
        public const int OrderRefundAmountLimit = 646;
        [Description("订单退单金额超限")]
        public const int InvoiceTitleLimit = 647;


        [Description("短信发送太频繁（未达到当日最大发送量）")]
        public const int SMSendBusinessLimitError = 681;

        [Description("短信当天发送太频繁（已达到当日最大发送量）")]
        public const int SMSendDayLimitError = 682;

        [Description("短信手机号不正确")]
        public const int SMSendPhoneNumberError = 683;


        [Description("活动已报过名")]
        public const int ActivityEnrolled = 700;
        [Description("活动报名未开始")]
        public const int ActivityEnrollNotStarted = 701;
        [Description("活动报名已结束")]
        public const int ActivityEnrollEnded = 702;
        [Description("活动类型为4的组已满员")]
        public const int ActivityT4GroupIsFull = 703;


        [Description("购物车已满")]
        public const int ShoppingCartIsFull = 801;
        [Description("购物车是空的，不能购买")]
        public const int ShoppingCartIsEmpty = 802;
        [Description("商品暂时不能购买")]
        public const int GoodsNotAvailable = 803;
        [Description("预订单中的商品价格发生变更")]
        public const int PreorderGoodsInfoChanged = 804;

        [Description("商品类目不存在")]
        public const int CategoryNotExist = 805;

        [Description("商品属性值需要刷新")]
        public const int CategoryRefresh = 806;

        [Description("由于隐私导致无权限访问")]
        public const int PrivacyDenied = 900;

        /// <summary>
        /// 通过Code值，获取对应的描述
        /// </summary>
        public static string GetDescription(int code)
        {
            if (codeDescriptionMapping.ContainsKey(code))
            {
                return codeDescriptionMapping[code];
            }
            return string.Empty;
        }
    }
}
