package teen

import (
	"server/common"
	"server/events"
	"server/game/room"
	"server/msg"

	"github.com/name5566/leaf/log"
)

func handleEvents() {
	for event := range events.EventChan {
		switch event.Type {
		case events.EventTeenPattiGameInfo:
			event.Agent.WriteMsg(&msg.ReqGameInfo{
				GameId:            "teen_patti",
				TeenPattiRoomList: convertToMsgRoomList(GameConfig.RoomList),
			})
		case events.EventLeaveTeenPattiRoom:
			m := event.Data.(*msg.ReqLeaveRoom)
			room, exists := roomStorage[m.RoomId]
			if !exists {
				log.Debug("Room %s not found", m.RoomId)
				return
			}
			LeaveTeenPattiRoom(room, m.UserId, "1")
		case events.EventJoinTeenPattiRoom:
			handleJoinTeenPattiRoom(event)

		case events.EventTeenPattiPlayerOptAction:
			// m := event.Data.(*msg.ResPlayerOptAction)
			log.Debug("EventTeenPattiPlayerOptAction")
			m := event.Data.(*msg.ResPlayerOptAction)
			// 查询房间信息
			TeenPattiRoom := getRoomInfo(m.RoomId)
			if TeenPattiRoom != nil {
				//说明正常获取
				recvPlayerOptAction(TeenPattiRoom, m.SitPos, m.PlayerOpt)
			} else { //说明有错误
				log.Error("Teen_Patti_Room is nil")
			}
		}
	}
}

// 获取玩家的昵称
func GetNickname(p *room.Player) string {
	userData := p.UserData.(*common.UserData)
	return userData.Nickname
}

// 获取玩家的头像
func GetAvatar(p *room.Player) string {
	userData := p.UserData.(*common.UserData)
	return userData.Head_Image
}
