package chargeCfg

import (
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/gamedata"
	"sort"
	"strings"
)

const (
	CFG_CHARGE         = "Charge"
	CFG_DIAMOND_SHOP   = "DiamondShop"   // 钻石商店
	CFG_ENERGY_SHOP    = "EnergyShop"    // 体力商店
	CFG_SEPCIAL_SHOP   = "SpecialShop"   // 特惠商店
	CFG_FREE_SHOP      = "FreeShop"      // 免费商店
	CFG_CHARGE_CONST   = "ChargeConst"   // 充值常量配置
	CFG_CHARGE_GIFT    = "ChargeGift"    // 充值礼包
	CFG_CHARGE_WISH    = "ChargeWish"    // 许愿配置
	CFG_CHARGE_PETCOIN = "ChargePetCoin" // 宠物币充值配置
	CFG_CHARGE_AD      = "ChargeAD"      // 广告充值配置
)

func init() {
	gamedata.InitCfg(CFG_CHARGE)
	gamedata.InitCfg(CFG_DIAMOND_SHOP)
	gamedata.InitCfg(CFG_ENERGY_SHOP)
	gamedata.InitCfg(CFG_SEPCIAL_SHOP)
	gamedata.InitCfg(CFG_FREE_SHOP)
	gamedata.InitCfg(CFG_CHARGE_CONST)
	gamedata.InitCfg(CFG_CHARGE_GIFT)
	gamedata.InitCfg(CFG_CHARGE_WISH)
	gamedata.InitCfg(CFG_CHARGE_PETCOIN)
	gamedata.InitCfg(CFG_CHARGE_AD)
}

func GetADReward(ChargeId int) ([]*item.Item, int) {
	data, err := gamedata.GetData(CFG_CHARGE_AD)
	if err != nil {
		return nil, 0
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "ChargeId") == ChargeId {
			return item.ParseItem(gamedata.GetValue(v, "Items")), gamedata.GetIntValue(v, "PetWork")
		}
	}
	return nil, 0
}

func GetWeeklyDiscountDay() int {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "weekly_discount_day")
	if err != nil {
		return -1
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetMoneyCharge(ChargeId int) float64 {
	data, err := gamedata.GetDataByIntKey(CFG_CHARGE, ChargeId)
	if err != nil {
		return 0
	}
	return gamedata.GetFloatValue(data, "Money")
}

func GetDiamondShopReward(ChargeId int) []*item.Item {
	data, err := gamedata.GetData(CFG_DIAMOND_SHOP)
	if err != nil {
		return nil
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "ChargeId") == ChargeId {
			return []*item.Item{item.NewItem(item.ITEM_DIAMOND_ID, gamedata.GetIntValue(v, "Diamond"))}
		}
	}
	return nil
}

func GetEnergyShopId(ChargeId int) int {
	data, err := gamedata.GetData(CFG_ENERGY_SHOP)
	if err != nil {
		return 0
	}
	for k, v := range data {
		if gamedata.GetIntValue(v, "ChargeId") == ChargeId {
			return GoUtil.Int(k)
		}
	}
	return 0
}

func GetWeeklyInfo(Pos int) (int, int) {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "weekly_chess_shop")
	if err != nil {
		return 0, 0
	}
	r := data["Value"].([]interface{})
	for _, v := range r {
		v1 := v.(map[string]interface{})
		Id := GoUtil.Int(v1["Id"])
		if Id == Pos {
			return GoUtil.Int(v1["Discount"]), GoUtil.Int(v1["Limit"])
		}
	}
	return 0, 0
}

func GetWeeklyInfoAll() map[int]gamedata.WeeklyDiscountInfo {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "weekly_chess_shop")
	if err != nil {
		return nil
	}
	r := data["Value"].([]interface{})
	res := make(map[int]gamedata.WeeklyDiscountInfo)
	for _, v := range r {
		v1 := v.(map[string]interface{})
		Id := GoUtil.Int(v1["Id"])
		res[Id] = gamedata.WeeklyDiscountInfo{
			Id:          GoUtil.Int(v1["Id"]),
			Discount:    GoUtil.Int(v1["Discount"]),
			WeeklyLimit: GoUtil.Int(v1["Limit"]),
		}
	}
	return res
}

func GetEnergyShopReward(ChargeId int, First bool) []*item.Item {
	data, err := gamedata.GetData(CFG_ENERGY_SHOP)
	if err != nil {
		return nil
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "ChargeId") == ChargeId {
			Energy := gamedata.GetIntValue(v, "Energy")
			if !First {
				Energy += gamedata.GetIntValue(v, "FirstCharge")
			}
			return []*item.Item{item.NewItem(item.ITEM_ENERGY_ID, Energy)}
		}
	}
	return nil
}

func GetSpecialShopReward(ChargeId int) []*item.Item {
	data, err := gamedata.GetData(CFG_SEPCIAL_SHOP)
	if err != nil {
		return nil
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "ChargeId") == ChargeId {
			return item.ParseItem(gamedata.GetValue(v, "Items"))
		}
	}
	return nil
}

func GetSpecialShopType(ChargeId int) int {
	data, err := gamedata.GetData(CFG_SEPCIAL_SHOP)
	if err != nil {
		return 0
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "ChargeId") == ChargeId {
			return gamedata.GetIntValue(v, "Type")
		}
	}
	return 0
}

func GetFreeShopReward(ChargeId int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CFG_FREE_SHOP, ChargeId)
	if err != nil {
		return nil
	}
	Item := gamedata.GetValue(data, "Items")
	return item.ParseItem(Item)
}

func GetSpecialShopGrade(Money float64, Type int) int {
	type grade struct {
		id    int
		money float64
	}
	data, err := gamedata.GetData(CFG_SEPCIAL_SHOP)
	if err != nil {
		return 0
	}
	l := make([]grade, 0)
	for _, v := range data {
		if gamedata.GetIntValue(v, "Type") != Type {
			continue
		}
		l = append(l, grade{
			id:    gamedata.GetIntValue(v, "Grade"),
			money: GetMoneyCharge(gamedata.GetIntValue(v, "ChargeId")),
		})
	}
	sort.Slice(l, func(i, j int) bool {
		return l[i].money > l[j].money
	})

	for _, v := range l {
		if Money <= v.money {
			return v.id
		}
	}
	return l[0].id
}

func GetUnlockShopLv() int {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "shop_unlock_lv")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetSpecialShopCount() int {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "special_shop_count")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetAdChargeId() int {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "ad_charge_id")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetPiggyDuration() int {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "pig_duration")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetPiggyMaxDia() int {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "pig_max_diamond")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetPiggyRandDia() (int, int) {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "pig_diamond_rand")
	if err != nil {
		return 0, 0
	}
	str := gamedata.GetStringValue(data, "Value")
	strArr := strings.Split(str, ",")
	if len(strArr) != 2 {
		return 0, 0
	}
	return GoUtil.Int(strArr[0]), GoUtil.Int(strArr[1])
}

func GetPiggyBankChargeId() int {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "pig_charge_id")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetGiftReward(ChargeId int) []*item.Item {
	data, err := gamedata.GetData(CFG_CHARGE_GIFT)
	if err != nil {
		return nil
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "ChargeId") == ChargeId {
			return item.ParseItem(gamedata.GetValue(v, "Items"))
		}
	}
	return nil
}

func GetGiftLimit(ChargeId int) int {
	data, err := gamedata.GetData(CFG_CHARGE_GIFT)
	if err != nil {
		return 0
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "ChargeId") == ChargeId {
			return gamedata.GetIntValue(v, "Count")
		}
	}
	return 0
}
func GetGiftId(ChargeId int) int {
	data, err := gamedata.GetData(CFG_CHARGE_GIFT)
	if err != nil {
		return 0
	}
	for k, v := range data {
		if gamedata.GetIntValue(v, "ChargeId") == ChargeId {
			return GoUtil.Int(k)
		}
	}
	return 0
}

func GetChargeInfo(ChargeId int) (float64, string) {
	data, err := gamedata.GetDataByIntKey(CFG_CHARGE, ChargeId)
	if err != nil {
		return 0, ""
	}
	return gamedata.GetFloatValue(data, "Money"), gamedata.GetStringValue(data, "Unit")
}

func GetProduceName(ChargeId int) string {
	data, err := gamedata.GetDataByIntKey(CFG_CHARGE, ChargeId)
	if err != nil {
		return ""
	}
	return gamedata.GetStringValue(data, "Remark")
}

func GetWishCount(ItemId int) int {
	data, err := gamedata.GetDataByIntKey(CFG_CHARGE_WISH, ItemId)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Wish")
}

func GetPetCoinShopReward(ChargeId int) []*item.Item {
	data, err := gamedata.GetData(CFG_CHARGE_PETCOIN)
	if err != nil {
		return nil
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "ChargeId") == ChargeId {
			return gamedata.GetItemList(v, "Items")
		}
	}
	return nil
}

func GetWeeklyDiscountStartEnd() (int64, int64) {
	data, err := gamedata.GetDataByKey(CFG_CHARGE_CONST, "weekly_discount_time")
	if err != nil {
		return 0, 0
	}
	str := gamedata.GetStringValue(data, "Value")
	strArr := strings.Split(str, "|")
	if len(strArr) != 2 {
		return 0, 0
	}
	return GoUtil.ParseTime(strArr[0]), GoUtil.ParseTime(strArr[1])
}
