package main

import (
	"../cross"
	"../log"
	"../mod"
	"../nt"
	pb "../protos"
	"../rpc"
	"../utils"
	"go.uber.org/zap"
)

type MgrEvent struct{}

// 变更金钱
func (self *MgrEvent) OnMgrChangeBalance(guid uint64, delta int64, balance int64) {
	nt.WriteMgrMsg(guid, int32(pb.Event_MGR_CHANGE_BALANCE), "log", utils.H{"delta": delta, "balance": balance})
}

// 变更银行
func (self *MgrEvent) OnMgrChangeBank(guid uint64, delta int64, bank int64) {
	nt.WriteMgrMsg(guid, int32(pb.Event_MGR_CHANGE_BANK), "log", utils.H{"delta": delta, "bank": bank})
}

// 禁止用户
func (self *MgrEvent) OnMgrDisableUser(guid uint64, status int64) {
	nt.WriteMgrMsg(guid, int32(pb.Event_MGR_DISABLE_USER), "log", utils.H{"guid": guid, "status": status})
}

// 变更游戏或渠道配置
func (self *MgrEvent) OnMgrChangeGameConfig(cmd string) {
	nt.WriteMgrMsg(0, int32(pb.Event_MGR_CHANGE_ARCH), "log", utils.H{"cmd": cmd})
}

// 发布跑马灯
func (self *MgrEvent) OnMgrPublishMarquee(cmd string, content string) {
	nt.WriteMgrMsg(0, int32(pb.Event_MGR_PUBLISH_MARQUEE), "log", utils.H{
		"cmd":  cmd,
		"text": content,
	})
}

// 发布私人郵件
func (self *MgrEvent) OnMgrPublishPrivateMail(cmd string, content string) {
	nt.WriteMgrMsg(0, int32(pb.Event_MGR_PUBLISH_MARQUEE), "log", utils.H{
		"cmd":  cmd,
		"text": content,
	})
}

// 更改客户端配置
func (self *MgrEvent) OnMgrChangeClientConfig(cmd, text string) {
	nt.WriteMgrMsg(0, int32(pb.Event_MGR_CHANGE_CLIENT_ADDRS), "log", utils.H{
		"cmd":  cmd,
		"text": text,
	})
}

// 踢人
func (self *MgrEvent) OnMgrKickOff(guids ...uint64) {
	nt.WriteMgrMsg(0, int32(pb.Event_MGR_KICKOFF), "log", utils.H{
		"guids": guids,
	})
}

// 踢服务器
func (self *MgrEvent) OnMgrOfflineLogic(id uint64) {
	nt.WriteMgrMsg(0, int32(pb.Event_MGR_OFFLINE), "log", utils.H{
		"id": id,
	})
}

func (self *MgrEvent) OnUserLevelup(guid uint64, oldLevel, newLevel int32, bets int64) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_LEVELUP), "db", utils.H{
		"oldLevel": oldLevel,
		"newLevel": newLevel,
		"bets":     bets,
	})
	//  rpc
	go func() {
		addr, err := rpc.GetUserAgentRpcUrl(guid)
		if err != nil {
			return
		}
		args := &rpc.RpcArgs{
			Cmd: "vip_up",
			Params: utils.H{
				"guid":   guid,
				"vip":    oldLevel,
				"newVip": newLevel,
			},
		}
		res := rpc.RpcResult{}
		err = rpc.RpcRequest(addr, args, &res)
		if err != nil {
			log.Warn("rpc vip_up", zap.Uint64("guid", guid))
		}
	}()
}

func (self *MgrEvent) OnUserReward(guid uint64, delta, newBalance int64) {
	log.Info("OnUserReward", zap.Uint64("guid", guid), zap.Int64("delta", delta), zap.Int64("newBalance", newBalance))

	nt.WriteUserMsg(guid, int32(pb.Event_USER_REWARD), "db", utils.H{
		"delta":   delta,
		"balance": newBalance,
		"reason":  pb.ReasonBalanceChange_PROMOTE,
	})
}

//////////////////// 其他

//
func (self *MgrEvent) OnDefCmdUnsupported(cmd string) {
	nt.WriteDefMsg(0, int32(pb.Event_DEF_CMD_UNSUPPORTED), "alm", utils.H{
		"guid": 0,
		"cmd":  cmd,
	})
}

// 玩家充钱，但玩家不在线的时候，由此服务器充值成功
func (self *MgrEvent) OnUserRecharge(cmd string, guid uint64, delta, orderId, newVal int64) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_RECHARGE), "db", utils.H{
		"guid":       guid,
		"delta":      delta,
		"orderId":    orderId,
		"newBalance": newVal,
	})
	// 汇总
	_, err := mod.AddUserRecharged(guid, delta)
	if err != nil {
		log.Error("AddUserRecharged", zap.Uint64("guid", guid), zap.Int64("delta", delta))
	}

	// 当天充值
	err = cross.TurntableOnUserRecharge(guid, delta)
	if err != nil {
		log.Error("AddUserRecharged", zap.Uint64("guid", guid), zap.Int64("delta", delta))
	}

}

func (self *MgrEvent) OnUserWithdraw(cmd string, guid uint64, delta, newVal int64) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_WITHDRAW), "db", utils.H{
		"guid":       guid,
		"delta":      delta,
		"newBalance": newVal,
	})
}
