package thirteen

import (
	"mongo.games.com/game/proto"
	"mongo.games.com/game/robot/base"

	"mongo.games.com/game/common"
	rule "mongo.games.com/game/gamerule/thirteen"
	"mongo.games.com/game/protocol/thirteen"

	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/netlib"
)

type SCThirteenWaterRoomInfoPacketFactory struct {
}

type SCThirteenWaterRoomInfoHandler struct {
}

func (this *SCThirteenWaterRoomInfoPacketFactory) CreatePacket() interface{} {
	pack := &thirteen.SCThirteenRoomInfo{}
	return pack
}

func (this *SCThirteenWaterRoomInfoHandler) Process(s *netlib.Session, packid int, pack interface{}) error {
	me := base.GetUser(s)
	logger.Logger.Tracef("(this *SCThirteenWaterRoomInfoHandler) Process [%v].", me.GetData().GetSnId())
	if msg, ok := pack.(*thirteen.SCThirteenRoomInfo); ok {
		scene, ok := base.SceneMgrSingleton.GetScene(msg.GetRoomId()).(*ThirteenWaterScene)
		if scene == nil || ok == false {
			scene = NewThirteenWaterScene(msg)
			base.SceneMgrSingleton.AddScene(scene)
		}
		if scene != nil {
			for _, pd := range msg.GetPlayers() {
				if scene.GetPlayerBySnid(pd.GetSnId()) == nil {
					p := NewThirteenWaterPlayer(pd)
					if p != nil {
						scene.AddPlayer(p)
					}
				}
			}
			switch int(msg.GetState()) {
			case rule.ThirteenWaterSceneStateWait, rule.ThirteenWaterSceneStateStart:
			}
		}

	} else {
		logger.Logger.Error("SCThirteenWaterRoomInfo package data error.")
	}
	return nil
}

type SCThirteenWaterRoomStatePacketFactory struct {
}

type SCThirteenWaterRoomStateHandler struct {
}

func (this *SCThirteenWaterRoomStatePacketFactory) CreatePacket() interface{} {
	pack := &thirteen.SCThirteenRoomState{}
	return pack
}

func (this *SCThirteenWaterRoomStateHandler) Process(s *netlib.Session, packid int, pack interface{}) error {
	me := base.GetUser(s)
	logger.Logger.Tracef("(this *SCThirteenWaterRoomStateHandler) Process [%v].", me.GetData().GetSnId())
	if msg, ok := pack.(*thirteen.SCThirteenRoomState); ok {
		if scene, ok := base.GetScene(s).(*ThirteenWaterScene); ok {

			p := scene.GetMe(s)
			if p == base.NilPlayer {
				return nil
			}
			if me, ok := p.(*ThirteenWaterPlayer); ok && me != nil {
				//logger.Logger.Trace(msg)
				switch int(msg.GetState()) {
				case rule.ThirteenWaterSceneStateOptCard:
					//十三水选牌
					player := p.(*ThirteenWaterPlayer)
					if player == nil {
						return nil
					}
					n := int32(0)
					if len(player.allcardsO) != 0 {
						//rands := rand.Intn(len(player.allcardsO))
						//n = player.allcardsO[rands]
						player.GetMaxCardsO()
						n = player.maxcardsO
					}
					if n == 0 {
						n = -1
					}
					pack := &thirteen.CSThirteenPlayerOp{
						OpCode:  proto.Int32(int32(rule.ThirteenWaterPlayerOpMS)),
						OpParam: []int64{int64(n), -2},
					}
					proto.SetDefaults(pack)
					base.DelaySendSecond(s, int(thirteen.TWMmoPacketID_PACKET_CSThirteenPlayerOp), pack, 5, 10)
				}
			}
		}
	} else {
		logger.Logger.Error("SCThirteenWaterRoomState package data error.")
	}
	return nil
}

type SCThirteenWaterPlayerEnterPacketFactory struct {
}

type SCThirteenWaterPlayerEnterHandler struct {
}

func (this *SCThirteenWaterPlayerEnterPacketFactory) CreatePacket() interface{} {
	pack := &thirteen.SCThirteenPlayerEnter{}
	return pack
}

func (this *SCThirteenWaterPlayerEnterHandler) Process(s *netlib.Session, packid int, pack interface{}) error {
	me := base.GetUser(s)
	logger.Logger.Tracef("(this *SCThirteenWaterPlayerEnterHandler) Process [%v].", me.GetData().GetSnId())
	if msg, ok := pack.(*thirteen.SCThirteenPlayerEnter); ok {
		if scene, ok := base.GetScene(s).(base.IScene); ok && scene != nil {
			//logger.Logger.Trace(msg)
			player := NewThirteenWaterPlayer(msg.GetData())
			if player != nil {
				scene.AddPlayer(player)
			}
		}
	} else {
		logger.Logger.Error("SCThirteenWaterPlayerEnter package data error.")
	}
	return nil
}

type SCThirteenWaterPlayerLeavePacketFactory struct {
}

type SCThirteenWaterPlayerLeaveHandler struct {
}

func (this *SCThirteenWaterPlayerLeavePacketFactory) CreatePacket() interface{} {
	pack := &thirteen.SCThirteenPlayerLeave{}
	return pack
}

func (this *SCThirteenWaterPlayerLeaveHandler) Process(s *netlib.Session, packid int, pack interface{}) error {
	me := base.GetUser(s)
	logger.Logger.Tracef("(this *SCThirteenWaterPlayerLeaveHandler) Process [%v].", me.GetData().GetSnId())
	if msg, ok := pack.(*thirteen.SCThirteenPlayerLeave); ok {
		if scene, ok := base.GetScene(s).(*ThirteenWaterScene); ok && scene != nil {
			//logger.Logger.Trace(msg)
			p := scene.GetPlayerByPos(msg.GetPos())
			if p != nil {
				if player, ok := p.(*ThirteenWaterPlayer); ok && player != nil {
					scene.DelPlayer(player.GetSnId())
				}
			}
		}
	} else {
		logger.Logger.Error("SCThirteenWaterPlayerLeave package data error.")
	}
	return nil
}

type SCThirteenWaterBilledPacketFactory struct {
}

type SCThirteenWaterBilledHandler struct {
}

func (this *SCThirteenWaterBilledPacketFactory) CreatePacket() interface{} {
	pack := &thirteen.SCThirteenBilled{}
	return pack
}

func (this *SCThirteenWaterBilledHandler) Process(s *netlib.Session, packid int, pack interface{}) error {
	me := base.GetUser(s)
	logger.Logger.Tracef("(this *SCThirteenWaterBilledHandler) Process [%v].", me.GetData().GetSnId())
	if msg, ok := pack.(*thirteen.SCThirteenBilled); ok {
		if scene, ok := base.GetScene(s).(*ThirteenWaterScene); ok && scene != nil {
			for _, v := range msg.GetAllBilled() {
				p := scene.GetPlayerByPos(v.GetPos())
				if player, ok := p.(*ThirteenWaterPlayer); ok && player != nil {
					player.Coin = v.GetCoin()
				}
			}
			scene.Clear()
		}
	} else {
		logger.Logger.Error("SCThirteenWaterBilled package data error.")
	}
	return nil
}

type SCThirteenWaterPlayerOpPacketFactory struct {
}

type SCThirteenWaterPlayerOpHandler struct {
}

func (this *SCThirteenWaterPlayerOpPacketFactory) CreatePacket() interface{} {
	pack := &thirteen.SCThirteenPlayerOp{}
	return pack
}

func (this *SCThirteenWaterPlayerOpHandler) Process(s *netlib.Session, packid int, pack interface{}) error {
	me := base.GetUser(s)
	logger.Logger.Tracef("(this *SCThirteenWaterPlayerOpHandler) Process [%v].", me.GetData().GetSnId())
	if msg, ok := pack.(*thirteen.SCThirteenPlayerOp); ok {
		if scene, ok := base.GetScene(s).(*ThirteenWaterScene); ok && scene != nil {
			p := scene.GetMe(s)
			if p == base.NilPlayer {
				return nil
			}
			player := p.(*ThirteenWaterPlayer)
			if me, ok := p.(*ThirteenWaterPlayer); ok && me != nil {
				if me.Pos == msg.Pos {
					if player.cardsO.PokerType == -1 {
						copy(common.CopySliceIntToInt32(player.cardsO.Head[:]), msg.GetCards().GetHead())
						copy(common.CopySliceIntToInt32(player.cardsO.Mid[:]), msg.GetCards().GetMid())
						copy(common.CopySliceIntToInt32(player.cardsO.End[:]), msg.GetCards().GetEnd())
						player.cardsO.PokerType = int(msg.GetCards().GetIndexType())
					}
				}
			}
		}
	} else {
		logger.Logger.Error("SCThirteenWaterPokerType package data error.")
	}
	return nil
}

type SCThirteenWaterPlayerCardsPacketFactory struct {
}

type SCThirteenWaterPlayerCardsHandler struct {
}

func (this *SCThirteenWaterPlayerCardsPacketFactory) CreatePacket() interface{} {
	pack := &thirteen.SCThirteenPlayerCards{}
	return pack
}

func (this *SCThirteenWaterPlayerCardsHandler) Process(s *netlib.Session, packid int, pack interface{}) error {
	me := base.GetUser(s)
	logger.Logger.Tracef("(this *SCThirteenWaterPlayerOpHandler) Process [%v].", me.GetData().GetSnId())
	if msg, ok := pack.(*thirteen.SCThirteenPlayerCards); ok {
		if scene, ok := base.GetScene(s).(*ThirteenWaterScene); ok && scene != nil {
			//logger.Logger.Trace(msg)
			p := scene.GetPlayerByPos(msg.GetPos())
			if p != nil {
				if player, ok := p.(*ThirteenWaterPlayer); ok && player != nil {
					if player.GetSnId() == me.GetData().GetSnId() {
						player.Cards = msg.GetCards()
						for k, v := range msg.GetAllCardsO() {
							player.allcardsO[k] = v.GetIndexType()
						}
					}
				}
			}
		}
	} else {
		logger.Logger.Error("SCThirteenWaterPlayerCards package data error.")
	}
	return nil
}
func init() {
	//SCThirteenWaterRoomInfo
	netlib.RegisterHandler(int(thirteen.TWMmoPacketID_PACKET_SCThirteenRoomInfo), &SCThirteenWaterRoomInfoHandler{})
	netlib.RegisterFactory(int(thirteen.TWMmoPacketID_PACKET_SCThirteenRoomInfo), &SCThirteenWaterRoomInfoPacketFactory{})

	//SCThirteenWaterRoomState
	netlib.RegisterHandler(int(thirteen.TWMmoPacketID_PACKET_SCThirteenRoomState), &SCThirteenWaterRoomStateHandler{})
	netlib.RegisterFactory(int(thirteen.TWMmoPacketID_PACKET_SCThirteenRoomState), &SCThirteenWaterRoomStatePacketFactory{})

	//SCThirteenWaterPlayerEnter
	netlib.RegisterHandler(int(thirteen.TWMmoPacketID_PACKET_SCThirteenPlayerEnter), &SCThirteenWaterPlayerEnterHandler{})
	netlib.RegisterFactory(int(thirteen.TWMmoPacketID_PACKET_SCThirteenPlayerEnter), &SCThirteenWaterPlayerEnterPacketFactory{})

	//SCThirteenWaterPlayerLeave
	netlib.RegisterHandler(int(thirteen.TWMmoPacketID_PACKET_SCThirteenPlayerLeave), &SCThirteenWaterPlayerLeaveHandler{})
	netlib.RegisterFactory(int(thirteen.TWMmoPacketID_PACKET_SCThirteenPlayerLeave), &SCThirteenWaterPlayerLeavePacketFactory{})

	//SCThirteenWaterBilled
	netlib.RegisterHandler(int(thirteen.TWMmoPacketID_PACKET_SCThirteenBilled), &SCThirteenWaterBilledHandler{})
	netlib.RegisterFactory(int(thirteen.TWMmoPacketID_PACKET_SCThirteenBilled), &SCThirteenWaterBilledPacketFactory{})

	//SCThirteenPlayerOp
	netlib.RegisterHandler(int(thirteen.TWMmoPacketID_PACKET_SCThirteenPlayerOp), &SCThirteenWaterPlayerOpHandler{})
	netlib.RegisterFactory(int(thirteen.TWMmoPacketID_PACKET_SCThirteenPlayerOp), &SCThirteenWaterPlayerOpPacketFactory{})

	//SCThirteenWaterPlayerCards
	netlib.RegisterHandler(int(thirteen.TWMmoPacketID_PACKET_SCThirteenPlayerCards), &SCThirteenWaterPlayerCardsHandler{})
	netlib.RegisterFactory(int(thirteen.TWMmoPacketID_PACKET_SCThirteenPlayerCards), &SCThirteenWaterPlayerCardsPacketFactory{})

}
