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.NewCommontImpl,
}

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

// 哈希加密
//
// 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")
	ivs := i18n.TTemplate(language, "errorFields", nil)
	errorFields := i18n.TTemplate(language, "errorFields", map[string]any{
		"num":      1000,
		"hello":    "你好",
		"h2o2":     false,
		"fileName": " ====",
	})
	c.JSON(200, map[string]any{
		"lang":        language,
		"i18nLang":    i18nLang,
		"ivs":         ivs,
		"hello":       hello,
		"errorFields": errorFields,
	})
}

// 系统可暴露的配置信息
//
// GET /sys-conf
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))
}
