package base

import (
	"mongo.games.com/game/common"
	"mongo.games.com/game/proto"
	"mongo.games.com/game/protocol/server"
	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/netlib"
	"mongo.games.com/goserver/srvlib"
)

/*
 给机器人服务发消息
*/

var NpcServerAgentSingleton = &NpcServerAgent{}

type NpcServerAgent struct {
}

func (nsa *NpcServerAgent) OnConnected() {
}

func (nsa *NpcServerAgent) OnDisconnected() {
	RobotSceneDBGameFreeSync = make(map[int]bool)
}

func (nsa *NpcServerAgent) OnPlayerEnterScene(s *Scene, p *Player) {
	logger.Logger.Trace("(nsa *NpcServerAgent) OnPlayerEnterScene")
}

func (nsa *NpcServerAgent) OnPlayerLeaveScene(s *Scene, p *Player) {
	logger.Logger.Trace("(nsa *NpcServerAgent) OnPlayerLeaveScene")
}

func (nsa *NpcServerAgent) OnSceneClose(s *Scene) {
	logger.Logger.Trace("(nsa *NpcServerAgent) OnSceneClose")
}

func (nsa *NpcServerAgent) GetSession() *netlib.Session {
	return srvlib.ServerSessionMgrSington.GetSession(common.GetSelfAreaId(), common.RobotServerType, common.RobotServerId)
}

func (nsa *NpcServerAgent) sendPacket(packetId int, pack interface{}) bool {
	s := nsa.GetSession()
	if s != nil {
		s.Send(packetId, pack)
		return true
	}
	return false
}

func (nsa *NpcServerAgent) SyncDBGameFree(roomId int, DBGameFree *server.DB_GameFree) {
	if !RobotSceneDBGameFreeSync[roomId] {
		pack := &server.GRGameFreeData{
			RoomId:     proto.Int(roomId),
			DBGameFree: DBGameFree,
		}
		if nsa.sendPacket(int(server.SSPacketID_PACKET_GR_GameFreeData), pack) {
			RobotSceneDBGameFreeSync[roomId] = true
		}
	}
}

// Invite 邀请机器人
func (nsa *NpcServerAgent) Invite(roomId, cnt int, gameFreeId int32) bool {
	//logger.Logger.Trace("(nsa *NpcServerAgent) Invite", roomId, cnt, isAgent, gameFreeId)
	s := nsa.GetSession()
	if s == nil {
		return false
	}
	pack := &server.WRInviteRobot{
		RoomId:  proto.Int(roomId),
		MatchId: proto.Int32(gameFreeId),
		Cnt:     proto.Int(cnt),
	}
	return s.Send(int(server.SSPacketID_PACKET_WR_INVITEROBOT), pack)
}

// MatchInvite 比赛场邀请机器人
func (nsa *NpcServerAgent) MatchInvite(roomId, matchId int32, platform string, cnt int32, needAwait bool) bool {
	//logger.Logger.Trace("(nsa *NpcServerAgent) MatchInvite", roomId, matchId, platform, cnt, needAwait)
	s := nsa.GetSession()
	if s == nil {
		return false
	}
	pack := &server.WRInviteRobot{
		RoomId:    proto.Int32(roomId),
		MatchId:   proto.Int32(matchId),
		Cnt:       proto.Int32(cnt),
		Platform:  proto.String(platform),
		IsMatch:   proto.Bool(true),
		NeedAwait: proto.Bool(needAwait),
	}
	return s.Send(int(server.SSPacketID_PACKET_WR_INVITEROBOT), pack)
}
