package controller

import (
	"fmt"

	"be.ems/src/framework/resp"
	"be.ems/src/modules/chart/service"

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

// 实例化控制层 ChartGraphController 结构体
var NewChartGraph = &ChartGraphController{
	chartGraphService: service.NewChartGraph,
}

// G6关系图
//
// PATH /graph
type ChartGraphController struct {
	// G6关系图数据表服务
	chartGraphService *service.ChartGraph
}

// 获取关系图组名
//
// GET /groups
//
//	@Tags			chart
//	@Accept			json
//	@Produce		json
//	@Success		200	{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Get relationship graph group name
//	@Description	Get relationship graph group name
//	@Router			/chart/graph/groups [get]
func (s *ChartGraphController) GroupNames(c *gin.Context) {
	data := s.chartGraphService.SelectGroup()
	c.JSON(200, resp.OkData(data))
}

// 获取关系图数据
//
// GET /
//
//	@Tags			chart
//	@Accept			json
//	@Produce		json
//	@Param			group	query		string	true	"Group"
//	@Param			type	query		string	false	"Type"	Enums(node, edge, combo)
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Getting Relationship Map Data
//	@Description	Getting Relationship Map Data
//	@Router			/chart/graph [get]
func (s *ChartGraphController) Load(c *gin.Context) {
	var querys struct {
		Group string `form:"group" binding:"required"`
		Type  string `form:"type" binding:"omitempty,oneof=node edge combo"`
	}
	if err := c.ShouldBindQuery(&querys); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	data := s.chartGraphService.LoadData(querys.Group, querys.Type)
	c.JSON(200, resp.OkData(data))
}

// 保存关系图数据
//
// POST /
//
//	@Tags			chart
//	@Accept			json
//	@Produce		json
//	@Param			data	body		object	true	"Request Param"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Saving Relationship Diagram Data
//	@Description	Saving Relationship Diagram Data
//	@Router			/chart/graph [post]
func (s *ChartGraphController) Save(c *gin.Context) {
	var body struct {
		Group string `json:"group" binding:"required"`
		Data  struct {
			Nodes  []map[string]any `json:"nodes" binding:"required"`
			Edges  []map[string]any `json:"edges" binding:"required"`
			Combos []map[string]any `json:"combos" binding:"required"`
		} `json:"data" 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
	}

	data := map[string]any{
		"nodes":  body.Data.Nodes,
		"edges":  body.Data.Edges,
		"combos": body.Data.Combos,
	}
	saveNum := s.chartGraphService.SaveData(body.Group, data)
	if saveNum > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// 删除关系图数据
//
// DELETE /:group
//
//	@Tags			chart
//	@Accept			json
//	@Produce		json
//	@Param			group	path		string	true	"Group"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Deleting Relationship Diagram Data
//	@Description	Deleting Relationship Diagram Data
//	@Router			/chart/graph/{group} [delete]
func (s *ChartGraphController) Delete(c *gin.Context) {
	group := c.Param("group")
	if group == "" {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: group is empty"))
		return
	}

	deleteNum := s.chartGraphService.DeleteGroup(group)
	if deleteNum > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}
