package controller

import (
	"be.ems/src/framework/i18n"
	"be.ems/src/framework/utils/ctx"
	"be.ems/src/framework/vo/result"
	traceService "be.ems/src/modules/trace/service"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

// 实例化控制层 TcpdumpController 结构体
var NewTcpdump = &TcpdumpController{
	TcpdumpService: traceService.NewTcpdumpImpl,
}

// 信令抓包请求
//
// PATH /tcpdump
type TcpdumpController struct {
	// 信令抓包服务
	TcpdumpService traceService.ITcpdump
}

// 网元抓包PACP 开始
//
// POST /start
func (s *TcpdumpController) DumpStart(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body struct {
		NeType string `json:"neType" binding:"required"` // 网元类型
		NeId   string `json:"neId" binding:"required"`   // 网元ID
		Cmd    string `json:"cmd" binding:"required"`    // 命令 "-n -s 0 -v -w"
	}
	err := c.ShouldBindBodyWith(&body, binding.JSON)
	if err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	fileName, err := s.TcpdumpService.DumpStart(body.NeType, body.NeId, body.Cmd)
	if err != nil {
		msg := err.Error()
		if msg == "noData" {
			// 找不到 %s %s 对应网元信息
			msg = i18n.TTemplate(language, "trace.tcpdump.noData", map[string]any{"type": body.NeType, "id": body.NeId})
		}
		c.JSON(200, result.ErrMsg(msg))
		return
	}
	c.JSON(200, result.OkData(map[string]any{
		"msg": "tcpdump started",
		"out": fileName,
		"log": "",
	}))
}

// 网元抓包PACP 结束
//
// POST /stop
func (s *TcpdumpController) DumpStop(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body struct {
		NeType   string `json:"neType" binding:"required"` // 网元类型
		NeId     string `json:"neId" binding:"required"`   // 网元ID
		FileName string `json:"fileName"`                  // 文件名 查看日志信息
	}
	err := c.ShouldBindBodyWith(&body, binding.JSON)
	if err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	logMsg, err := s.TcpdumpService.DumpStop(body.NeType, body.NeId, body.FileName)
	if err != nil {
		msg := err.Error()
		if msg == "noData" {
			// 找不到 %s %s 对应网元信息
			msg := i18n.TTemplate(language, "trace.tcpdump.noData", map[string]any{"type": body.NeType, "id": body.NeId})
			c.JSON(200, result.ErrMsg(msg))
			return
		}
		c.JSON(200, result.ErrMsg(msg))
		return
	}
	c.JSON(200, result.OkData(map[string]any{
		"msg": "tcpdump stopped",
		"out": body.FileName,
		"log": logMsg,
	}))
}

// UPF标准版内部抓包
//
// POST /traceUPF
func (s *TcpdumpController) TraceUPF(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body struct {
		NeType string `json:"neType" binding:"required"` // 网元类型
		NeId   string `json:"neId" binding:"required"`   // 网元ID
		Cmd    string `json:"cmd" binding:"required"`    // 命令
	}
	err := c.ShouldBindBodyWith(&body, binding.JSON)
	if err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	fileName, logMsg, err := s.TcpdumpService.DumpUPF(body.NeType, body.NeId, body.Cmd)
	if err != nil {
		msg := err.Error()
		if msg == "noData" {
			// 找不到 %s %s 对应网元信息
			msg = i18n.TTemplate(language, "trace.tcpdump.noData", map[string]any{"type": body.NeType, "id": body.NeId})
		}
		c.JSON(200, result.ErrMsg(msg))
		return
	}
	c.JSON(200, result.OkData(map[string]any{
		"msg": "trace UPF dump pacp",
		"out": fileName,
		"log": logMsg,
	}))
}
