package base

import (
	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/netlib"

	gamehallproto "mongo.games.com/game/protocol/gamehall"
)

func init() {
	//SCEnterRoom
	netlib.Register(int(gamehallproto.GameHallPacketID_PACKET_SC_ENTERROOM), gamehallproto.SCEnterRoom{}, SCEnterRoom)
	//SCDestroyRoom
	netlib.Register(int(gamehallproto.GameHallPacketID_PACKET_SC_DESTROYROOM), gamehallproto.SCDestroyRoom{}, SCDestroyRoom)
	//SCLeaveRoom
	netlib.Register(int(gamehallproto.GameHallPacketID_PACKET_SC_LEAVEROOM), gamehallproto.SCLeaveRoom{}, SCLeaveRoom)
	//SCReturnRoom
	netlib.Register(int(gamehallproto.GameHallPacketID_PACKET_SC_RETURNROOM), gamehallproto.SCReturnRoom{}, SCReturnRoom)
	//SCQuitGame
	netlib.Register(int(gamehallproto.GameHallPacketID_PACKET_SC_QUITGAME), gamehallproto.SCQuitGame{}, SCQuitGame)
}

func SCEnterRoom(s *netlib.Session, packid int, pack interface{}) error {
	logger.Logger.Trace("SCEnterRoom ", pack)
	msg, ok := pack.(*gamehallproto.SCEnterRoom)
	if !ok {
		return nil
	}

	if msg.GetOpRetCode() == gamehallproto.OpResultCode_Game_OPRC_Sucess_Game {
		s.SetAttribute(SessionAttributeSceneId, msg.GetRoomId())
	}
	s.RemoveAttribute(SessionAttributeEnteringScene)
	s.RemoveAttribute(SessionAttributeEnteringMatchScene)
	return nil
}

func SCDestroyRoom(s *netlib.Session, packid int, pack interface{}) error {
	logger.Logger.Trace("SCDestroyRoom ", pack)

	msg, ok := pack.(*gamehallproto.SCDestroyRoom)
	if !ok {
		return nil
	}

	if msg.GetOpRetCode() == gamehallproto.OpResultCode_Game_OPRC_Sucess_Game {
		s.RemoveAttribute(SessionAttributeScene)
		s.RemoveAttribute(SessionAttributeSceneId)
		s.RemoveAttribute(SessionAttributeEnteringScene)
		s.RemoveAttribute(SessionAttributeEnteringMatchScene)
		return nil
	}

	logger.Logger.Error("SCDestroyRoom failed")

	return nil
}

func SCLeaveRoom(s *netlib.Session, packid int, pack interface{}) error {
	logger.Logger.Trace("SCLeaveRoom ", pack)

	msg, ok := pack.(*gamehallproto.SCLeaveRoom)
	if !ok {
		return nil
	}

	if msg.GetOpRetCode() == gamehallproto.OpResultCode_Game_OPRC_Sucess_Game {
		if scene, ok := GetScene(s).(IScene); ok && scene != nil {
			p := scene.GetMe(s)
			if p != nil {
				logger.Logger.Trace("(this *SCLeaveRoomHandler) snid [%v].", p.GetSnId())
				scene.DelPlayer(p.GetSnId())
			}
		}
		s.RemoveAttribute(SessionAttributeScene)
		s.RemoveAttribute(SessionAttributeSceneId)
		s.RemoveAttribute(SessionAttributeEnteringScene)
		s.RemoveAttribute(SessionAttributeEnteringMatchScene)
		return nil
	}

	logger.Logger.Error("SCLeaveRoom failed")

	return nil
}

func SCReturnRoom(s *netlib.Session, packid int, pack interface{}) error {
	logger.Logger.Trace("SCReturnRoom ", pack)

	msg, ok := pack.(*gamehallproto.SCReturnRoom)
	if !ok {
		return nil
	}

	if msg.GetOpRetCode() == gamehallproto.OpResultCode_Game_OPRC_Sucess_Game {
		logger.Logger.Info("SCReturnRoom success")
		return nil
	}

	logger.Logger.Error("SCReturnRoom failed")

	return nil
}

func SCQuitGame(s *netlib.Session, packid int, pack interface{}) error {
	logger.Logger.Trace("SCQuitGame ", pack)

	msg, ok := pack.(*gamehallproto.SCQuitGame)
	if !ok {
		return nil
	}

	if msg.GetOpCode() == gamehallproto.OpResultCode_Game_OPRC_Sucess_Game {
		if scene, ok := GetScene(s).(IScene); ok && scene != nil {
			p := scene.GetMe(s)
			if p != nil {
				logger.Logger.Trace("(this *SCQuitGameHandler) snid [%v].", p.GetSnId())
				scene.DelPlayer(p.GetSnId())
			}
		}
		s.RemoveAttribute(SessionAttributeScene)
		s.RemoveAttribute(SessionAttributeSceneId)
		s.RemoveAttribute(SessionAttributeEnteringScene)
		s.RemoveAttribute(SessionAttributeEnteringMatchScene)
		return nil
	}

	logger.Logger.Error("SCQuitGame failed")

	return nil
}
