package monitor

import (
	"be.ems/src/framework/logger"
	"be.ems/src/framework/middleware"
	"be.ems/src/framework/middleware/collectlogs"
	"be.ems/src/framework/middleware/repeat"
	"be.ems/src/modules/monitor/controller"
	"be.ems/src/modules/monitor/processor"
	"be.ems/src/modules/monitor/service"

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

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

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

	// 服务器资源监控信息
	monitorGroup := router.Group("/monitor")
	{
		monitorGroup.GET("/load",
			middleware.AuthorizeUser(nil),
			controller.NewMonitor.Load,
		)
	}

	// 服务器信息
	router.GET("/monitor/system",
		middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:system:info"}}),
		controller.NewSystem.Info,
	)

	// 在线用户监控
	sysUserOnlineGroup := router.Group("/monitor/user-online")
	{
		sysUserOnlineGroup.GET("/list",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:online:list"}}),
			controller.NewSysUserOnline.List,
		)
		sysUserOnlineGroup.DELETE("/logout/:tokenId",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:online:logout"}}),
			controller.NewSysUserOnline.Logout,
		)
	}

	// 缓存服务信息
	sysCacheGroup := router.Group("/monitor/cache")
	{
		sysCacheGroup.GET("",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:info"}}),
			controller.NewSysCache.Info,
		)
		sysCacheGroup.GET("/names",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:list"}}),
			controller.NewSysCache.Names,
		)
		sysCacheGroup.GET("/keys",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:list"}}),
			controller.NewSysCache.Keys,
		)
		sysCacheGroup.GET("/value",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:query"}}),
			controller.NewSysCache.Value,
		)
		sysCacheGroup.DELETE("/names",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
			controller.NewSysCache.CleanNames,
		)
		sysCacheGroup.DELETE("/keys",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
			controller.NewSysCache.CleanKeys,
		)
		sysCacheGroup.DELETE("/value",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
			controller.NewSysCache.CleanValue,
		)
	}

	// 调度任务日志信息
	sysJobLogGroup := router.Group("/monitor/job/log")
	{
		sysJobLogGroup.GET("/list",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:list"}}),
			controller.NewSysJobLog.List,
		)
		sysJobLogGroup.GET("/:logId",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:query"}}),
			controller.NewSysJobLog.Info,
		)
		sysJobLogGroup.DELETE("/:logId",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJobLog", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewSysJobLog.Remove,
		)
		sysJobLogGroup.DELETE("/clean",
			repeat.RepeatSubmit(5),
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJobLog", collectlogs.BUSINESS_TYPE_CLEAN)),
			controller.NewSysJobLog.Clean,
		)
		sysJobLogGroup.GET("/export",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:export"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJobLog", collectlogs.BUSINESS_TYPE_EXPORT)),
			controller.NewSysJobLog.Export,
		)
	}

	// 调度任务信息
	sysJobGroup := router.Group("/monitor/job")
	{
		sysJobGroup.GET("/list",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:list"}}),
			controller.NewSysJob.List,
		)
		sysJobGroup.GET("/:jobId",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:query"}}),
			controller.NewSysJob.Info,
		)
		sysJobGroup.POST("",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:add"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_INSERT)),
			controller.NewSysJob.Add,
		)
		sysJobGroup.PUT("",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:edit"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewSysJob.Edit,
		)
		sysJobGroup.DELETE("/:jobId",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_DELETE)),
			controller.NewSysJob.Remove,
		)
		sysJobGroup.PUT("/status",
			repeat.RepeatSubmit(5),
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewSysJob.Status,
		)
		sysJobGroup.PUT("/run/:jobId",
			repeat.RepeatSubmit(10),
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_UPDATE)),
			controller.NewSysJob.Run,
		)
		sysJobGroup.PUT("/reset",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_CLEAN)),
			controller.NewSysJob.ResetQueueJob,
		)
		sysJobGroup.GET("/export",
			middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:export"}}),
			collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_EXPORT)),
			controller.NewSysJob.Export,
		)
	}
}

// InitLoad 初始参数
func InitLoad() {
	// 初始化定时任务处理
	processor.InitCronQueue()
	// 启动时，初始化调度任务
	service.NewSysJob.Reset()
}
