package controller

import (
	"fmt"

	"be.ems/src/framework/i18n"
	"be.ems/src/framework/reqctx"
	"be.ems/src/framework/resp"
	"be.ems/src/framework/utils/parse"
	"be.ems/src/modules/core/model"
	"be.ems/src/modules/core/service"

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

// 实例化控制层 CoreInfoController 结构体
var NewCoreInfo = &CoreInfoController{
	CoreInfoService: service.NewCoreInfo,
}

// 核心网信息请求
//
// PATH /core
type CoreInfoController struct {
	CoreInfoService *service.CoreInfo //核心网信息服务
}

// 核心网信息列表
//
// GET /list
func (s CoreInfoController) List(c *gin.Context) {
	query := reqctx.QueryMap(c)
	rows, total := s.CoreInfoService.FindByPage(query)
	c.JSON(200, resp.OkData(map[string]any{"total": total, "rows": rows}))
}

// 核心网信息信息
//
// GET /:id
func (s CoreInfoController) Info(c *gin.Context) {
	id := parse.Number(c.Param("id"))
	if id <= 0 {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty"))
		return
	}

	neHost := s.CoreInfoService.FindById(id)
	if neHost.ID != id {
		c.JSON(200, resp.ErrMsg("not found data"))
		return
	}

	c.JSON(200, resp.OkData(neHost))
}

// 核心网信息新增
//
// POST /
func (s CoreInfoController) Add(c *gin.Context) {
	var body model.CoreInfo
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 检查属性值唯一
	uniqueInfo := s.CoreInfoService.CheckUniqueGroupTitle(body.Name, body.SN, 0)
	if !uniqueInfo {
		c.JSON(200, resp.ErrMsg("name/SN already exists"))
		return
	}

	body.CreateBy = reqctx.LoginUserToUserName(c)
	insertId := s.CoreInfoService.Insert(body)
	if insertId > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// 核心网信息修改
//
// PUT /
func (s CoreInfoController) Edit(c *gin.Context) {
	var body model.CoreInfo
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 检查属性值唯一
	uniqueHostCmd := s.CoreInfoService.CheckUniqueGroupTitle(body.Name, body.SN, body.ID)
	if !uniqueHostCmd {
		c.JSON(200, resp.ErrMsg("name/SN already exists"))
		return
	}

	// 检查是否存在
	coreInfo := s.CoreInfoService.FindById(body.ID)
	if coreInfo.ID != body.ID {
		c.JSON(200, resp.ErrMsg("not found data"))
		return
	}

	coreInfo.Name = body.Name
	coreInfo.SN = body.SN
	coreInfo.TimeZone = body.TimeZone
	coreInfo.Longitude = body.Longitude
	coreInfo.Latitude = body.Latitude
	coreInfo.Address = body.Address
	coreInfo.Remark = body.Remark
	body.UpdateBy = reqctx.LoginUserToUserName(c)
	rows := s.CoreInfoService.Update(coreInfo)
	if rows > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// 核心网信息删除
//
// DELETE /:id
func (s CoreInfoController) Remove(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	id := c.Param("id")
	if id == "" {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty"))
		return
	}

	// 处理字符转id数组后去重
	uniqueIDs := parse.RemoveDuplicatesToArray(id, ",")
	// 转换成int64数组类型
	ids := make([]int64, 0)
	for _, v := range uniqueIDs {
		ids = append(ids, parse.Number(v))
	}

	rows, err := s.CoreInfoService.DeleteByIds(ids)
	if err != nil {
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
		return
	}
	msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
	c.JSON(200, resp.OkMsg(msg))
}
