package controller

import (
	"strings"

	"be.ems/src/framework/i18n"
	"be.ems/src/framework/utils/ctx"
	"be.ems/src/framework/utils/parse"
	"be.ems/src/framework/vo/result"
	"be.ems/src/modules/network_element/model"
	neService "be.ems/src/modules/network_element/service"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

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

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

// 网元软件包列表
//
// GET /list
//
//	@Tags			network_element/software
//	@Accept			json
//	@Produce		json
//	@Param			neType	query		string	true	"NE Type"	Enums(IMS,AMF,AUSF,UDM,SMF,PCF,NSSF,NRF,UPF,MME,CBC,OMC,SGWC)
//	@Param			neId	query		string	true	"NE ID"		default(001)
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		List of Network Element Software Packages
//	@Description	List of Network Element Software Packages
//	@Router			/ne/software/list [get]
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.Path == "" || body.ID != "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 找到已存在的删除后重新添加
	neSoftwares := s.neSoftwareService.SelectList(model.NeSoftware{
		NeType:  body.NeType,
		Name:    body.Name,
		Version: body.Version,
	})
	if len(neSoftwares) > 0 {
		neSoftware := neSoftwares[0]
		s.neSoftwareService.DeleteByIds([]string{neSoftware.ID})
	}

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

	body.CreateBy = ctx.LoginUserToUserName(c)
	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.Path == "" || body.ID == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 检查属性值唯一
	uniqueSoftware := s.neSoftwareService.CheckUniqueTypeAndNameAndVersion(body.NeType, body.Name, body.Version, body.ID)
	if !uniqueSoftware {
		// 网元软件包操作【%s】失败，网元类型与文件名版本已存在
		msg := i18n.TTemplate(language, "neSoftware.errKeyExists", map[string]any{"name": body.Name})
		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
	}

	body.UpdateBy = ctx.LoginUserToUserName(c)
	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 /newNeVersion
//
//	@Tags			network_element/software
//	@Accept			json
//	@Produce		json
//	@Param			data	body		object	true	"Request Param"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Net Element package set to Net Element new version
//	@Description	Net Element package set to Net Element new version
//	@Router			/ne/software/newNeVersion [post]
func (s *NeSoftwareController) NewNeVersion(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(model.NeSoftware{
		NeType:  body.NeType,
		Name:    body.Name,
		Version: body.Version,
	})
	if len(neSoftwares) > 0 {
		neSoftware := neSoftwares[0]
		s.neSoftwareService.UpdateVersions(neSoftware, model.NeVersion{
			NeType:   neSoftware.NeType,
			UpdateBy: ctx.LoginUserToUserName(c),
		})
		c.JSON(200, result.Ok(nil))
		return
	}
	c.JSON(200, result.Err(nil))
}
