package router

import (
	"ARKItems/configs"
	"ARKItems/entity"
	"fmt"
	"github.com/gin-gonic/gin"
)

func ArkItemRouter(api *gin.RouterGroup) {
	api.GET("/arkItems", GetArkItems)
	api.GET("/arkDinos", GetArkDinos)
	api.POST("/arkItems", PostArkItemList)
	api.POST("/arkDinos", PostArkDinoList)

	api.PUT("/arkItem", UpdateArkItem)
	api.PUT("/arkDino", UpdateArkDino)
}
func GetArkItems(c *gin.Context) {
	var items []entity.ArkItemInfo
	count, err := configs.Engine.Table("ark_item_info").FindAndCount(&items)
	if err != nil {
		c.JSON(200, CreateResultError(500, err.Error()))
		return
	}
	c.JSON(200, gin.H{
		"code":    200,
		"message": "success",
		"data": gin.H{
			"list":  items,
			"total": count,
		},
	})
}

func GetArkDinos(c *gin.Context) {
	var items []entity.ArkDinoInfo
	count, err := configs.Engine.Table("ark_dino_info").FindAndCount(&items)
	if err != nil {
		c.JSON(200, CreateResultError(500, err.Error()))
		return
	}

	c.JSON(200, gin.H{
		"code":    200,
		"message": "success",
		"data": gin.H{
			"list":  items,
			"total": count,
		},
	})
}

type ReqArkItem struct {
	PageNum  int    `json:"pageNum"`
	PageSize int    `json:"pageSize"`
	Name     string `json:"name"`
	Category string `json:"category"`
	Label    string `json:"label"`
	AllLike  string `json:"allLike"`
}

func PostArkItemList(c *gin.Context) {
	var item ReqArkItem
	err := c.BindJSON(&item)
	if err != nil {
		c.JSON(200, CreateResultError(500, err.Error()))
		return
	}
	var items []entity.ArkItemInfo
	session := configs.Engine.Table("ark_item_info")
	if item.Name != "" {
		session.Where("name like ?", "%"+item.Name+"%")
	}
	if item.Category != "" {
		session.Where("category like ?", "%"+item.Category+"%")
	}
	if item.Label != "" {
		session.Where("label like ?", "%"+item.Label+"%")
	}
	if item.AllLike != "" {
		session.Where("name like ? or category like ? or label like ?", "%"+item.AllLike+"%", "%"+item.AllLike+"%", "%"+item.AllLike+"%")
	}
	count, err := session.Limit(item.PageSize, (item.PageNum-1)*item.PageSize).FindAndCount(&items)
	if err != nil {
		c.JSON(200, CreateResultError(500, err.Error()))
		return
	}
	c.JSON(200, gin.H{
		"code":    200,
		"message": "success",
		"data": gin.H{
			"list":     items,
			"total":    count,
			"pageNum":  item.PageNum,
			"pageSize": item.PageSize,
		},
	})
}

func PostArkDinoList(c *gin.Context) {
	var item ReqArkItem
	err := c.BindJSON(&item)
	if err != nil {
		c.JSON(200, CreateResultError(500, err.Error()))
		return
	}
	var items []entity.ArkDinoInfo
	session := configs.Engine.Table("ark_dino_info")
	if item.Name != "" {
		session.Where("name like ?", "%"+item.Name+"%")
	}
	if item.Category != "" {
		session.Where("category like ?", "%"+item.Category+"%")
	}
	if item.Label != "" {
		session.Where("label like ?", "%"+item.Label+"%")
	}
	if item.AllLike != "" {
		session.Where("name like ? or category like ? or label like ?", "%"+item.AllLike+"%", "%"+item.AllLike+"%", "%"+item.AllLike+"%")
	}
	count, err := session.Limit(item.PageSize, (item.PageNum-1)*item.PageSize).FindAndCount(&items)
	if err != nil {
		c.JSON(200, CreateResultError(500, err.Error()))
		return
	}
	c.JSON(200, gin.H{
		"code":    200,
		"message": "success",
		"data": gin.H{
			"list":     items,
			"total":    count,
			"pageNum":  item.PageNum,
			"pageSize": item.PageSize,
		},
	})

}

func UpdateArkItem(c *gin.Context) {
	info := entity.ArkItemInfo{}
	err := c.BindJSON(&info)
	if err != nil {
		fmt.Println(err)
		c.JSON(200, CreateResultError(500, err.Error()))
	}
	if info.Id == "" {
		_, err = configs.Engine.Insert(&info)
	} else {
		_, err = configs.Engine.Where("id = ?", info.Id).Update(&info)
	}
}
func UpdateArkDino(c *gin.Context) {
	info := entity.ArkDinoInfo{}
	err := c.BindJSON(&info)
	if err != nil {
		fmt.Println(err)
		c.JSON(200, CreateResultError(500, err.Error()))
	}
	if info.Id == "" {
		_, err = configs.Engine.Insert(&info)
	} else {
		_, err = configs.Engine.Where("id = ?", info.Id).Update(&info)
	}
}
