package GuideTaskCfg

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

const (
	GUIDE_TASK_CONST  = "GuideTaskConst"
	GUIDE_TASK_TASK   = "GuideTaskTask"
	GUIDE_TASK_ACTIVE = "GuideTaskActive"
)

func init() {
	gamedata.InitCfg(GUIDE_TASK_CONST)
	gamedata.InitCfg(GUIDE_TASK_TASK)
	gamedata.InitCfg(GUIDE_TASK_ACTIVE)
}

func GetTaskRewardById(Id int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(GUIDE_TASK_TASK, Id)
	if err != nil {
		return nil
	}
	return gamedata.GetItemList(data, "Items")
}

func GetTaskById(Id int) string {
	data, err := gamedata.GetDataByIntKey(GUIDE_TASK_TASK, Id)
	if err != nil {
		return ""
	}
	return gamedata.GetStringValue(data, "Task")
}

func GetTaskActive(Id int) int {
	data, err := gamedata.GetDataByIntKey(GUIDE_TASK_TASK, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Active")
}
func GetUnlock(Id int) int {
	data, err := gamedata.GetDataByIntKey(GUIDE_TASK_TASK, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Unlock")
}

func GetActiveReward(Id int) ([]*item.Item, int) {
	data, err := gamedata.GetDataByIntKey(GUIDE_TASK_ACTIVE, Id)
	if err != nil {
		return nil, 0
	}
	return gamedata.GetItemList(data, "Items"), gamedata.GetIntValue(data, "Active")
}
func GetActiveReward2(reward []int, Active int) []*item.Item {
	data, err := gamedata.GetData(GUIDE_TASK_ACTIVE)
	if err != nil {
		return nil
	}
	for k, v := range data {
		active := gamedata.GetIntValue(v, "Active")
		if active < Active {
			continue
		}
		if GoUtil.InArray(GoUtil.Int(k), reward) {
			continue
		}
		return gamedata.GetItemList(v, "Items")
	}
	return nil
}

func GetUnlockLv() int {
	data, err := gamedata.GetDataByKey(GUIDE_TASK_CONST, "UnlockLv")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetDays() int {
	data, err := gamedata.GetDataByKey(GUIDE_TASK_CONST, "Days")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetTaskList(Lv int) map[int]string {
	r := make(map[int]string)
	data, err := gamedata.GetData(GUIDE_TASK_TASK)
	if err != nil {
		log.Debug("GetTaskList get data nil")
		return r
	}
	for k, v := range data {
		k1, _ := strconv.Atoi(k)
		r[k1] = gamedata.GetStringValue(v, "Task")
	}
	return r
}
