package response

import (
	"github.com/gin-gonic/gin"
)

var codeMsg map[int]string

type Message struct {
	Code  int         `json:"code"`
	Msg   string      `json:"msg"`
	Data  interface{} `json:"data,omitempty"`
	Token string      `json:"token,omitempty"`
	Uid   int         `json:"uid,omitempty"`
}

//var MessageMap map[int]*Message
func NewMessage(code int) *Message {
	if _, ok := codeMsg[code]; !ok {
		code = ERR_PARM
	}
	return &Message{
		Code: code,
		Msg:  codeMsg[code],
	}
}

const (
	OK              int = 200
	LOGOUT          int = 401
	USER_PASWD_FAIL int = 402
	IP_ERR          int = 404
	NOT_ADMIN       int = 405
	PLOT_END        int = 501

	ERR_PARM int = 10000
	ERR_SYS  int = 10001
	DB_ERROR int = 10003

	ERR_PARSING int = 10004
	METHOD_ERR  int = 10005

	PLAYER_NOT_EXIST int = 5205
	PLAYER_EXIST     int = 5206

	FRIEND_ALREADY_INVITE int = 57001
	FRIEND_OFFLINE        int = 57002

	USERNAME_NOT_EXIST int = 1021
	PASSWD_ERR         int = 1022
	USERNAME_ERR       int = 1025
	IDNUM_ERR          int = 1026
	NAME_ERR           int = 1027

	TPL_UPDATE_ERR int = 2001

	TRANS_FILE_ERR        int = 3001
	TRANS_FILE_UPLOAD_ERR int = 3002
	TRANS_FILE_CREATE_ERR int = 3003
	TRANS_FILE_WRITE_ERR  int = 3004

	SERVER_OFF int = 3005

	FILTER_LIST_ERR int = 4001
)

func init() {
	codeMsg = make(map[int]string)
	codeMsg[OK] = "成功"
	codeMsg[LOGOUT] = "用户未登陆"
	codeMsg[USER_PASWD_FAIL] = "用户名或密码错误"
	codeMsg[IP_ERR] = "IP不合法"
	codeMsg[NOT_ADMIN] = "非超级管理员"

	codeMsg[PLOT_END] = "剧情结束"

	codeMsg[ERR_PARM] = "参数错误"
	codeMsg[ERR_SYS] = "系统错误"
	codeMsg[DB_ERROR] = "数据库异常"
	codeMsg[ERR_PARSING] = "解析JSON出错"
	codeMsg[METHOD_ERR] = "Method not allowed"

	codeMsg[PLAYER_NOT_EXIST] = "玩家不存在"
	codeMsg[PLAYER_EXIST] = "玩家已存在"

	codeMsg[FRIEND_ALREADY_INVITE] = "已经申请"
	codeMsg[FRIEND_OFFLINE] = "对方离线"

	codeMsg[USERNAME_NOT_EXIST] = "用户不存在"
	codeMsg[PASSWD_ERR] = "密码错误"
	codeMsg[USERNAME_ERR] = "用户名异常"
	codeMsg[IDNUM_ERR] = "身份证号码异常"
	codeMsg[NAME_ERR] = "名字异常"

	codeMsg[TPL_UPDATE_ERR] = "模板今天数据更新失败"

	codeMsg[TRANS_FILE_ERR] = "transFile err"
	codeMsg[TRANS_FILE_UPLOAD_ERR] = "Read file error"
	codeMsg[TRANS_FILE_CREATE_ERR] = "Create file error"
	codeMsg[TRANS_FILE_WRITE_ERR] = "Write file error"
	codeMsg[SERVER_OFF] = "服务器关闭"

	codeMsg[FILTER_LIST_ERR] = "敏感词更新失败"
}

func ReturnErrMsg(code int, c *gin.Context) {
	// 开始时间
	c.JSON(200, NewMessage(code))
}
