package nt

import (
	"../conf"
	pb "../protos"
	"../utils"
	"sort"
	"sync"
)

// 所有配置
type allConfT struct {
	Vip   []*pb.VipConfItem
	muVip sync.Mutex

	Lobbies   map[string]*conf.Conf
	muLobbies sync.Mutex

	Games   []*pb.GameItemEntry
	muGames sync.Mutex

	Channels   []*pb.GameItemEntry
	muChannels sync.Mutex

	ClientAddrsConf *pb.S_Ntf_Config
	muClientAddrs   sync.Mutex

	Notices   map[int32]*pb.Notice
	muNotices sync.Mutex

	Kv   map[string]interface{}
	muKv sync.Mutex

	Wallet   *walletConf
	muWallet sync.Mutex

	Checkin7   []*pb.DimInt64
	muCheckin7 sync.Mutex

	GameTasks   []*pb.GameTask
	muGameTasks sync.Mutex

	Turntable  *TurntableConf
	muTurntable sync.Mutex

	cacheIdItem map[int64][]*pb.GameItemEntry
}

//////////////////////

type ZkEventbusConf struct {
	Addrs     []string `json:"addrs"`
	Username  string   `json:"username"`
	Password  string   `json:"password"`
	ClusterId string   `json:"cluster_id"`
}

type ZkMqConf struct {
	ZkEventbusConf
}

////////////////////// wallet
type zzylConf struct {
	Url    string `json:"url"`
	Agent  string `json:"agent"`
	DesKey string `json:"desKey"`
	Md5Key string `json:"md5Key"`
}

type dbdzConf struct {
	Url    string `json:"url"`
	Agent  string `json:"agent"`
	Dc     string `json:"dc"`
	AesIv  string `json:"aesIv"`
	AesKey string `json:"aesKey"`
}

type hboConf struct {
	Url   string `json:"url"`
	Agent string `json:"agent"`
	Key   string `json:"key"`
}

type cq9Conf struct {
	Cq9Token string `json:"cq9token"`
	Cq9MainUrl string `json:"cq9mainurl"`
}

type igConf struct {
	IgKey string `json:"igkey"`
	IgDzAddr string `json:"igdzaddr"`
	IgCtcAddr string `json:"igctcaddr"`
	IgLiveAddr string `json:"igliveaddr"`
	IgAboutMoneyAddr string `json:"igaboutmoneyaddr"`
}

type vgConf struct {
	VgAddr string `json:"vgaddr"`
}

type dbdzqpConf struct {
	Qp_url string `json:"qpurl"`
	Qp_dbdzAgent string `json:"qpdbdzagent"`
	Qp_DC string `json:"qpdc"`
	Qp_aesIvDefValue string `json:"qpaesivdefvalue"`
	Qp_aesKeyDefValue string `json:"qpaeskeydefvalue"`
}

type fgConf struct {
	Fg_url string `json:"fgurl"`
	Fg_merchantname string `json:"fgmerchantname"`
	Fg_merchantcode string `json:"fgmerchantcode"`
}

type walletConf struct {
	Zzyl zzylConf `json:"zzyl"`
	Dbdz dbdzConf `json:"dbdz"`
	Hbo  hboConf  `json:"hbo"`
	CQ9	 cq9Conf `json:"cq9"`
	IG   igConf `json:"ig"`
	VG   vgConf `json:"vg"`
	Dbdzqp dbdzqpConf `json:"dbdzqp"`
	FG   fgConf `json:"fg"`
}

// 转盘概率配置
type RewardProb struct {
	Reward int64
	P int32
}

type TurntableConf struct {
	RewardProb []*RewardProb
}

var DefaultTurnTableConf = NewTurntableConf()

func NewTurntableConf() *TurntableConf {
	tc := &TurntableConf{}
	tc.RewardProb =append(tc.RewardProb, &RewardProb{
		Reward: 80,
		P:      950,
	})
	tc.RewardProb =append(tc.RewardProb, &RewardProb{
		Reward: 180,
		P:      30,
	})
	tc.RewardProb =append(tc.RewardProb, &RewardProb{
		Reward: 580,
		P:      10,
	})
	tc.RewardProb =append(tc.RewardProb, &RewardProb{
		Reward: 800,
		P:      6,
	})
	tc.RewardProb =append(tc.RewardProb, &RewardProb{
		Reward: 1800,
		P:      3,
	})
	tc.RewardProb =append(tc.RewardProb, &RewardProb{
		Reward: 5800,
		P:      1,
	})
	tc.RewardProb =append(tc.RewardProb, &RewardProb{
		Reward: 18000,
		P:      0,
	})
	tc.RewardProb =append(tc.RewardProb, &RewardProb{
		Reward: 88800,
		P:      0,
	})

	tc.Sort()

	return tc
}

func (self *TurntableConf)Len() int {
	return len(self.RewardProb)
}

func (self *TurntableConf)Less(i, j int) bool {
	return self.RewardProb[i].P > self.RewardProb[j].P
}

func (self *TurntableConf)Swap(i, j int) {
	self.RewardProb[i], self.RewardProb[j] = self.RewardProb[j], self.RewardProb[i]
}

func (self *TurntableConf)Sort() {
	sort.Sort(self)
}

func (self *TurntableConf) GetRewards() []int64 {
	var r []int64
	for _, item := range self.RewardProb {
		r = append(r, item.Reward)
	}
	return r
}

// 按 p 随机一个
func (self *TurntableConf)Choice() int {
	n := int32(utils.Intn(1000))	// 0-999

	indexOfMin := 0
	min := int64(0x7FffFFffFFffFFff)

	for i := len(self.RewardProb) - 1; i >= 0; i-- {
		if n < self.RewardProb[i].P {
			return i
		}
		if self.RewardProb[i].Reward < min {
			min = self.RewardProb[i].Reward
			indexOfMin = i
		}
	}
	return indexOfMin
}