package trace

import (
	"be.ems/src/framework/logger"
	"be.ems/src/framework/middleware"
	"be.ems/src/framework/middleware/collectlogs"
	"be.ems/src/modules/trace/controller"
	"be.ems/src/modules/trace/service"

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

// 模块路由注册
func Setup(router *gin.Engine) {
	logger.Infof("开始加载 ====> trace 模块路由")

	// 启动时需要的初始参数
	InitLoad()

	// 信令抓包
	tcpdumpGroup := router.Group("/trace/tcpdump")
	{
		tcpdumpGroup.POST("/start",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewTCPdump.DumpStart,
		)
		tcpdumpGroup.POST("/stop",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewTCPdump.DumpStop,
		)
		tcpdumpGroup.POST("/upf",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewTCPdump.UPFTrace,
		)
	}

	// 信令跟踪
	packetGroup := router.Group("/trace/packet")
	{
		packetGroup.GET("/devices",
			middleware.AuthorizeUser(nil),
			controller.NewPacket.Devices,
		)
		packetGroup.POST("/start",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.packet", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewPacket.Start,
		)
		packetGroup.POST("/stop",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.packet", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewPacket.Stop,
		)
		packetGroup.PUT("/filter",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.packet", collectlogs.BUSINESS_TYPE_OTHER)),
			controller.NewPacket.Filter,
		)
		packetGroup.PUT("/keep-alive",
			middleware.AuthorizeUser(nil),
			controller.NewPacket.KeepAlive,
		)
		packetGroup.GET("/filePull",
			middleware.AuthorizeUser(nil),
			controller.NewPacket.FilePull,
		)
	}

	// 跟踪任务 网元HLR (免登录)
	taskHLRGroup := router.Group("/trace/task/hlr")
	{
		taskHLRGroup.GET("/list",
			controller.NewTraceTaskHlr.List,
		)
		taskHLRGroup.DELETE("/:id",
			controller.NewTraceTaskHlr.Remove,
		)
		taskHLRGroup.POST("/start",
			controller.NewTraceTaskHlr.Start,
		)
		taskHLRGroup.POST("/stop",
			controller.NewTraceTaskHlr.Stop,
		)
		taskHLRGroup.POST("/file",
			controller.NewTraceTaskHlr.File,
		)
		taskHLRGroup.GET("/filePull",
			controller.NewTraceTaskHlr.FilePull,
		)
	}

	// 跟踪任务
	taskGroup := router.Group("/trace/task")
	{
		taskGroup.GET("/list",
			middleware.AuthorizeUser(nil),
			controller.NewTraceTask.List,
		)
		taskGroup.GET("/:id",
			middleware.AuthorizeUser(nil),
			controller.NewTraceTask.Info,
		)
		taskGroup.POST("",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.task", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewTraceTask.Add,
		)
		taskGroup.DELETE("/:id",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.task", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewTraceTask.Remove,
		)
		taskGroup.GET("/filePull",
			middleware.AuthorizeUser(nil),
			controller.NewTraceTask.FilePull,
		)
	}

	// 跟踪数据
	taskDataGroup := router.Group("/trace/data")
	{
		taskDataGroup.GET("/list",
			middleware.AuthorizeUser(nil),
			controller.NewTraceData.List,
		)
		taskDataGroup.GET("/:id",
			middleware.AuthorizeUser(nil),
			controller.NewTraceData.Info,
		)
		taskDataGroup.DELETE("/:id",
			middleware.AuthorizeUser(nil),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.taskData", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewTraceData.Remove,
		)
	}
}

// InitLoad 初始参数
func InitLoad() {
	// 创建跟踪任务信令数据通道UDP
	if err := service.NewTraceTask.CreateUDP(false); err != nil {
		logger.Errorf("socket udp init fail: %s", err.Error())
	}
}
