package response

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/utils"
	"strings"
)

var (
	OK_CODE                 = 200  //业务成功
	FAIL_CODE               = 400  //业务错误
	TOKEN_ERROR_CODE        = 401  //toke异常
	NO_AUTH                 = 403  //没有权限
	SPECIFIC_FAIL_CODE      = 4001 // 业务指定错误
	REPORT_NOT_RELEASE_CODE = 4002 // 报告未发布错误
)

type ResultData struct {
	Code   int         `json:"code" description:"状态码"`
	Msg    string      `json:"msg" description:"提示信息"`
	Data   interface{} `json:"data" description:"返回数据"`
	ErrMsg string      `json:"-" description:"错误信息，不用返回给前端，只是做日志记录"`
}

func result(code int, resultData ResultData, c *gin.Context) {
	jsonByte, _ := json.Marshal(resultData)
	logSlice := utils.GetContextLogListByClaims(c)
	logSlice = append(logSlice, fmt.Sprint("ResultData:", string(jsonByte)))

	//记录错误日志
	if resultData.ErrMsg != "" {
		logSlice = append(logSlice, fmt.Sprint("ErrMsg:", resultData.ErrMsg))
		//global.LOG.Info(strings.Join(logSlice, "；"))
	}
	global.LOG.Info(strings.Join(logSlice, "\n"))

	// 测试环境不加密
	if global.CONFIG.Serve.RunMode == "debug" {
		c.JSON(code, resultData)
	} else {
		encryptResult := utils.DesBase64Encrypt(jsonByte)
		c.JSON(code, string(encryptResult))
	}
	c.Abort()
}

// OK 操作成功
func Ok(msg string, c *gin.Context) {
	resultData := ResultData{
		Code: OK_CODE,
		Msg:  msg,
	}
	result(200, resultData, c)
}

// OkData 成功返回数据
func OkData(msg string, data interface{}, c *gin.Context) {
	resultData := ResultData{
		Code: OK_CODE,
		Msg:  msg,
		Data: data,
	}
	result(200, resultData, c)
}

// Fail 操作失败
func Fail(msg string, c *gin.Context) {
	resultData := ResultData{
		Code: FAIL_CODE,
		Msg:  msg,
	}
	result(200, resultData, c)
}

// FailData 成功返回数据
func FailData(msg string, data interface{}, c *gin.Context) {
	resultData := ResultData{
		Code: FAIL_CODE,
		Msg:  msg,
		Data: data,
	}
	result(200, resultData, c)
}

// Custom 自定义状态码+操作成功
func Custom(code int, msg string, c *gin.Context) {
	resultData := ResultData{
		Code: code,
		Msg:  msg,
	}
	result(200, resultData, c)
}

// CustomData 自定义状态码+返回数据
func CustomData(code int, msg string, data interface{}, c *gin.Context) {
	resultData := ResultData{
		Code: code,
		Msg:  msg,
		Data: data,
	}
	result(200, resultData, c)
}

// TokenError token异常
func TokenError(data interface{}, message, errMsg string, c *gin.Context) {
	resultData := ResultData{
		Code:   TOKEN_ERROR_CODE,
		Msg:    message,
		Data:   data,
		ErrMsg: errMsg,
	}
	result(200, resultData, c)
}

// AuthError 没有权限
func AuthError(data interface{}, message string, c *gin.Context) {
	resultData := ResultData{
		Code: NO_AUTH,
		Msg:  message,
		Data: data,
	}
	result(200, resultData, c)
}

// SpecificFail 业务指定错误
func SpecificFail(data interface{}, message string, c *gin.Context) {
	resultData := ResultData{
		Code: SPECIFIC_FAIL_CODE,
		Msg:  message,
		Data: data,
	}
	result(200, resultData, c)
}

// FailMsg 操作失败
func FailMsg(msg, errMsg string, c *gin.Context) {
	resultData := ResultData{
		Code:   FAIL_CODE,
		Msg:    msg,
		ErrMsg: errMsg,
	}
	result(200, resultData, c)
}

type BaseResponse struct {
	Ret         int
	Msg         string
	ErrMsg      string
	ErrCode     string
	Data        interface{}
	Success     bool `description:"true 执行成功，false 执行失败"`
	IsSendEmail bool `json:"-" description:"true 发送邮件，false 不发送邮件"`
	IsAddLog    bool `json:"-" description:"true 新增操作日志，false 不新增操作日志" `
}

// NotReleaseMsg 报告未发布错误
func NotReleaseMsg(msg, errMsg string, c *gin.Context) {
	resultData := ResultData{
		Code:   REPORT_NOT_RELEASE_CODE,
		Msg:    msg,
		ErrMsg: errMsg,
	}
	result(200, resultData, c)
}
