package sysstatuscode

import (
	"fmt"
	"go-admin/app/admin/models/sysmodel"
	"go-admin/app/admin/service/common"
	"go-admin/app/admin/service/sysdb"
	"go-admin/pkg/utility"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

var (
	statusCodeMap = make(map[int]sysmodel.StatusCode)
)

// InitStatusCodeLanguage 初始化状态码语言包
func InitStatusCodeLanguage(orm *gorm.DB) {
	statusCodeList := sysdb.GetAllStatusCode(orm)
	for _, code := range statusCodeList {
		statusCodeMap[code.Code] = code
	}
}

// GetStatusCodeLanguage 获取状态码多语言
func GetStatusCodeLanguage(code int, language string) string {
	c, ok := statusCodeMap[code]
	if !ok {
		return utility.IntToString(code)
	}
	switch language {
	case "en":
		return c.En
	case "zh-CN":
		return c.ZhCN
	case "zh-HK":
		return c.ZhHK
	default:
		return c.Explain
	}

	//if strings.EqualFold("en", language) {
	//	return c.En
	//} else if strings.EqualFold("zh-CN", language) {
	//	return c.ZhCN
	//} else if strings.EqualFold("zh-HK", language) {
	//	return c.ZhHK
	//} else {
	//	//默认返回
	//	return c.Explain
	//}
}

func GetStatusCodeDescription(c *gin.Context, code int) string {
	lang := common.GetLanguage(c)

	return GetStatusCodeLanguage(code, lang)
}

func GetStatusCodeDiscreptionArgs(c *gin.Context, code int, langArg interface{}) string {
	lang := common.GetLanguage(c)
	description := GetStatusCodeLanguage(code, lang)

	if _, ok := langArg.(int); ok {
		description = fmt.Sprintf(description, langArg)
	} else if _, ok := langArg.(int64); ok {
		description = fmt.Sprintf(description, langArg)
	}

	return description
}
