package router

import (
	"ARKItems/entity"
	"ARKItems/entity/ark"
	"ARKItems/util"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"net/http"
)

func ARKConfigRouter(api *gin.RouterGroup) {
	api.GET("/messages", GetMessages)

	api.GET("/timedPointsReward", GetTimedPointsReward)

	api.POST("/timedPointsReward/group", SaveGroup)
	api.PUT("/timedPointsReward/group", UpdateGroup)
	api.DELETE("/timedPointsReward/group", DeleteGroup)

	api.GET("/general", GetGeneral)
	api.PUT("/general", UpdateGeneral)

	api.GET("/kit", GetKits)
	api.POST("/kit", SaveKit)
	api.PUT("/kit", UpdateKit)
	api.DELETE("/kit", DeleteKit)

	api.GET("/shopItem", GetShopItem)
	api.POST("/shopItem", SaveShopItem)
	api.PUT("/shopItem", UpdateShopItem)
	api.DELETE("/shopItem", DeleteShopItem)

	api.GET("/sellItem", GetSellItem)
	api.POST("/sellItem", SaveSellItem)
	api.PUT("/sellItem", UpdateSellItem)
	api.DELETE("/sellItem", DeleteSellItem)
}

func GetMessages(c *gin.Context) {
	c.JSON(200, CreateResultData(ARKConfig.Messages))
}
func GetTimedPointsReward(c *gin.Context) {
	c.JSON(200, CreateResultData(ARKConfig.General.TimedPointsReward))
}
func GetGeneral(c *gin.Context) {
	marshal, err := json.Marshal(ARKConfig.General)
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	m := make(map[string]interface{})
	json.Unmarshal(marshal, &m)
	delete(m, "Groups")
	c.JSON(200, CreateResultData(m))
}

func UpdateGeneral(c *gin.Context) {
	param := util.GetJsonAnyParam(c)
	g, err := param("group")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	var General = ark.General{}
	err = util.DataToObject(g, &General)
	if ARKConfig.General.UseOriginalTradeCommandWithUI != General.UseOriginalTradeCommandWithUI {
		ARKConfig.General.UseOriginalTradeCommandWithUI = General.UseOriginalTradeCommandWithUI
	}
	if ARKConfig.General.CryoLimitedTime != General.CryoLimitedTime {
		ARKConfig.General.CryoLimitedTime = General.CryoLimitedTime
	}
	if ARKConfig.General.GiveDinosInCryopods != General.GiveDinosInCryopods {
		ARKConfig.General.GiveDinosInCryopods = General.GiveDinosInCryopods
	}
	if ARKConfig.General.UseSoulTraps != General.UseSoulTraps {
		ARKConfig.General.UseSoulTraps = General.UseSoulTraps
	}
	if ARKConfig.General.ItemsPerPage != General.ItemsPerPage {
		ARKConfig.General.ItemsPerPage = General.ItemsPerPage
	}
	if ARKConfig.General.ShopDisplayTime != General.ShopDisplayTime {
		ARKConfig.General.ShopDisplayTime = General.ShopDisplayTime
	}
	if ARKConfig.General.ShopTextSize != General.ShopTextSize {
		ARKConfig.General.ShopTextSize = General.ShopTextSize
	}
	if ARKConfig.General.DefaultKit != General.DefaultKit {
		ARKConfig.General.DefaultKit = General.DefaultKit
	}
	if ARKConfig.General.ShopDisplayTime != General.ShopDisplayTime {
		ARKConfig.General.ShopDisplayTime = General.ShopDisplayTime
	}
	if ARKConfig.General.DbPathOverride != General.DbPathOverride {
		ARKConfig.General.DbPathOverride = General.DbPathOverride
	}
	c.JSON(200, CreateResult())
}

//-----------------组修改----------------------

// SaveGroup 添加VIP
func SaveGroup(c *gin.Context) {
	param := util.GetJsonAnyParam(c)
	g, err := param("group")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	var Group = entity.Group{}
	err = util.DataToObject(g, &Group)
	fmt.Println(g, Group)
	ARKConfig.General.TimedPointsReward.Groups[Group.Name] = ark.Group{Amount: Group.Amount}
	c.JSON(200, CreateResult())
}

// UpdateGroup 修改VIP
func UpdateGroup(c *gin.Context) {
	param := util.GetJsonAnyParam(c)
	g, err := param("group")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	var Group = entity.Group{}
	err = util.DataToObject(g, &Group)
	if Group.Name == "" {
		c.JSON(200, CreateResultError(400, "VIP名称不能为空"))
		return
	}
	_, ok := ARKConfig.General.TimedPointsReward.Groups[Group.Name]
	if ok {
		ARKConfig.General.TimedPointsReward.Groups[Group.Name] = ark.Group{Amount: Group.Amount}
		c.JSON(200, CreateResult())
		return
	}
	c.JSON(200, CreateResultError(400, "VIP组名不存在"))
}

// DeleteGroup 修改VIP
func DeleteGroup(c *gin.Context) {
	name := c.Query("groupName")

	if cast.ToString(name) == "" {
		c.JSON(200, CreateResultError(400, "VIP名称不能为空"))
		return
	}
	_, ok := ARKConfig.General.TimedPointsReward.Groups[cast.ToString(name)]
	if ok {
		//删除一个元素
		delete(ARKConfig.General.TimedPointsReward.Groups, cast.ToString(name))
		c.JSON(200, CreateResult())
		return
	}
	c.JSON(200, CreateResultError(400, "VIP组名不存在"))
}

//==================礼包==========================

func GetKits(c *gin.Context) {
	c.JSON(200, CreateResultData(ARKConfig.Kits))
}

func SaveKit(c *gin.Context) {
	param := util.GetJsonAnyParam(c)
	obj, err := param("kit")
	Name, err := param("kitName")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	var Kit = ark.Kit{}
	err = util.DataToObject(obj, &Kit)
	if Kit.Description == "" || Kit.Price == 0 || Kit.DefaultAmount == 0 {
		c.JSON(200, CreateResultError(400, "礼包描述不能为空，价格，数量不能为0"))
		return
	}
	ARKConfig.Kits[cast.ToString(Name)] = Kit
}
func UpdateKit(c *gin.Context) {
	param := util.GetJsonAnyParam(c)
	obj, err := param("kit")
	Name, err := param("kitName")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	var Kit = ark.Kit{}
	err = util.DataToObject(obj, &Kit)
	if Name == "" {
		c.JSON(200, CreateResultError(400, "礼包名不能为空"))
	}
	_, ok := ARKConfig.Kits[cast.ToString(Name)]
	if ok {
		if Kit.Price == 0 || Kit.DefaultAmount == 0 {
			c.JSON(200, CreateResultError(400, "价格，数量不能为0"))
			return
		}
		ARKConfig.Kits[cast.ToString(Name)] = Kit
		c.JSON(200, CreateResult())
		return
	}
	c.JSON(200, CreateResultError(400, "礼包不存在"))
}
func DeleteKit(c *gin.Context) {
	name := c.Query("kitName")

	if cast.ToString(name) == "" {
		c.JSON(200, CreateResultError(400, "礼包名称不能为空"))
		return
	}
	_, ok := ARKConfig.Kits[cast.ToString(name)]
	if ok {
		//删除一个元素
		delete(ARKConfig.Kits, cast.ToString(name))
		c.JSON(200, CreateResult())
		return
	}
	c.JSON(200, CreateResultError(400, "礼包不存在"))
}

//====================商店==========================

func GetShopItem(c *gin.Context) {
	value := c.Query("type")
	if value == "" {
		c.JSON(200, CreateResultData(ARKConfig.ShopItems))
		return
	}
	switch cast.ToString(value) {
	case "item":
		c.JSON(200, CreateResultData(ARKShopItems))
		break
	case "dino":
		c.JSON(200, CreateResultData(ARKShopDines))
		break
	case "beacon":
		c.JSON(200, CreateResultData(ARKShopBeacons))
		break
	case "experience":
		c.JSON(200, CreateResultData(ARKShopExperiences))
		break
	case "unlockengram":
		c.JSON(200, CreateResultData(ARKShopUnlockengram))
		break
	case "command":
		c.JSON(200, CreateResultData(ARKShopCommands))
		break
	default:
		c.JSON(200, CreateResultError(400, "商店类型错误"))
	}

}

func SaveShopItem(c *gin.Context) {
	param := util.GetJsonAnyParam(c)
	obj, err := param("shopItem")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	shopType, err := param("shopType")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	Name, err := param("shopItemName")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	toMap := util.DataToMap(obj)
	if cast.ToString(Name) == "" || toMap["Price"] == 0 || toMap["Type"] == "" {
		c.JSON(200, CreateResultError(400, "商店描述，价格，类型不能为空，为0"))
		return
	}
	_, ok := ARKConfig.ShopItems[cast.ToString(Name)]
	if ok {
		c.JSON(200, CreateResultError(400, "商品名已存在"))
		return
	}

	switch cast.ToString(shopType) {
	case "item":
		var item = ark.ShopItem{}
		err = util.DataToObject(obj, &item)
		ARKConfig.ShopItems[cast.ToString(Name)] = item
		ARKShopItems[cast.ToString(Name)] = item
		break
	case "dino":
		var dino = ark.ShopDino{}
		err = util.DataToObject(obj, &dino)
		ARKConfig.ShopItems[cast.ToString(Name)] = dino
		ARKShopDines[cast.ToString(Name)] = dino
		break
	case "baecon":
		var beacon = ark.ShopBeacon{}
		err = util.DataToObject(obj, &beacon)
		ARKConfig.ShopItems[cast.ToString(Name)] = beacon
		ARKShopBeacons[cast.ToString(Name)] = beacon
		break
	case "experience":
		var experience = ark.ShopExperience{}
		err = util.DataToObject(obj, &experience)
		ARKConfig.ShopItems[cast.ToString(Name)] = experience
		ARKShopExperiences[cast.ToString(Name)] = experience
		break
	case "unlockengram":
		var unlockengram = ark.ShopCommand{}
		err = util.DataToObject(obj, &unlockengram)
		ARKConfig.ShopItems[cast.ToString(Name)] = unlockengram
		ARKShopCommands[cast.ToString(Name)] = unlockengram
		break
	case "command":
		var command = ark.ShopCommand{}
		err = util.DataToObject(obj, &command)
		ARKConfig.ShopItems[cast.ToString(Name)] = command
		ARKShopCommands[cast.ToString(Name)] = command
		break
	default:
		c.JSON(200, CreateResultError(400, "商店类型错误"))
		return
	}
	c.JSON(200, CreateResult())
}

func UpdateShopItem(c *gin.Context) {
	param := util.GetJsonAnyParam(c)
	obj, err := param("shopItem")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	shopType, err := param("shopType")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}
	Name, err := param("shopItemName")
	if err != nil {
		c.JSON(200, CreateResultError(400, "参数错误"))
		return
	}

	if cast.ToString(Name) == "" {
		c.JSON(200, CreateResultError(400, "商店名不能为空"))
		return
	}
	toMap := util.DataToMap(obj)
	if cast.ToString(Name) == "" || toMap["Price"] == 0 || toMap["Type"] == "" {
		c.JSON(200, CreateResultError(400, "商店描述，价格，类型不能为空，为0"))
		return
	}
	_, ok := ARKConfig.ShopItems[cast.ToString(Name)]
	if !ok {
		c.JSON(200, CreateResultError(400, "商品名不存在"))
		return
	}
	if toMap["Price"] == 0 || toMap["Type"] == "" {
		c.JSON(200, CreateResultError(400, "价格，类型不能为空，为0"))
		return
	}
	switch cast.ToString(shopType) {
	case "item":
		var item = ark.ShopItem{}
		err = util.DataToObject(obj, &item)
		ARKConfig.ShopItems[cast.ToString(Name)] = item
		ARKShopItems[cast.ToString(Name)] = item
		break
	case "dino":
		var dino = ark.ShopDino{}
		err = util.DataToObject(obj, &dino)
		ARKConfig.ShopItems[cast.ToString(Name)] = dino
		ARKShopDines[cast.ToString(Name)] = dino
		break
	case "baecon":
		var beacon = ark.ShopBeacon{}
		err = util.DataToObject(obj, &beacon)
		ARKConfig.ShopItems[cast.ToString(Name)] = beacon
		ARKShopBeacons[cast.ToString(Name)] = beacon
		break
	case "experience":
		var experience = ark.ShopExperience{}
		err = util.DataToObject(obj, &experience)
		ARKConfig.ShopItems[cast.ToString(Name)] = experience
		ARKShopExperiences[cast.ToString(Name)] = experience
		break
	case "unlockengram":
		var unlockengram = ark.ShopCommand{}
		err = util.DataToObject(obj, &unlockengram)
		ARKConfig.ShopItems[cast.ToString(Name)] = unlockengram
		ARKShopCommands[cast.ToString(Name)] = unlockengram
		break
	case "command":
		var command = ark.ShopCommand{}
		err = util.DataToObject(obj, &command)
		ARKConfig.ShopItems[cast.ToString(Name)] = command
		ARKShopCommands[cast.ToString(Name)] = command
		break
	default:
		c.JSON(200, CreateResultError(400, "商店类型错误"))
		return
	}
	c.JSON(200, CreateResult())
}

func DeleteShopItem(c *gin.Context) {
	name := c.Query("shopItemName")
	if cast.ToString(name) == "" {
		c.JSON(200, CreateResultError(400, "商店名不能为空"))
		return
	}
	_, ok := ARKConfig.ShopItems[cast.ToString(name)]
	if ok {
		//删除一个元素
		delete(ARKConfig.ShopItems, cast.ToString(name))
		delete(ARKShopItems, cast.ToString(name))
		delete(ARKShopDines, cast.ToString(name))
		delete(ARKShopBeacons, cast.ToString(name))
		delete(ARKShopExperiences, cast.ToString(name))
		delete(ARKShopUnlockengram, cast.ToString(name))
		delete(ARKShopCommands, cast.ToString(name))
		c.JSON(200, CreateResult())
		return
	}
	c.JSON(200, CreateResultError(400, "商店名不存在"))
}

//========================回收==========================

func GetSellItem(c *gin.Context) {
	c.JSON(http.StatusOK, CreateResultData(ARKConfig.SellItems))
}

func SaveSellItem(c *gin.Context) {
	param := util.GetJsonAnyParam(c)
	obj, err := param("sellItem")
	Name, err := param("sellItemName")
	if err != nil {
		c.JSON(http.StatusBadRequest, CreateResultError(400, "参数错误"))
		return
	}
	var sellItem ark.SellItem
	err = util.DataToObject(obj, &sellItem)
	if cast.ToString(Name) == "" || sellItem.Price == 0 || sellItem.Type == "" {
		c.JSON(http.StatusBadRequest, CreateResultError(400, "描述、价格、类型不能为空或为0"))
		return
	}
	_, ok := ARKConfig.SellItems[cast.ToString(Name)]
	if ok {
		c.JSON(http.StatusBadRequest, CreateResultError(400, "商品名已存在"))
		return
	}
	ARKConfig.SellItems[cast.ToString(Name)] = sellItem
	c.JSON(http.StatusOK, CreateResult())
}

func UpdateSellItem(c *gin.Context) {
	param := util.GetJsonAnyParam(c)
	obj, err := param("sellItem")
	Name, err := param("sellItemName")
	if err != nil {
		c.JSON(http.StatusBadRequest, CreateResultError(400, "参数错误"))
		return
	}
	var sellItem ark.SellItem
	err = util.DataToObject(obj, &sellItem)
	if cast.ToString(Name) == "" {
		c.JSON(http.StatusBadRequest, CreateResultError(400, "商品描述不能为空"))
		return
	}
	_, ok := ARKConfig.SellItems[cast.ToString(Name)]
	if ok {
		if sellItem.Price == 0 || sellItem.Type == "" {
			c.JSON(http.StatusBadRequest, CreateResultError(400, "价格、类型不能为空或为0"))
			return
		}
		ARKConfig.SellItems[cast.ToString(Name)] = sellItem
		c.JSON(http.StatusOK, CreateResult())
		return
	}
	c.JSON(http.StatusBadRequest, CreateResultError(400, "商品名不存在"))
}

func DeleteSellItem(c *gin.Context) {
	name := c.Query("sellItemName")
	if cast.ToString(name) == "" {
		c.JSON(http.StatusBadRequest, CreateResultError(400, "商品描述不能为空"))
		return
	}
	_, ok := ARKConfig.SellItems[cast.ToString(name)]
	if ok {
		// 删除一个元素
		delete(ARKConfig.SellItems, cast.ToString(name))
		c.JSON(http.StatusOK, CreateResult())
		return
	}
	c.JSON(http.StatusBadRequest, CreateResultError(400, "商品名不存在"))
}
