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

type DiscreteRuleConfigRow struct {
	ID        int          // #离散规则信息ID
	Reward    []ItemReward // 奖励
	Condition string       // 条件
}

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

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

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

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

func (p *discreteRuleConfig) GetAll() map[int]*DiscreteRuleConfigRow {
	var ret = make(map[int]*DiscreteRuleConfigRow)
	for _, v := range p.maps {
		ret[v.ID] = v
	}
	return ret
}

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