package dailyTaskCfg

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

const (
	DAILY_TASK_CONST   = "DailyTaskConst"
	DAILY_TASK         = "DailyTask"
	DAILY_TASK_ACTIVE  = "DailyTaskActive"
	DAILY_TASK_JACKPOT = "DailyTaskJackpot"
)

func init() {
	gamedata.InitCfg(DAILY_TASK_CONST)
	gamedata.InitCfg(DAILY_TASK)
	gamedata.InitCfg(DAILY_TASK_ACTIVE)
	gamedata.InitCfg(DAILY_TASK_JACKPOT)
}

// 获取任务列表
func GetTaskList(Lv int) map[int]string {
	data, err := gamedata.GetData(DAILY_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 {
		lv := gamedata.GetIntValue(v, "Lv")
		if Lv >= lv {
			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
}

func GetTaskActiveId() []int {
	data, err := gamedata.GetData(DAILY_TASK_ACTIVE)
	if err != nil {
		log.Debug("GetTaskActiveId err:%v", err)
		return nil
	}
	r := make([]int, 0, len(data))
	for k := range data {
		Id, err := strconv.Atoi(k)
		if err != nil {
			log.Debug("GetTaskActiveId parse id err:%v, key=%s", err, k)
			continue
		}
		r = append(r, Id)
	}
	return r
}

func GetTaskActiveById(Id int) int {
	data, err := gamedata.GetDataByIntKey(DAILY_TASK_ACTIVE, Id)
	if err != nil {
		log.Debug("GetTaskActiveById err:%v, Id=%d", err, Id)
		return 0
	}
	return gamedata.GetIntValue(data, "Num")
}

type Jackpot struct {
	Items   []*item.Item
	Is_card int
}

func GetTaskWeekJackpot() map[int]Jackpot {
	data, err := gamedata.GetData(DAILY_TASK_JACKPOT)
	if err != nil {
		log.Debug("GetTaskWeekJackpot err:%v", err)
		return nil
	}

	r := make(map[int]Jackpot, len(data))
	for k, v := range data {
		Id, err := strconv.Atoi(k)
		if err != nil {
			log.Debug("GetTaskWeekJackpot parse id err:%v, key=%s", err, k)
			continue
		}
		isCard := gamedata.GetIntValue(v, "CardPack")
		Item := item.ParseItem(gamedata.GetValue(v, "Items"))
		r[Id] = Jackpot{
			Items:   Item,
			Is_card: isCard,
		}
	}
	return r
}
