package gmdata

import (
	"encoding/json"
	"github.com/sirupsen/logrus"
	"os"
)

// NoviceTaskItem 任务详情
type NoviceTaskItem struct {
	ID          int64     `json:"ID"`
	DayValue    int64     `json:"dayValue"`
	TaskType    int64     `json:"taskType"`
	TaskNum     int64     `json:"taskNum"`
	ActiveAward int64     `json:"activeAward"`
	AwardGroup  [][]int64 `json:"awardGroup"`
	Describe    string    `json:"describe"`
}

// NoviceTaskAwardItem 活跃度奖励
type NoviceTaskAwardItem struct {
	ID          int64     `json:"ID"`
	ActiveValue int64     `json:"activeValue"`
	ShowType    int64     `json:"showType"`
	AwardGroup  [][]int64 `json:"awardGroup"`
	Describe    string    `json:"describe"`
}

// json配置列表
var (
	NoviceTaskList      []*NoviceTaskItem
	NoviceActivityAward []*NoviceTaskAwardItem
)

// loadNoviceTask 加载配置信息
func loadNoviceTask(confPath string) {
	// 任务列表
	loadNoviceTaskFile(confPath+"/"+"SevenDaysActivityTaskList.json", &NoviceTaskList)

	// 活跃度奖励
	loadNoviceTaskFile(confPath+"/"+"SevenDaysActivityReward.json", &NoviceActivityAward)
}

func loadNoviceTaskFile(filePath string, val interface{}) {
	ptrFile, err := os.Open(filePath)
	if err != nil {
		logrus.Fatalln("读取json文件失败", err)
	}
	defer ptrFile.Close()

	decoder := json.NewDecoder(ptrFile)
	if err = decoder.Decode(&val); err != nil {
		logrus.Fatalln("loadNoviceTaskFile Decoder failed ", filePath, err.Error())
	}
}

// GetNoviceTask 获取单个任务
func GetNoviceTask(id int64) *NoviceTaskItem {
	for _, v := range NoviceTaskList {
		if v.ID == id {
			return v
		}
	}

	return nil
}

// GetNoviceAward 获取单个奖励
func GetNoviceAward(id int64) *NoviceTaskAwardItem {
	for _, v := range NoviceActivityAward {
		if v.ID == id {
			return v
		}
	}

	return nil
}
