package service

import (
	"fmt"
	"fonchain-fiee/pkg/common"
	"fonchain-fiee/pkg/e"
	"fonchain-fiee/pkg/logic"
	"fonchain-fiee/pkg/serializer"
	"fonchain-fiee/pkg/service/approval/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

const (
	Failed = 1
	Ok     = 0

	RetryCode = 2
)

// Response 基础序列化器
type Response struct {
	Status int         `json:"status"`
	Data   interface{} `json:"data"`
	Msg    string      `json:"msg"`
	Code   int         `json:"code"`
	Error  error       `json:"error"`
}

// Success Success(c,someMap) datas传结构体中文会有乱码问题
func Success(c *gin.Context, datas ...interface{}) {
	var data interface{}
	if datas != nil {
		data = datas[0]
	} else {
		data = struct{}{}
	}
	c.JSON(http.StatusOK, Response{
		Status: Ok,
		Code:   Ok,
		Data:   data,
		Msg:    "ok",
	})
	c.Abort()
}

func Success1(c *gin.Context, msg string, datas ...interface{}) {
	var data interface{}

	if datas != nil {
		data = datas[0]
	} else {
		data = struct{}{}
	}
	c.JSON(http.StatusOK, Response{
		Status: Ok,
		Code:   Ok,
		Data:   data,
		Msg:    msg,
	})
	c.Abort()
}

// Error 统一错误返回
func ErrorWeb(c *gin.Context, code int, err error, msg ...string) {

	fmt.Println("err:", err)
	status := e.Failed
	if code == e.NotLogin {
		status = e.NotLogin
	}

	if code == e.NotLoginSqueeze {
		status = e.NotLoginSqueeze
	}

	var msgStr string
	if msg != nil {
		msgStr = msg[0]
	} else if err != nil {
		msgStr = err.Error()
	}

	// 国际化
	if e.VerifyMsgKey(msgStr) {
		msgStr = logic.ConvertOaMsg(c, msgStr)
	} else {
		lang := c.Request.Header.Get("Accept-Language")
		msgStr = model.GetLanguageType(lang, msgStr)
	}

	c.JSON(e.Success, serializer.Response{
		Status: status,
		Msg:    msgStr,
		Data:   nil,
	})

	c.Abort()
}

// Error 统一错误返回
func Error(c *gin.Context, err error) {

	errMsg := ""
	if err != nil {
		errMsg = translateErrorMessage(c, err.Error())
	}

	c.JSON(http.StatusOK, Response{
		Code:   Failed,
		Status: Failed,
		Msg:    errMsg,
		Data:   struct{}{},
	})

	c.Abort()
}

// Error 统一错误返回
func Error1(c *gin.Context, err error) {

	c.JSON(http.StatusOK, Response{
		Code:   Failed,
		Status: Failed,
		Msg:    err.Error(),
		Data:   struct{}{},
	})

	c.Abort()
}

// 重试
func Retry(c *gin.Context, err error) {
	errMsg := ""
	if err != nil {
		errMsg = err.Error()
	}

	c.JSON(http.StatusOK, Response{
		Code: RetryCode,
		Msg:  errMsg,
		Data: struct{}{},
	})

	c.Abort()
}

// NotLoginRes 未登录的返回
func NotLoginRes(c *gin.Context, msg string) {

	c.JSON(e.Success, serializer.Response{
		Code:   401,
		Status: 1,
		Msg:    msg,
		Data:   nil,
	})

	c.Abort()
}

func NotLoginError(c *gin.Context, err error) {

	errMsg := ""
	if err != nil {
		errMsg = translateErrorMessage(c, err.Error())
	}

	c.JSON(http.StatusUnauthorized, Response{
		Code: Failed,
		Msg:  errMsg,
		Data: struct{}{},
	})

	c.Abort()
}

func translateErrorMessage(c *gin.Context, message string) string {
	lang := c.GetHeader("Accept-Language")
	switch lang {
	case "zh-CN":
		return message
	case "zh-TW":
		return common.ZhTWMessages[message]
	case "de-DE":
		return common.DeDEMessages[message]
	case "ja-JP":
		return common.JaJPMessages[message]
	default:
		return common.EnMessages[message]
	}
}
