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"
)

// 实例化控制层 NeSoftwareController 结构体
var NewNeSoftware = &NeSoftwareController{
	neSoftwareService: neService.NewNeSoftwareImpl,
}

// 网元软件包信息请求
//
// PATH /software
type NeSoftwareController struct {
	// 网元软件包信息服务
	neSoftwareService neService.INeSoftware
}

// 网元软件包信息列表
//
// GET /list
func (s *NeSoftwareController) List(c *gin.Context) {
	querys := ctx.QueryMap(c)
	data := s.neSoftwareService.SelectPage(querys)

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

// 网元软件包信息信息
//
// GET /:softwareId
func (s *NeSoftwareController) Info(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	softwareId := c.Param("softwareId")
	if softwareId == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	neSoftware := s.neSoftwareService.SelectById(softwareId)
	if neSoftware.ID != softwareId {
		// 没有可访问网元版本数据！
		c.JSON(200, result.ErrMsg(i18n.TKey(language, "neSoftware.noData")))
		return
	}

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

// 网元软件包信息新增
//
// POST /
func (s *NeSoftwareController) Add(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body model.NeSoftware
	err := c.ShouldBindBodyWith(&body, binding.JSON)
	if err != nil || body.ID != "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 检查属性值唯一
	uniqueSoftware := s.neSoftwareService.CheckUniqueTypeAndFileNameAndVersion(body.NeType, body.FileName, body.Version, "")
	if !uniqueSoftware {
		// 网元软件包操作【%s】失败，网元类型与文件名版本已存在
		msg := i18n.TTemplate(language, "neSoftware.errKeyExists", map[string]any{"name": body.FileName})
		c.JSON(200, result.ErrMsg(msg))
		return
	}

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

// 网元软件包信息修改
//
// PUT /
func (s *NeSoftwareController) Edit(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body model.NeSoftware
	err := c.ShouldBindBodyWith(&body, binding.JSON)
	if err != nil || body.ID == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 检查属性值唯一
	uniqueSoftware := s.neSoftwareService.CheckUniqueTypeAndFileNameAndVersion(body.NeType, body.FileName, body.Version, body.ID)
	if !uniqueSoftware {
		// 网元软件包操作【%s】失败，网元类型与文件名版本已存在
		msg := i18n.TTemplate(language, "neSoftware.errKeyExists", map[string]any{"name": body.FileName})
		c.JSON(200, result.ErrMsg(msg))
		return
	}

	// 检查是否存在
	neSoftware := s.neSoftwareService.SelectById(body.ID)
	if neSoftware.ID != body.ID {
		// 没有可访问网元版本数据！
		c.JSON(200, result.ErrMsg(i18n.TKey(language, "neSoftware.noData")))
		return
	}

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

// 网元软件包信息删除
//
// DELETE /:softwareIds
func (s *NeSoftwareController) Remove(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	softwareIds := c.Param("softwareIds")
	if softwareIds == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}
	// 处理字符转id数组后去重
	ids := strings.Split(softwareIds, ",")
	uniqueIDs := parse.RemoveDuplicates(ids)
	if len(uniqueIDs) <= 0 {
		c.JSON(200, result.Err(nil))
		return
	}
	rows, err := s.neSoftwareService.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))
}

// 网元软件包安装
//
// POST /install
func (s *NeSoftwareController) Install(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body model.NeSoftware
	err := c.ShouldBindBodyWith(&body, binding.JSON)
	if err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 检查是否存在
	neSoftwares := s.neSoftwareService.SelectList(body)
	if len(neSoftwares) == 0 {
		// 没有可访问网元版本数据！
		c.JSON(200, result.ErrMsg(i18n.TKey(language, "neSoftware.noData")))
		return
	}
	neSoftware := neSoftwares[0]

	// 进行安装
	output, err := s.neSoftwareService.Install(neSoftware)
	if err != nil {
		c.JSON(200, result.OkData(output))
		return
	}
	c.JSON(200, result.Err(nil))
}
