package chesstitians

import (
	"mongo.games.com/game/common"
	"mongo.games.com/game/gamesrv/base"
	"mongo.games.com/game/protocol/chesstitians"
	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/netlib"
)

type CSPlayerOpPacketFactory struct {
}
type CSPlayerOpHandler struct {
}

func (f *CSPlayerOpPacketFactory) CreatePacket() interface{} {
	pack := &chesstitians.CSChesstitiansPlayerOp{}
	return pack
}
func (h *CSPlayerOpHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSPlayerOpHandler Process recv ", data)
	if msg, ok := data.(*chesstitians.CSChesstitiansPlayerOp); ok {
		p := base.PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warn("CSPlayerOpHandler p == nil")
			return nil
		}
		scene := p.GetScene()
		if scene == nil {
			logger.Logger.Warn("CSPlayerOpHandler p.scene == nil")
			return nil
		}
		if !common.IsChess(scene.GetGameId()) {
			logger.Logger.Error("CSPlayerOpHandler gameId Error ", scene.GameId)
			return nil
		}
		if !scene.HasPlayer(p) {
			return nil
		}
		sp := scene.GetScenePolicy()
		if sp != nil {
			sp.OnPlayerOp(scene, p, int(msg.GetOpCode()), msg.GetOpParam())
		}
		return nil
	}
	return nil
}

// dev

type CSPlayerDevOpPacketFactory struct {
}
type CSPlayerDevOpHandler struct {
}

func (f *CSPlayerDevOpPacketFactory) CreatePacket() interface{} {
	pack := &chesstitians.CSChesstitiansPlayerDevOp{}
	return pack
}

func (h *CSPlayerDevOpHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSPlayerDevOpHandler Process recv ", data)
	if msg, ok := data.(*chesstitians.CSChesstitiansPlayerDevOp); ok {
		p := base.PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warn("CSPlayerDevOpHandler p == nil")
			return nil
		}
		scene := p.GetScene()
		sp := scene.GetScenePolicy()
		if scene == nil {
			logger.Logger.Warn("CSPlayerDevOpHandler p.scene == nil")
			return nil
		}
		if !common.IsChess(scene.GetGameId()) {
			logger.Logger.Error("CSPlayerDevOpHandler gameId Error ", scene.GameId)
			return nil
		}
		if !scene.HasPlayer(p) {
			return nil
		}
		if sp != nil {
			sp.(*ScenePolicyEntity).OnPlayerDevOp(scene, p, int(msg.GetOpCode()), msg.GetOpParamStr(), int64(msg.GetCurOpIdx()))
			//logger.Logger.Info("msg", msg)
		}
		return nil
	}
	return nil
}

func init() {
	common.RegisterHandler(int(chesstitians.ChesstitiansPacketID_PACKET_CSChesstitiansPlayerOp), &CSPlayerOpHandler{})
	netlib.RegisterFactory(int(chesstitians.ChesstitiansPacketID_PACKET_CSChesstitiansPlayerOp), &CSPlayerOpPacketFactory{})
	// dev
	common.RegisterHandler(int(chesstitians.ChesstitiansPacketID_PACKET_CSChesstitiansPlayerDevOp), &CSPlayerDevOpHandler{})
	netlib.RegisterFactory(int(chesstitians.ChesstitiansPacketID_PACKET_CSChesstitiansPlayerDevOp), &CSPlayerDevOpPacketFactory{})

}
