package decorateCfg

import (
	"server/game/mod/item"
	"server/gamedata"
	"server/pkg/github.com/name5566/leaf/log"
	"strconv"
)

const (
	DECORATE_COST   = "DecorateCost"
	INDOOR_PROGRESS = "IndoorProgress"
	DECORATE_CONST  = "DecorateConst"
)

func init() {
	gamedata.InitCfg(DECORATE_COST)
	gamedata.InitCfg(INDOOR_PROGRESS)
	gamedata.InitCfg(DECORATE_CONST)
}

func GetStarCost(AreaId, SortId int) int {
	data, err := gamedata.GetData(DECORATE_COST)
	if err != nil {
		log.Debug("DecorateCost GetStarCost not found")
		return 0
	}
	for _, v := range data {
		v1 := v.(map[string]interface{})
		if gamedata.ParseInt(v1["AreaId"]) == AreaId && gamedata.ParseInt(v1["SortId"]) == SortId {
			return gamedata.ParseInt(v1["CostCount"])
		}
	}
	return 0
}

func GetAreaId(id int) int {
	value, err := gamedata.GetDataByIntKey("DecorateCost", id)
	if err != nil {
		log.Debug("DecorateCost GetAreaId id:%v not found", id)
		return 0
	}
	return gamedata.ParseInt(value["AreaId"])
}

func GetNextAreaId(AreaId, SortId int) int {
	data, err := gamedata.GetData(DECORATE_COST)
	if err != nil {
		log.Debug("DecorateCost GetStarCost not found")
		return 0
	}
	for k, v := range data {
		v1 := v.(map[string]interface{})
		if gamedata.ParseInt(v1["AreaId"]) == AreaId && gamedata.ParseInt(v1["SortId"]) == SortId {
			UnionKey, _ := strconv.Atoi(k)
			nextId := UnionKey + 1
			AreaId = GetAreaId(nextId)
			return AreaId
		}
	}
	return 0
}

func GetAllSortIdByAreaId(AreaId int) []int {
	data, err := gamedata.GetData(DECORATE_COST)
	if err != nil {
		log.Debug("DecorateCost GetAllSortIdByAreaId not found")
		return nil
	}
	var SortIdList []int
	for _, v := range data {
		v1 := v.(map[string]interface{})
		if gamedata.ParseInt(v1["AreaId"]) == AreaId {
			Sort := gamedata.ParseInt(v1["SortId"])
			if Sort == 0 {
				continue
			}
			SortIdList = append(SortIdList, Sort)
		}
	}
	return SortIdList
}

func GetProgressReward(Scene, Lv int) []*item.Item {
	value, err := gamedata.GetData("IndoorProgress")
	if err != nil {
		log.Debug("IndoorProgress GetProgressReward not found")
		return nil
	}
	for _, v := range value {
		v1 := v.(map[string]interface{})
		if gamedata.ParseInt(v1["Scene"]) == Scene && gamedata.ParseInt(v1["Lv"]) == Lv {
			return item.ParseItem(v1["Item"])
		}
	}
	return nil
}

// 获取一键装饰的解锁等级
func GetOneKeyDecorateUnlockLv() int {
	data, err := gamedata.GetDataByKey(DECORATE_CONST, "OneKey")
	if err != nil {
		log.Debug("GetOneKeyDecorateUnlockLv err:%v", err)
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}
