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 {
		log.Debug("GetTaskRewardById err:%v, Id=%d", err, Id)
		return nil
	}
	return gamedata.GetItemList(data, "Items")
}

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

func GetTaskActive(Id int) int {
	data, err := gamedata.GetDataByIntKey(GUIDE_TASK_TASK, Id)
	if err != nil {
		log.Debug("GetTaskActive err:%v, Id=%d", err, Id)
		return 0
	}
	return gamedata.GetIntValue(data, "Active")
}
func GetUnlock(Id int) int {
	data, err := gamedata.GetDataByIntKey(GUIDE_TASK_TASK, Id)
	if err != nil {
		log.Debug("GetUnlock err:%v, Id=%d", err, Id)
		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 {
		log.Debug("GetActiveReward err:%v, Id=%d", err, Id)
		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 {
		log.Debug("GetActiveReward2 err:%v, Active=%d", err, Active)
		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 {
		log.Debug("GetUnlockLv err:%v", err)
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

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

func GetTaskList(Lv int) map[int]string {
	data, err := gamedata.GetData(GUIDE_TASK_TASK)
	if err != nil {
		log.Debug("GetTaskList err:%v, Lv=%d", err, Lv)
		return nil
	}
	r := make(map[int]string, len(data))
	for k, v := range data {
		k1, err := strconv.Atoi(k)
		if err != nil {
			log.Debug("GetTaskList parse id err:%v, key=%s", err, k)
			continue
		}
		r[k1] = gamedata.GetStringValue(v, "Task")
	}
	return r
}
