package room

import (
	"game/common/baseroom"
	"game/common/proto/pb"
)

func (rm *ColorRoom) notifyGameStart() {
	ntf := &pb.NtfColorGameStart{
		EndTime: rm.endTimeStatus(),
		Jackpot: rm.jackpotValue,
	}
	rm.Broadcast(pb.MsgId_NtfColorGameStartId, ntf)
}

func (rm *ColorRoom) notifyBetting() {
	ntf := &pb.NtfColorBetting{EndTime: rm.endTimeStatus()}
	rm.Broadcast(pb.MsgId_NtfColorBettingId, ntf)
}

func (rm *ColorRoom) notifyEndBetting() {
	ntf := &pb.NtfColorEndBetting{
		EndTime: rm.endTimeStatus(),
		AreaMul: rm.endBetAreaMul,
		Jackpot: rm.jackpotValue,
	}
	rm.Broadcast(pb.MsgId_NtfColorEndBettingId, ntf)
	rm.notifyBigUsers()
}

func (rm *ColorRoom) notifyOpenThreeDice() {
	rm.Broadcast(pb.MsgId_NtfColorOpenThreeDiceId, rm.ntfOpenThreeDice)
}

func (rm *ColorRoom) notifySettle() {
	rm.RangePlayer(func(u baseroom.IPlayer) bool {
		user := GetPlayer(u)
		rm.SendMsg(user, pb.MsgId_NtfColorSettleId, user.settleMsg)
		return true
	})
	rm.notifyBigUsers()
}

func (rm *ColorRoom) notifyBigUsers() {
	rm.Broadcast(pb.MsgId_NtfColorBigUserId, &pb.NtfColorBigUser{BigUser: rm.bigUsers})
}

func (rm *ColorRoom) notifyTrend() {
	rm.Broadcast(pb.MsgId_NtfColorTrendId, rm.getNotifyTrend())
}

// 每秒更新各投注区域投注信息
func (rm *ColorRoom) notifyColorBetAreaInfo() {
	rm.RangePlayer(func(u baseroom.IPlayer) bool {
		user := GetPlayer(u)
		for _, areaInfo := range rm.betAreaInfo {
			areaInfo.MyBet = user.totalBets[areaInfo.Area]
		}
		rm.SendMsg(user, pb.MsgId_NtfColorBetAreaInfoId, &pb.NtfColorBetAreaInfo{
			AreaInfos: rm.betAreaInfo,
		})
		return true
	})
}

// 每秒更新各投注区域投注信息
func (rm *ColorRoom) notifyKickoutUser(user *ColorPlayer, code pb.ErrCode) {
	rm.SendMsg(user, pb.MsgId_NtfKickOutUserId, &pb.NtfKickOutUser{Code: code})
}

// 每秒更新各投注区域投注信息
func (rm *ColorRoom) notifyPayoutFail(user *ColorPlayer, code pb.ErrCode) {
	rm.SendMsg(user, pb.MsgId_NtfPayoutFailId, &pb.NtfPayoutFail{Code: code})
}

// 推送房间信息给玩家
func (rm *ColorRoom) notifyColorRoomInfo(user *ColorPlayer) {
	ntf := &pb.NtfColorRoomInfo{
		Status:        rm.status,
		EndTime:       rm.endTimeStatus(),
		Config:        rm.getUserRoomConfig(user),
		User:          rm.getProtoUser(user),
		WaitStart:     nil,
		Start:         nil,
		Betting:       nil,
		EndBet:        nil,
		OpenThreeDice: nil,
		Settle:        nil,
	}
	switch rm.status {
	case pb.ColorGameStatus_CGS_WaitStart:
		ntf.WaitStart = &pb.ColorRoomWaitStart{Trends: rm.getNotifyTrend()}
	case pb.ColorGameStatus_CGS_Start:
		ntf.Start = &pb.ColorRoomStart{
			Trends: rm.getNotifyTrend(),
			Start: &pb.NtfColorGameStart{
				EndTime: rm.endTimeStatus(),
				Jackpot: rm.jackpotValue,
			},
		}
	case pb.ColorGameStatus_CGS_Betting:
		ntf.Betting = &pb.ColorRoomBetting{
			BetAreaInfo: &pb.NtfColorBetAreaInfo{
				AreaInfos: rm.betAreaInfo,
			},
		}
	case pb.ColorGameStatus_CGS_BetEnd:
		ntf.EndBet = &pb.ColorRoomEndBet{
			AreaMul: &pb.NtfColorEndBetting{
				EndTime: rm.endTimeStatus(),
				Jackpot: rm.jackpotValue,
				AreaMul: rm.endBetAreaMul,
			},
			BetAreaInfo: &pb.NtfColorBetAreaInfo{
				AreaInfos: rm.betAreaInfo,
			},
			BigUser: &pb.NtfColorBigUser{BigUser: rm.bigUsers},
		}
	case pb.ColorGameStatus_CGS_OpenThreeDice:
		ntf.OpenThreeDice = &pb.ColorRoomOpenThreeDice{Dices: rm.ntfOpenThreeDice}
	case pb.ColorGameStatus_CGS_Settle:
		ntf.Settle = &pb.ColorRoomSettle{
			Settle: user.settleMsg,
		}
	}
	rm.SendMsg(user, pb.MsgId_NtfColorRoomInfoId, ntf)
}
