package controller

import (
	"be.ems/lib/core/utils/date"
	"be.ems/src/framework/i18n"
	"be.ems/src/framework/utils/ctx"
	"be.ems/src/framework/vo/result"
	"be.ems/src/modules/network_data/model"
	neDataService "be.ems/src/modules/network_data/service"
	neService "be.ems/src/modules/network_element/service"
	"github.com/gin-gonic/gin"
)

// 实例化控制层 PerfKPIController 结构体
var NewPerfKPIController = &PerfKPIController{
	neInfoService:  neService.NewNeInfoImpl,
	perfKPIService: neDataService.NewPerfKPIImpl,
}

// 性能统计
//
// PATH /kpi
type PerfKPIController struct {
	// 网元信息服务
	neInfoService neService.INeInfo
	// 统计信息服务
	perfKPIService neDataService.IPerfKPI
}

// 获取统计数据
//
// GET /data
func (s *PerfKPIController) GoldKPI(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var querys model.GoldKPIQuery
	if err := c.ShouldBindQuery(&querys); err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 时间格式校验
	startTime := date.ParseStrToDate(querys.StartTime, date.YYYY_MM_DD_HH_MM_SS)
	if startTime.IsZero() {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}
	endTime := date.ParseStrToDate(querys.EndTime, date.YYYY_MM_DD_HH_MM_SS)
	if endTime.IsZero() {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}
	if querys.Interval < 5 || querys.Interval > 3600 {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 查询网元获取IP
	neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
	if neInfo.NeId != querys.NeID || neInfo.IP == "" {
		c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
		return
	}
	querys.RmUID = neInfo.RmUID

	// 查询数据
	kpiData := s.perfKPIService.SelectGoldKPI(querys)
	c.JSON(200, result.OkData(kpiData))
}

// 获取统计标题
//
// GET /title
func (s *PerfKPIController) Title(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	neType := c.Query("neType")
	if neType == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	kpiTitles := s.perfKPIService.SelectGoldKPITitle(neType)

	c.JSON(200, result.OkData(kpiTitles))
}
