package exception

import stringUtils "eta/eta_mini_ht_api/common/utils/string"

type EtaError struct {
	ErrorCode int
	ErrorMsg  string
}

func (e *EtaError) Error() string {
	return e.ErrorMsg
}

const (
	// SysErrCode 系统错误
	SysErrCode int = iota + 10000 // iota 自动递增，从 1 开始
	UnknownError
	Unauthorized
)

// BIZErrCode  业务错误
const (
	BIZErrCode int = iota + 20000 // iota 自动递增，从 1 开始
	//短信
	IllegalCodeLength
	IllegalPhoneNumber
	SMSCodeGenerateFailed
	SendingSMSFailed
	SMSCodeAlreadySent
	SMSCodeExpired
	SMSCodeError
	GetAreaCodesFailed
	AnalystNotFound
	IllegalAreaCode
	MediaTypeError
	GetAnalystListFailed
	BatchFollowingAnalystFailed
)

// UserErrCode 用户
const (
	UserErrCode int = iota + 30000 // iota 自动递增，从 1 开始
	TemplateUserNotFound
	TemplateUserCreateFailed
	GenerateTokenFailed
	JWTTokenDecodeFailed

	JWTTokenExpired
	JWTTokenInvalid
	NotCurrentUserError
	FeedBackMsgEmpty
	FeedBackError
	IllegalFollowType
	UserFollowAnalystFailed
	GetNoticeFileError
	GetDisclaimerFileError
	AnalystNameEmptyError
	IllegalAnalystIdError
	GetFollowingAnalystListFailed
	TransferFollowingAnalystListFailed
	GetUserUnReadMsgFailed
	IllegalMessageId
	IllegalAnalystId
	ReadMessageFailed
	BindMobileFailed
	CheckFollowStatusByNamesFailed
)

// WechatErrCode 微信
const (
	WechatErrCode int = iota + 40000 // iota 自动递增，从 1 开始
	WeChatServerError
	WeChatResponseError
	WechatUserInfoFailed
	WeChatCodeEmpty
	WeChatIllegalRequest
)
const (
	ReportErrCode int = iota + 50000 // iota 自动递增，从 1 开始
	GetPublishedRandListFailed
	GetPermissionListFailed
	ReportRecordClickCountFailed
	MediaRecordClickCountFailed
	GetHotRandListFailed
	QueryReportPageFailed
	SearchReportPageFailed
	GetReportFailed
	SearchKeyEmptyError
)

const (
	MediaErrCode int = iota + 60000 // iota 自动递增，从 1 开始
	MediaFoundFailed
	GetMediaListFailed
	GetAnalystMediaListFailed
)

// ErrorMap 用于存储错误码和错误信息的映射
var ErrorMap = map[int]string{
	UnknownError:          "未知错误",
	Unauthorized:          "用户未授权",
	IllegalCodeLength:     "无效的验证码位数设置",
	IllegalPhoneNumber:    "无效的手机号码",
	SMSCodeGenerateFailed: "生成手机验证码失败",
	SendingSMSFailed:      "发送手机验证码失败",
	SMSCodeAlreadySent:    "当前手机验证码已发送，请稍后再试",
	SMSCodeExpired:        "验证码已过期",
	SMSCodeError:          "验证码错误",
	AnalystNotFound:       "研究员不存在",
	GetAreaCodesFailed:    "获取手机区号失败",
	IllegalAreaCode:       "无效的区号",
	MediaTypeError:        "媒体类型非法",
	GetAnalystListFailed:  "获取研究员列表失败",
	//用户
	TemplateUserNotFound:               "临时用户记录不存在",
	TemplateUserCreateFailed:           "创建临时用户失败",
	GenerateTokenFailed:                "创建token失败",
	JWTTokenDecodeFailed:               "token解析失败",
	JWTTokenExpired:                    "token已过期",
	JWTTokenInvalid:                    "token无效",
	NotCurrentUserError:                "用户信息不一致,非当前手机用户操作",
	FeedBackMsgEmpty:                   "反馈信息不能为空",
	FeedBackError:                      "提交反馈信息失败",
	IllegalFollowType:                  "无效的关注类型",
	UserFollowAnalystFailed:            "关注研究员失败",
	GetNoticeFileError:                 "获取注册须知失败",
	GetDisclaimerFileError:             "获取免责声明失败",
	AnalystNameEmptyError:              "研究员姓名不能为空",
	GetFollowingAnalystListFailed:      "获取关注研究员列表失败",
	TransferFollowingAnalystListFailed: "转换关注研究员列表失败",
	GetUserUnReadMsgFailed:             "获取未读消息列表失败",
	IllegalMessageId:                   "非法的消息ID",
	ReadMessageFailed:                  "已读消息失败",
	IllegalAnalystId:                   "研究员Id非法",
	CheckFollowStatusByNamesFailed:     "获取关注猪状态失败",
	BatchFollowingAnalystFailed:        "批量关注研究员列表失败",
	//微信
	WeChatServerError:    "微信服务器发生错误",
	WechatUserInfoFailed: "获取微信用户信息失败",
	WeChatResponseError:  "解析微信响应数据失败",
	WeChatCodeEmpty:      "微信获取用户信息,code不能为空",
	WeChatIllegalRequest: "不合法的微信请求",
	//研报
	GetPublishedRandListFailed:   "获取已发布研报列表失败",
	GetPermissionListFailed:      "获取品种列表失败",
	ReportRecordClickCountFailed: "添加点击访问次数失败",
	MediaRecordClickCountFailed:  "添加媒体点击访问次数失败",
	GetHotRandListFailed:         "获取热门研报列表失败",
	QueryReportPageFailed:        "分页查询报告列表失败",
	SearchReportPageFailed:       "分页搜索报告列表失败",
	GetReportFailed:              "获取研报详情失败",
	SearchKeyEmptyError:          "搜索关键字不能为空",
	//媒体
	MediaFoundFailed:          "查询媒体信息失败",
	GetMediaListFailed:        "查询媒体列表失败",
	GetAnalystMediaListFailed: "查询研究员媒体列表失败",
	BindMobileFailed:          "绑定手机号失败",
}

func Equals(code int, message string) bool {
	if stringUtils.IsEmptyOrNil(message) {
		return false
	}
	return ErrorMap[code] == message
}

func GetMsg(code int) string {
	return ErrorMap[code]
}
func newException(code int, msg string) error {
	return &EtaError{
		ErrorCode: code,
		ErrorMsg:  msg,
	}
}

func New(code int) *EtaError {
	err := ErrorMap[code]
	if stringUtils.IsBlank(err) {
		return newException(UnknownError, ErrorMap[UnknownError]).(*EtaError)
	}
	return newException(code, err).(*EtaError)
}
