// 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 achieveTaskConfig struct {
	maps map[int]*AchieveTaskConfigRow
}

type AchieveTaskConfigRow struct {
	ID        int          // #任务ID
	Order     int          //顺序
	Enable    int          // 是否开启(0否1是)
	Type      int          // 任务类型(1基本类型2邀请好友相关)
	Condition RangeInt32   // 完成条件等级
	Reward    []ItemReward // 奖励
	Url       string       // 链接
	Icon      string       // Icon地址
}

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

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

func (p *achieveTaskConfig) 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]*AchieveTaskConfigRow)
	for index, data := range list {
		loadConfig := &AchieveTaskConfigRow{}
		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 *achieveTaskConfig) OnAfterLoad(_ bool) {
}

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

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

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