package router

import (
	"ARKItems/configs"
	"ARKItems/entity/ark"
	"ARKItems/util"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
)

var ARKConfig ark.Config

func LoadRouter(router *gin.Engine) {
	router.POST("/login", Login)
	group := router.Group("/ark")
	ArkItemRouter(group)
}

func Login(c *gin.Context) {
	param := util.GetJsonAnyParam(c)
	username, _ := param("username")
	password, _ := param("password")
	if username == configs.Manager.Username && password == configs.Manager.Password {
		token, err := util.GenerateToken(cast.ToString(username))
		if err != nil {
			c.JSON(200, CreateResultError(500, "token生成失败"))
			return
		}
		c.JSON(200, CreateResultData(gin.H{
			"token": token,
		}))
		return
	}
	c.JSON(200, CreateResultError(500, "用户名或密码错误"))
}

//===============================================================

func CreateResult() gin.H {
	return gin.H{
		"code": 200,
		"msg":  "success",
	}
}
func CreateResultData(Data any) gin.H {
	return gin.H{
		"code": 200,
		"msg":  "success",
		"data": Data,
	}
}

func CreateResultError(errCode int, errMsg string) gin.H {
	return gin.H{
		"code": errCode,
		"msg":  errMsg,
	}
}
