package main

import (
	"sort"
	"time"

	"mongo.games.com/goserver/core/logger"

	"mongo.games.com/game/common"
	"mongo.games.com/game/model"
	webapi_proto "mongo.games.com/game/protocol/webapi"
	"mongo.games.com/goserver/core/module"
)

const (
	MoneyRatio = 100 // 默认
)

var VipMgrSington = &VipMgr{
	VIPcfg: make(map[string]*webapi_proto.VIPcfgDataList),
}

type VipMgr struct {
	VIPcfg map[string]*webapi_proto.VIPcfgDataList
}

func (this *VipMgr) ModuleName() string {
	return "VipMgr"
}

func (this *VipMgr) Init() {
	if !model.GameParamData.UseEtcd {
		// 后台说现在没有不走ETCD情况~
	} else {

		EtcdMgrSington.InitUpdateVIPcfg()
	}
}

func (this *VipMgr) UpdateVIPcfg(cfg *webapi_proto.VIPcfgDataList) {
	this.VIPcfg[cfg.Platform] = cfg
}

func (this *VipMgr) GetVIPcfg(platform string) *webapi_proto.VIPcfgDataList {
	if platform == common.Platform_Rob {
		return nil
	}

	if this.VIPcfg[platform] == nil {
		logger.Logger.Error("玩家平台错误  platform = ", platform)
		return nil
	}
	sort.Slice(this.VIPcfg[platform].List, func(i, j int) bool {
		return this.VIPcfg[platform].List[i].VipId < this.VIPcfg[platform].List[j].VipId
	})
	return this.VIPcfg[platform]
}

func (this *VipMgr) GetVipCfg(platform string, vipLevel int32) *webapi_proto.VIPcfg {
	if this.VIPcfg[platform] != nil {
		for _, cfg := range this.VIPcfg[platform].List {
			if cfg.VipId == vipLevel {
				return cfg
			}
		}
	}
	return nil
}

// 获取VIP赛季积分加成
func (this *VipMgr) GetVipPointsExtra(platform string, vipLevel int32) int32 {
	if this.VIPcfg[platform] == nil {
		return 0
	}
	for _, cfg := range this.VIPcfg[platform].List {
		if cfg.VipId == vipLevel {
			return cfg.Privilege4
		}
	}
	return 0
}

// 获取VIP钻石加成
func (this *VipMgr) GetVipDiamondExtra(platform string, vipLevel int32) int32 {
	if this.VIPcfg[platform] == nil {
		return 0
	}
	for _, cfg := range this.VIPcfg[platform].List {
		if cfg.VipId == vipLevel {
			return cfg.Privilege6
		}
	}
	return 0
}
func (this *VipMgr) GetVIPLevelCfg(platform string, vip int32) *webapi_proto.VIPcfg {
	vips := this.GetVIPcfg(platform)
	if vip > 0 && vips != nil && len(vips.List) > 0 {
		for _, v := range vips.List {
			if v.VipId == vip {
				return v
			}
		}
	}
	return nil
}

func (this *VipMgr) Update() {

}

func (this *VipMgr) Shutdown() {
	module.UnregisteModule(this)
}

func init() {
	module.RegisteModule(VipMgrSington, time.Second, 0)
}
