package passCfg

import (
	"server/game/mod/item"
	"server/gamedata"
	"slices"
)

const (
	CFG_PASS_TEMPLATE = "PassTemplate"
	CFG_PASS          = "Pass"
)

func init() {
	gamedata.InitCfg(CFG_PASS_TEMPLATE)
	gamedata.InitCfg(CFG_PASS)
}

func GetTemplate(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_PASS_TEMPLATE, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Template")
}

func GetActivityItemId(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_PASS_TEMPLATE, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "ItemId")
}

func GetLowChargeId(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_PASS_TEMPLATE, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "LowChargeId")
}

func GetHighChargeId(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_PASS_TEMPLATE, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "HighChargeId")
}

func GetNewLevel(Template int, Score int, Reward []int) []int {
	data, err := gamedata.GetData(CFG_PASS)
	if err != nil {
		return nil
	}
	NewReward := make([]int, 0, len(data))
	for _, v := range data {
		if gamedata.GetIntValue(v, "Template") != Template {
			continue
		}
		LevelScore := gamedata.GetIntValue(v, "TotalScore")
		Index := gamedata.GetIntValue(v, "Level")
		if Score >= LevelScore && !slices.Contains(Reward, Index) {
			NewReward = append(NewReward, Index)
		}
	}
	return NewReward
}

func GetFreeChargeItems(Template int, Reward []int) []*item.Item {
	data, err := gamedata.GetData(CFG_PASS)
	if err != nil {
		return nil
	}
	Items := make([]*item.Item, 0, len(data))
	for _, v := range data {
		if gamedata.GetIntValue(v, "Template") != Template {
			continue
		}
		Index := gamedata.GetIntValue(v, "Level")
		if slices.Contains(Reward, Index) {
			Items = append(Items, gamedata.GetItemList(data, "FreeReward")...)
		}
	}
	return Items
}

func GetLowChargeItems(Template int, Reward []int) []*item.Item {
	data, err := gamedata.GetData(CFG_PASS)
	if err != nil {
		return nil
	}
	Items := make([]*item.Item, 0, len(data))
	for _, v := range data {
		if gamedata.GetIntValue(v, "Template") != Template {
			continue
		}
		Index := gamedata.GetIntValue(v, "Level")
		if slices.Contains(Reward, Index) {
			Items = append(Items, gamedata.GetItemList(data, "LowReward")...)
		}
	}
	return Items
}

func GetHighChargeItems(Template int, Reward []int) []*item.Item {
	data, err := gamedata.GetData(CFG_PASS)
	if err != nil {
		return nil
	}
	Items := make([]*item.Item, 0, len(data))
	for _, v := range data {
		if gamedata.GetIntValue(v, "Template") != Template {
			continue
		}
		Index := gamedata.GetIntValue(v, "Level")
		if slices.Contains(Reward, Index) {
			Items = append(Items, gamedata.GetItemList(data, "HighReward")...)
		}
	}
	return Items
}
