package controller

import (
	"strings"

	"nms_nbi/src/framework/i18n"
	"nms_nbi/src/framework/utils/ctx"
	"nms_nbi/src/framework/utils/parse"
	"nms_nbi/src/framework/vo/result"
	"nms_nbi/src/modules/network_element/model"
	neService "nms_nbi/src/modules/network_element/service"

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

// 实例化控制层 NeHostCmdController 结构体
var NewNeHostCmd = &NeHostCmdController{
	neHostCmdService: neService.NewNeHostCmdImpl,
}

// 网元主机命令请求
//
// PATH /hostCmd
type NeHostCmdController struct {
	// 网元主机命令服务
	neHostCmdService neService.INeHostCmd
}

// 网元主机命令列表
//
// GET /list
func (s *NeHostCmdController) List(c *gin.Context) {
	querys := ctx.QueryMap(c)
	data := s.neHostCmdService.SelectPage(querys)

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

// 网元主机命令信息
//
// GET /:cmdId
func (s *NeHostCmdController) Info(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	cmdId := c.Param("cmdId")
	if cmdId == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	neHost := s.neHostCmdService.SelectById(cmdId)
	if neHost.CmdID != cmdId {
		// 没有可访问主机命令数据！
		c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHostCmd.noData")))
		return
	}

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

// 网元主机命令新增
//
// POST /
func (s *NeHostCmdController) Add(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body model.NeHostCmd
	err := c.ShouldBindBodyWith(&body, binding.JSON)
	if err != nil || body.CmdID != "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 检查属性值唯一
	uniqueHostCmd := s.neHostCmdService.CheckUniqueGroupTitle(body.GroupID, body.Title, body.CmdType, "")
	if !uniqueHostCmd {
		// 主机命令操作【%s】失败，同组内名称已存在
		msg := i18n.TTemplate(language, "neHostCmd.errKeyExists", map[string]any{"name": body.Title})
		c.JSON(200, result.ErrMsg(msg))
		return
	}

	body.CreateBy = ctx.LoginUserToUserName(c)
	insertId := s.neHostCmdService.Insert(body)
	if insertId != "" {
		c.JSON(200, result.Ok(nil))
		return
	}
	c.JSON(200, result.Err(nil))
}

// 网元主机命令修改
//
// PUT /
func (s *NeHostCmdController) Edit(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body model.NeHostCmd
	err := c.ShouldBindBodyWith(&body, binding.JSON)
	if err != nil || body.CmdID == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 检查属性值唯一
	uniqueHostCmd := s.neHostCmdService.CheckUniqueGroupTitle(body.GroupID, body.Title, body.CmdType, body.CmdID)
	if !uniqueHostCmd {
		// 主机命令操作【%s】失败，同组内名称已存在
		msg := i18n.TTemplate(language, "neHostCmd.errKeyExists", map[string]any{"name": body.Title})
		c.JSON(200, result.ErrMsg(msg))
		return
	}

	// 检查是否存在
	neHost := s.neHostCmdService.SelectById(body.CmdID)
	if neHost.CmdID != body.CmdID {
		// 没有可访问主机命令数据！
		c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHostCmd.noData")))
		return
	}

	body.UpdateBy = ctx.LoginUserToUserName(c)
	rows := s.neHostCmdService.Update(body)
	if rows > 0 {
		c.JSON(200, result.Ok(nil))
		return
	}
	c.JSON(200, result.Err(nil))
}

// 网元主机命令删除
//
// DELETE /:cmdIds
func (s *NeHostCmdController) Remove(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	cmdIds := c.Param("cmdIds")
	if cmdIds == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}
	// 处理字符转id数组后去重
	ids := strings.Split(cmdIds, ",")
	uniqueIDs := parse.RemoveDuplicates(ids)
	if len(uniqueIDs) <= 0 {
		c.JSON(200, result.Err(nil))
		return
	}
	rows, err := s.neHostCmdService.DeleteByIds(uniqueIDs)
	if err != nil {
		c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
		return
	}
	msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
	c.JSON(200, result.OkMsg(msg))
}
