package controller

import (
	"fmt"

	"be.ems/src/framework/i18n"
	"be.ems/src/framework/reqctx"
	"be.ems/src/framework/resp"
	traceService "be.ems/src/modules/trace/service"

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

// 实例化控制层 TCPdumpController 结构体
var NewTCPdump = &TCPdumpController{
	tcpdumpService: traceService.NewTCPdump,
}

// 信令抓包
//
// PATH /tcpdump
type TCPdumpController struct {
	tcpdumpService *traceService.TCPdump // 信令抓包服务
}

// 网元抓包PACP 开始
//
// POST /start
//
//	@Tags			trace/tcpdump
//	@Accept			json
//	@Produce		json
//	@Param			data	body		object	true	"Request Param"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Network Element Capture Packet PACP Start
//	@Description	Network Element Capture Packet PACP Start
//	@Router			/trace/tcpdump/start [post]
func (s *TCPdumpController) DumpStart(c *gin.Context) {
	language := reqctx.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"
	}
	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
	}

	taskCode, err := s.tcpdumpService.DumpStart(body.NeType, body.NeId, body.Cmd)
	if err != nil {
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
		return
	}
	c.JSON(200, resp.OkData(taskCode))
}

// 网元抓包PACP 结束
//
// POST /stop
//
//	@Tags			trace/tcpdump
//	@Accept			json
//	@Produce		json
//	@Param			data	body		object	true	"Request Param"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Network Element Capture Packet PACP Stop
//	@Description	Network Element Capture Packet PACP Stop
//	@Router			/trace/tcpdump/stop [post]
func (s *TCPdumpController) DumpStop(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body struct {
		NeType   string `json:"neType" binding:"required"`   // 网元类型
		NeId     string `json:"neId" binding:"required"`     // 网元ID
		TaskCode string `json:"taskCode" binding:"required"` // 任务码，停止任务并查看日志信息
	}
	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
	}

	logFiles, err := s.tcpdumpService.DumpStop(body.NeType, body.NeId, body.TaskCode)
	if err != nil {
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
		return
	}
	c.JSON(200, resp.OkData(logFiles))
}

// UPF标准版内部抓包
//
// POST /upf
//
//	@Tags			trace/tcpdump
//	@Accept			json
//	@Produce		json
//	@Param			data	body		object	true	"Request Param"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		UPF Standard Edition Internal Grab Bag
//	@Description	UPF Standard Edition Internal Grab Bag
//	@Router			/trace/tcpdump/upf [post]
func (s *TCPdumpController) UPFTrace(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body struct {
		NeType string `json:"neType" binding:"required"` // 网元类型
		NeId   string `json:"neId" binding:"required"`   // 网元ID
		Cmd    string `json:"cmd" binding:"required"`    // 命令
	}
	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
	}

	msg, err := s.tcpdumpService.UPFTrace(body.NeType, body.NeId, body.Cmd)
	if err != nil {
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
		return
	}
	c.JSON(200, resp.OkData(msg))
}
