package decorateCfg

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

const (
	DECORATE_COST   = "DecorateCost"
	INDOOR_PROGRESS = "IndoorProgress"
	DECORATE_CONST  = "DecorateConst"
	DECORATE_AREA   = "DecorateArea" // 装饰区域
)

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

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 {
		if gamedata.GetIntValue(v, "AreaId") == AreaId && gamedata.GetIntValue(v, "SortId") == SortId {
			return gamedata.GetIntValue(v, "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 {
		if gamedata.GetIntValue(v, "AreaId") == AreaId && gamedata.GetIntValue(v, "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 {
		if gamedata.GetIntValue(v, "AreaId") == AreaId {
			Sort := gamedata.GetIntValue(v, "SortId")
			if Sort == 0 {
				continue
			}
			SortIdList = append(SortIdList, Sort)
		}
	}
	return SortIdList
}

func GetProgressReward(Scene, Lv int) []*item.Item {
	value, err := gamedata.GetData(INDOOR_PROGRESS)
	if err != nil {
		log.Debug("IndoorProgress GetProgressReward not found")
		return nil
	}
	for _, v := range value {
		if gamedata.GetIntValue(v, "Scene") == Scene && gamedata.GetIntValue(v, "Lv") == Lv {
			return gamedata.GetItemList(v, "AreaReward")
		}
	}
	return nil
}

func GetDecoarteReward(Scene int, Lv int) []*item.Item {
	value, err := gamedata.GetData(INDOOR_PROGRESS)
	if err != nil {
		log.Debug("IndoorProgress GetDecoarteReward not found")
		return nil
	}
	for _, v := range value {
		if gamedata.GetIntValue(v, "Scene") == Scene && gamedata.GetIntValue(v, "Lv") == Lv {
			return gamedata.GetItemList(v, "Item")
		}
	}
	return nil
}

func GetAreaReward(AreaId int) []*item.Item {
	value, err := gamedata.GetDataByIntKey(DECORATE_AREA, AreaId)
	if err != nil {
		log.Debug("IndoorProgress GetDecoarteReward not found")
		return nil
	}
	return gamedata.GetItemList(value, "Items")
}

// 获取一键装饰的解锁等级
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")
}

// 获取每次装饰增加的体力值
func GetDecorateAddEnergy() int {
	data, err := gamedata.GetDataByKey(DECORATE_CONST, "AddEnergy")
	if err != nil {
		log.Debug("GetDecorateAddEnergy not found")
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetAreaPerPExp(AreaId int) int {
	data, err := gamedata.GetDataByIntKey(DECORATE_AREA, AreaId)
	if err != nil {
		log.Debug("DecorateArea GetAreaPerPExp not found AreaId:%v", AreaId)
		return 0
	}
	return gamedata.GetIntValue(data, "PerPExp")
}

func GetDecoratePExp(AreaId, SortId int) int {
	data, err := gamedata.GetData(DECORATE_COST)
	if err != nil {
		log.Debug("DecorateArea GetDecoratePExp not found")
		return 0
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "AreaId") == AreaId && gamedata.GetIntValue(v, "SortId") == SortId {
			return gamedata.GetIntValue(v, "PetExp")
		}
	}
	return 0
}

func GetAllPartId() []int {
	data, err := gamedata.GetDataByKey(DECORATE_CONST, "AllPartId")
	if err != nil {
		log.Debug("DecorateCost GetAllPartId not found")
		return []int{1505, 1515, 1525, 1535}
	}
	var PartIdList []int
	str := gamedata.GetStringValue(data, "Value")
	r1 := strings.Split(str, "|")
	for _, v := range r1 {
		id, _ := strconv.Atoi(v)
		PartIdList = append(PartIdList, id)
	}
	return PartIdList
}

func GetIdBySenceAndLv(Sence, Lv int) int {
	data, err := gamedata.GetData(INDOOR_PROGRESS)
	if err != nil {
		log.Debug("DecorateArea GetIdBySenceAndLv not found")
		return 0
	}
	for k, v := range data {
		if gamedata.GetIntValue(v, "Scene") == Sence && gamedata.GetIntValue(v, "Lv") == Lv {
			id, _ := strconv.Atoi(k)
			return id
		}
	}
	return 0
}

func GetPartNumByAreaId(AreaId int) map[int]int {
	data, err := gamedata.GetData(INDOOR_PROGRESS)
	if err != nil {
		log.Debug("DecorateArea GetPartNumByAreaId not found AreaId:%v", AreaId)
		return nil
	}
	res := make(map[int]int)
	for k, v := range data {
		Sence := gamedata.GetIntValue(v, "Scene")
		if Sence != AreaId {
			continue
		}
		id, _ := strconv.Atoi(k)
		PartNum := gamedata.GetIntValue(v, "Part")
		if PartNum == 0 {
			continue
		}
		res[id] = PartNum
	}
	return res
}

func GetAreaIdByIndoorId(IndoorId int) int {
	data, err := gamedata.GetDataByIntKey(INDOOR_PROGRESS, IndoorId)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Scene")
}
