package room

import (
	"game/common/proto/pb"
	"github.com/fox/fox/ipb"
	"github.com/fox/fox/log"
)

func (rm *ColorRoom) checkEnterRoom(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.ReqEnterRoom) pb.ErrCode {
	_ = user
	_ = iMsg
	_ = req
	return pb.ErrCode_OK
}

func (rm *ColorRoom) onMatchRoom(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.ReqMatchRoom) {
	_ = req
	code := rm.checkEnterRoom(user, iMsg, &pb.ReqEnterRoom{})
	if code != pb.ErrCode_OK {
		rm.SendMsg(user, pb.MsgId_RspMatchRoomId, &pb.RspMatchRoom{Code: code})
		return
	}
	rm.enterRoom(user)
	return
}

func (rm *ColorRoom) enterRoom(user *ColorPlayer) {
	if rm.AddPayer(user) {
		user.resetData()
	}
	rm.notifyColorRoomInfo(user)
	log.Debug(rm.Log("玩家:%v进入房间", user.ID))
}

func (rm *ColorRoom) onEnterRoom(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.ReqEnterRoom) {
	code := rm.checkEnterRoom(user, iMsg, req)
	rm.SendMsg(user, pb.MsgId_RspEnterRoomId, &pb.RspEnterRoom{Code: code})
	if code != pb.ErrCode_OK {
		return
	}
	rm.enterRoom(user)
	return
}

func (rm *ColorRoom) checkLeaveRoom(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.ReqLeaveRoom) pb.ErrCode {
	_ = iMsg
	_ = req
	if user.totalBet == 0 {
		return pb.ErrCode_OK
	}
	if rm.status >= pb.ColorGameStatus_CGS_Settle || rm.status < pb.ColorGameStatus_CGS_Betting {
		return pb.ErrCode_OK
	}
	return pb.ErrCode_NotLeaveRoom
}

func (rm *ColorRoom) leaveRoom(user *ColorPlayer) {
	rm.DelPlayer(user.Id())
	rm.userMgr.Del(user.ID)
	rm.bindService.DelUserService(user.ID, pb.ServiceTypeId_STI_ColorGame)
	log.Debug(rm.Log("玩家:%v离开房间", user.ID))
}

func (rm *ColorRoom) onLeaveRoom(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.ReqLeaveRoom) {
	code := rm.checkLeaveRoom(user, iMsg, req)
	rm.SendMsg(user, pb.MsgId_RspEnterRoomId, &pb.RspEnterRoom{Code: code})
	if code == pb.ErrCode_OK {
		rm.leaveRoom(user)
	}
}

func (rm *ColorRoom) onUserOffline(user *ColorPlayer, iMsg *ipb.InternalMsg, ntf *pb.NtfUserOffline) {
	_ = ntf
	code := rm.checkLeaveRoom(user, iMsg, &pb.ReqLeaveRoom{})
	if code == pb.ErrCode_OK {
		rm.leaveRoom(user)
	}
}

func (rm *ColorRoom) checkBet(user *ColorPlayer, _ *ipb.InternalMsg, req *pb.ReqColorBetting) pb.ErrCode {
	if req.Area < pb.ColorBetArea_CBA_Yellow || req.Area > pb.ColorBetArea_CBA_Green3 {
		return pb.ErrCode_ParamErr
	}
	// 金币不足，无法下注
	if rm.GetGold(user) < user.totalBet+req.Bet {
		return pb.ErrCode_GoldNotEnough
	}
	if rm.roomCfg.TotalBetLimit < user.totalBet+req.Bet {
		return pb.ErrCode_TotalBetExceedsLimit
	}
	if rm.roomCfg.AreaBetLimit < user.totalBets[req.Area]+req.Bet {
		return pb.ErrCode_AreaBetExceedsLimit
	}
	return pb.ErrCode_OK
}

func (rm *ColorRoom) onBet(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.ReqColorBetting) {
	code := rm.checkBet(user, iMsg, req)
	if code != pb.ErrCode_OK {
		rm.SendMsg(user, pb.MsgId_RspColorBettingId, &pb.RspColorBetting{Code: code})
		return
	}
	curBetAreaInfo := rm.betAreaInfo[req.Area]
	_, _ = rm.AddGold(user, -req.Bet)
	curBetAreaInfo.TotalBet += req.Bet
	if user.totalBets[req.Area] == 0 {
		curBetAreaInfo.PlayerNum++
	}
	user.totalBets[req.Area] += req.Bet
	curBetAreaInfo.MyBet = user.totalBets[req.Area]
	rm.SendMsg(user, pb.MsgId_RspColorBettingId, &pb.RspColorBetting{
		Code:     code,
		AreaInfo: curBetAreaInfo,
	})
	return
}
