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

type RankRewardConfigRow struct {
	ID     int          // #排行榜奖励ID
	RankID int          // 关联排行榜ID
	Rank   RangeInt32   // 奖励名次
	Reward []ItemReward // 奖励
}

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

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

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

func (p *rankRewardConfig) GetRewardByRank(rid, rank int) []ItemReward {
	for _, v := range p.maps {
		if v.RankID == rid && rank >= v.Rank.Min && rank <= v.Rank.Max {
			return v.Reward
		}
	}
	return nil
}

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

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