package main

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

type LobbyEvent struct {
}

func (self *LobbyEvent) OnUserEnterLobby(guid uint64, ses nnet.ISession) {
	//go func() {
	//	amount, remain, ec := alms_status(guid)
	//	if ec == pb.ErrCode_OK {
	//		SendToClient(ses, guid, &pb.S_Ntf_Alms{
	//			Ec:     int32(ec),
	//			Amount: amount,
	//			Times:  int32(remain),
	//		})
	//	}
	//}()
}

func (self *LobbyEvent) OnUserLeaveGameFinally(guid uint64) {
	//sbalance, err := mod.GetUserAttr(guid, "balance")
	//if err != nil {
	//	return
	//}
	//balance, err := utils.StrToInt64(sbalance)
	//if err != nil {
	//	return
	//}
	//UA_OnLeaveGameOrOffline(guid, balance)
}

// 登录
// 每一次登录都会记录，所以，
// 1. 一次登录事件也意味着有一次登录请求事件
// 2. 失败的登录通过 S_Login 的 Ec 表示
func (self *LobbyEvent) OnUserLogin(guid uint64, data *pb.S_Login, ses nnet.ISession) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_LOGIN), "log", data)
	// 设置玩家所在 agent
	if data.Ec == 0 {
		mod.SaveUserAgent(guid, nt.InstConf.Id)
	}

	PostClientAddrsConfig(ses, guid)
	//PostUserUnreadCS(ses, guid)
	self.OnUserEnterLobby(guid, ses)
	gameapi.OnUserLogin_ClearTask(guid)
}

// 登出
func (self *LobbyEvent) OnUserLogout(guid uint64, balance int64, bank int64, failed int32, reason int32) {
	log.Info("OnUserLogout", zap.Uint64("guid", guid), zap.Int64("balance", balance), zap.Int64("bank", bank), zap.Int32("reason", reason))

	nt.WriteUserMsg(guid, int32(pb.Event_USER_LOGOUT), "log", utils.H{
		"balance": balance,
		"bank":    bank,
		"failed":  failed,
		"reason":  reason,
	})
	mod.RemoveUserAgent(guid, nt.InstConf.Id)
	gameapi.RemoveUserLogicByGuid(guid)
	gameapi.OnUserLogout_AddTask(guid)

	turntable_award_user(guid)

	self.OnUserLeaveGameFinally(guid)
}

// 更新设置
func (self *LobbyEvent) OnUserUpdateSettings(guid uint64, data *pb.C_UserSettings) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_UPDATE_SETTING), "log", data)

	LoginPostUpdateUserinfo(guid, data)
}

// 更新密码
func (self *LobbyEvent) OnUserUpdatePassword(guid uint64, data *pb.S_UpdatePassword) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_UPDATE_PASSWORD), "log", data)
}

// 绑定手机号
func (self *LobbyEvent) OnUserBindPhone(guid uint64, data *pb.S_BindPhone) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_BIND_PHONE), "log", data)
}

func (self *LobbyEvent) OnUserBindAlipay(guid uint64, data *pb.C_BindAlipay) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_BIND_PAY), "log", data)
	// 不用發送給登錄服，因為不需要用 alipay 登錄

}

func (self *LobbyEvent) OnUserPrivateMail(guid uint64, data *pb.S_PrivateMail) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_GET_MAIL), "log", data)
}

func (self *LobbyEvent) OnUserRemovePrivateMail(guid uint64, data *pb.C_RemPrivateMail) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_REM_MAIL), "log", data)
}

// 充值
func (self *LobbyEvent) OnUserRecharge(guid uint64, money, newVal, orderId int64) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_RECHARGE), "db", utils.H{
		"guid":       guid,
		"delta":      money,
		"newBalance": newVal,
		"orderId":    orderId,
	})

	err := cross.TurntableOnUserRecharge(guid, money)
	if err != nil {
		log.Error("", zap.Error(err), zap.Uint64("guid", guid))
	}

	log.Info("recharge", zap.Uint64("guid", guid), zap.Int64("delta", money),
		zap.Int64("newBalance", newVal), zap.Int64("orderId", orderId))
}

// 用户被踢
func (self *LobbyEvent) OnUserKickedoff(guid uint64) {
	log.Info("kickoff", zap.Uint64("guid", guid))
}

// 提现成功
func (self *LobbyEvent) OnUserWithdrawSuc(guid uint64, money, newVal int64) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_WITHDRAW), "db", utils.H{
		"guid":       guid,
		"delta":      money,
		"newBalance": newVal,
	})

	log.Info("withdraw", zap.Uint64("guid", guid),
		zap.Int64("delta", money), zap.Int64("newBalance", newVal))
}

// 存钱保险箱
func (self *LobbyEvent) OnUserUpdateBank(guid uint64, delta, newBalance, newBank int64, ses nnet.ISession) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_DEPOSITE), "log", utils.H{
		"delta":      delta,
		"newBalance": newBalance,
		"newBank":    newBank,
	})
	// 通知
	var reason int32
	if delta > 0 {
		reason = int32(pb.ReasonBalanceChange_TOBANK)
	} else {
		reason = int32(pb.ReasonBalanceChange_BANKOUT)
	}
	SendToClient(ses, guid, &pb.S_Ntf_UserBalance{
		Guid:    guid,
		Reason:  reason,
		Balance: newBalance,
		Delta:   -delta,
	})

	SendToClient(ses, guid, &pb.S_Ntf_UserBank{
		Reason: reason,
		Bank:   newBank,
		Delta:  delta,
	})
}

// 设置保险箱密码
func (self *LobbyEvent) OnUserUpdateBankPassword(guid uint64, data *pb.C_UpdateBankPassword) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_UPDATE_BANKER_PASS), "log", data)
}

// 保险箱密码输入错误
func (self *LobbyEvent) OnUserAuthBankFailed() {

}

func (self *LobbyEvent) OnUserPreEnterGame(guid uint64, gameId int64, ses nnet.ISession) {
	//sbalance, err := mod.GetUserAttr(guid, "balance")
	//if err != nil {
	//	return
	//}
	//balance, err := utils.StrToInt64(sbalance)
	//if err != nil {
	//	return
	//}
	//UA_OnEnterGame(guid, gameId, balance)
}

// 进入游戏成功
func (self *LobbyEvent) OnUserEnterGame(guid uint64, gameId int64, ses nnet.ISession) {

}

// 离开游戏
func (self *LobbyEvent) OnUserLeaveGame(guid uint64, gameId int64, ses nnet.ISession) {
	self.OnUserEnterLobby(guid, ses)

	self.OnUserLeaveGameFinally(guid)
}

// 领取奖金
func (self *LobbyEvent) OnUserReward(guid uint64, delta, balance int64, reason int32, ses nnet.ISession) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_REWARD), "db", utils.H{
		"delta":   delta,
		"balance": balance,
		"reason":  reason,
	})

	SendToClient(ses, guid, &pb.S_Ntf_UserBalance{
		Guid:    guid,
		Reason:  reason,
		Balance: balance,
		Delta:   delta,
	})

	log.Info("OnUserReward", zap.Uint64("guid", guid), zap.Int64("delta", delta), zap.Int64("newBalance", balance), zap.Int32("reason", reason))
}

// 领取红包
func (self *LobbyEvent) OnRedEnvelope(guid uint64, delta, balance int64, reason int32, ses nnet.ISession) {
	nt.WriteUserMsg(guid, int32(pb.Event_USER_REWARD), "db", utils.H{
		"delta":   delta,
		"balance": balance,
		"reason":  reason,
	})

	SendToClient(ses, guid, &pb.S_RedEnvelope{
		Ec:    0,
		Reward:  delta,
	})

	SendToClient(ses, ses.Id(), &pb.S_Ntf_UserBalance{
		Ec : 0,
		Balance: balance + delta,
	})

	log.Info("OnUserReward", zap.Uint64("guid", guid), zap.Int64("delta", delta), zap.Int64("newBalance", balance), zap.Int32("reason", reason))
}

// 系统错误
func (self *LobbyEvent) OnSysError(e string) {

}

func (self *LobbyEvent) OnLoginLostConnection(thisId uint64, loginIp string) {
	nt.WriteSysMsg(0, int32(pb.Event_SYS_ERR_LOGIN_OFFLINE), "alm", utils.H{
		"addr":    loginIp,
		"logicId": thisId,
	})
}

// 系统警告
func (self *LobbyEvent) OnSysWarn() {

}

// 系统事件
func (self *LobbyEvent) OnSysEvent() {

}

// 管理事件
func (self *LobbyEvent) OnMgrEvent() {

}

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

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

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

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

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

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