// this file is auto create by program, don't edit manually

package data

import (
	mhayaError "github.com/mhaya/error"
	mhayaLogger "github.com/mhaya/logger"
)

type dailyTaskConfig struct {
	maps map[int]*DailyTaskConfigRow
}

type DailyTaskConfigRow struct {
	ID        int          // #任务ID
	Enable    int          // 是否开启(0否1是)
	Type      int          // 任务类型(1骰子次数相关任务)
	Condition int          // 完成条件
	Reward    []ItemReward // 奖励
}

func (p *dailyTaskConfig) Name() string {
	return "dailyTaskConfig"
}

func (p *dailyTaskConfig) Init() {
	p.maps = make(map[int]*DailyTaskConfigRow)
}

func (p *dailyTaskConfig) OnLoad(maps interface{}, _ bool) (int, error) {
	list, ok := maps.([]interface{})
	if !ok {
		return 0, mhayaError.Error("maps convert to []interface{} error.")
	}

	loadMaps := make(map[int]*DailyTaskConfigRow)
	for index, data := range list {
		loadConfig := &DailyTaskConfigRow{}
		err := DecodeData(data, loadConfig)
		if err != nil {
			mhayaLogger.Warnf("decode error. [row = %d, %v], err = %s", index+1, loadConfig, err)
			continue
		}

		loadMaps[loadConfig.ID] = loadConfig
	}

	p.maps = loadMaps

	return len(list), nil
}

func (p *dailyTaskConfig) OnAfterLoad(_ bool) {
}

func (p *dailyTaskConfig) Get(pk int) (*DailyTaskConfigRow, bool) {
	i, found := p.maps[pk]
	return i, found
}

func (p *dailyTaskConfig) GetAll() map[int]*DailyTaskConfigRow {
	var ret = make(map[int]*DailyTaskConfigRow)
	for _, v := range p.maps {
		if v.Enable == 1 {
			ret[v.ID] = v
		}
	}
	return ret
}

func (p *dailyTaskConfig) Contain(pk int) bool {
	_, found := p.Get(pk)
	return found
}
