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("GetStarCost err:%v, AreaId=%d, SortId=%d", err, AreaId, SortId)
		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("GetAreaId err:%v, id=%d", err, id)
		return 0
	}
	return gamedata.GetIntValue(value, "AreaId")
}

func GetNextAreaId(AreaId, SortId int) int {
	data, err := gamedata.GetData(DECORATE_COST)
	if err != nil {
		log.Debug("GetNextAreaId err:%v, AreaId=%d, SortId=%d", err, AreaId, SortId)
		return 0
	}
	for k, v := range data {
		if gamedata.GetIntValue(v, "AreaId") == AreaId && gamedata.GetIntValue(v, "SortId") == SortId {
			UnionKey, err := strconv.Atoi(k)
			if err != nil {
				log.Debug("GetNextAreaId parse key err:%v, key=%s", err, k)
				return 0
			}
			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("GetAllSortIdByAreaId err:%v, AreaId=%d", err, AreaId)
		return nil
	}
	SortIdList := make([]int, 0, len(data)/5)
	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("GetProgressReward err:%v, Scene=%d, Lv=%d", err, Scene, Lv)
		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("GetDecoarteReward err:%v, Scene=%d, Lv=%d", err, Scene, Lv)
		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("GetAreaReward err:%v, AreaId=%d", err, AreaId)
		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 err:%v", err)
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetAreaPerPExp(AreaId int) int {
	data, err := gamedata.GetDataByIntKey(DECORATE_AREA, AreaId)
	if err != nil {
		log.Debug("GetAreaPerPExp err:%v, AreaId=%d", err, 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("GetDecoratePExp err:%v, AreaId=%d, SortId=%d", err, AreaId, SortId)
		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("GetAllPartId err:%v", err)
		return nil
	}
	str := gamedata.GetStringValue(data, "Value")
	r1 := strings.Split(str, "|")
	PartIdList := make([]int, 0, len(r1))
	for _, v := range r1 {
		id, err := strconv.Atoi(v)
		if err != nil {
			log.Debug("GetAllPartId parse id err:%v, v=%s", err, v)
			continue
		}
		PartIdList = append(PartIdList, id)
	}
	return PartIdList
}

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

func GetPartNumByAreaId(AreaId int) map[int]int {
	data, err := gamedata.GetData(INDOOR_PROGRESS)
	if err != nil {
		log.Debug("GetPartNumByAreaId err:%v, AreaId=%d", err, AreaId)
		return nil
	}
	res := make(map[int]int, len(data))
	for k, v := range data {
		Sence := gamedata.GetIntValue(v, "Scene")
		if Sence != AreaId {
			continue
		}
		id, err := strconv.Atoi(k)
		if err != nil {
			log.Debug("GetPartNumByAreaId parse id err:%v, key=%s", err, k)
			continue
		}
		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 {
		log.Debug("GetAreaIdByIndoorId err:%v, IndoorId=%d", err, IndoorId)
		return 0
	}
	return gamedata.GetIntValue(data, "Scene")
}
