package randnameCfg

import (
	GoUtil "server/game_util"
	"server/gamedata"
	"strconv"
)

const (
	CFG_RAND_NAME = "RandName"
)

func init() {
	gamedata.InitCfg(CFG_RAND_NAME)
}

func GetRandName() string {
	data, _ := gamedata.GetData(CFG_RAND_NAME)
	Id := GoUtil.RandNum(1, len(data))
	v, err := gamedata.GetDataByIntKey(CFG_RAND_NAME, Id)
	if err != nil {
		return "Lily"
	}
	return gamedata.GetStringValue(v, "EnName")
}

func GetRandNames(n int) []string {
	rs := make([]string, 0)
	data, err := gamedata.GetData(CFG_RAND_NAME)
	if err != nil {
		for i := 0; i < n; i++ {
			id := strconv.Itoa(i + 1)
			rs[i] = "Lily" + id
		}
	}
	all := make([]int, len(data))
	index := 0
	for k := range data {
		ik := GoUtil.Int(k)
		all[index] = ik
		index++
	}
	ids := GoUtil.RandSliceNumNonAdjacent(all, n)
	for _, v := range ids {
		v, err := gamedata.GetDataByIntKey(CFG_RAND_NAME, v)
		if err != nil {
			rs = append(rs, "Lucy")
		}
		rs = append(rs, gamedata.GetStringValue(v, "EnName"))
	}
	return rs
}
