package typeManage

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log-server/global"
	"log-server/model/common/request"
	"log-server/model/common/response"
	"log-server/model/typeManage"
	typeManageRes "log-server/model/typeManage/response"
	"log-server/utils"
)

type GameListApi struct{}

//创造游戏列表
func (g *GameListApi) CreateGameList(c *gin.Context) {
	//获取前端结构体
	var gameList typeManage.GameList
	_ = c.ShouldBindJSON(&gameList)
	if err := utils.Verify(gameList, utils.GameListVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := gameListService.CreateGameList(gameList); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

//单个删除游戏列表
func (g *GameListApi) DeleteGameList(c *gin.Context) {
	var game typeManage.GameList
	_ = c.ShouldBindJSON(&game)
	if err := utils.Verify(game, utils.GameListVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := gameListService.DeleteGameList(game); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//批量删除游戏列表
func (g *GameListApi) DeleteGameListsByIds(c *gin.Context) {
	var ids request.IdsReq
	_ = c.ShouldBindJSON(&ids)
	if err := gameListService.DeleteGameListsByIds(ids); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//更改游戏列表信息
func (g *GameListApi) UpdateGameList(c *gin.Context) {
	var game typeManage.GameList
	_ = c.ShouldBindJSON(&game)
	//verify数据校验
	if err := utils.Verify(game, utils.GameListVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	//调用service
	if err := gameListService.UpdateGameList(game); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage("修改失败", c)
	} else {
		response.OkWithMessage("修改成功", c)
	}

}

//获取单个游戏列表
func (g *GameListApi) GetGameListById(c *gin.Context) {
	var idInfo request.GetById
	_ = c.ShouldBindJSON(&idInfo)
	//前端校验
	if err := utils.Verify(idInfo, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	gameList, err := gameListService.GetGameListById(idInfo.ID)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(typeManageRes.GameListResponse{GameList: gameList}, "获取成功", c)
	}

}

//获取所有游戏列表
func (g *GameListApi) GetGameList(c *gin.Context) {
	//传入查询结构体
	var pageInfo typeManage.SearchGameListParams
	_ = c.ShouldBindJSON(&pageInfo)
	//进行数据校验
	if err := utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	//访问service
	if list, option, total, err := gameListService.GetGameList(pageInfo, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		//响应结果
		response.OkWithDetailed(typeManageRes.GameListPageResult{
			List:       list,
			OptionList: option,
			Total:      total,
			Page:       pageInfo.Page,
			PageSize:   pageInfo.PageSize,
		}, "获取成功", c)
	}

}
