package controller

import (
	"be.ems/src/framework/i18n"
	"be.ems/src/framework/utils/ctx"
	"be.ems/src/framework/vo/result"
	commonService "be.ems/src/modules/common/service"
	"github.com/gin-gonic/gin"
)

// 实例化控制层 CommontController 结构体
var NewCommont = &CommontController{
	commontService: commonService.NewCommont,
}

// 通用请求
//
// PATH /
type CommontController struct {
	commontService *commonService.Commont // 通用请求服务
}

// 哈希加密
//
// POST /hash
func (s *CommontController) Hash(c *gin.Context) {
	c.String(200, "commont Hash")
}

// 多语言处理
//
// GET /i18n
func (s *CommontController) I18n(c *gin.Context) {
	language := ctx.AcceptLanguage(c)

	i18nLang := i18n.TKey(language, "i18n")
	hello := i18n.TKey(language, "hello")
	errorFields := i18n.TTemplate(language, "dictData.errLabelExists", map[string]any{
		"name": " ====",
	})
	c.JSON(200, map[string]any{
		"lang":        language,
		"i18nLang":    i18nLang,
		"hello":       hello,
		"errorFields": errorFields,
	})
}

// 系统的配置信息
//
// GET /sys-conf
//
//	@Tags			common
//	@Accept			json
//	@Produce		json
//	@Success		200	{object}	object	"Response Results"
//	@Summary		Configuration information for the system
//	@Description	Configuration information for the system
//	@Router			/sys-conf [get]
func (s *CommontController) SysConfig(c *gin.Context) {
	data := s.commontService.SystemConfigInfo()

	// 闭包函数处理多语言
	language := ctx.AcceptLanguage(c)
	converI18n := func(language string, arr *map[string]string) {
		for k, v := range *arr {
			(*arr)[k] = i18n.TKey(language, v)
		}
	}
	converI18n(language, &data)

	c.JSON(200, result.OkData(data))
}
