package gm_rpc

import (
	"leafstalk/covenant/msg"
	"leafstalk/covenant/pbfmsg"
	"leafstalk/network/cluster"
	"leafstalk/router/json"
	"leafstalk/router/protobuf"

	"github.com/sirupsen/logrus"
)

var (
	//MsgRouter 消息序列化及反序列化
	MsgRouter = json.NewProcessor()
	PbfRouter = protobuf.NewProcessor()
)

// 注册系统中有哪些需要处理的消息
func init() {
	//MsgRouter.Register(&msg.ServerLogin{})
	//MsgRouter.Register(&msg.ResponseServerLogin{})
	// MsgRouter.Register(&msg.RouteMsg{})

	MsgRouter.Register(&msg.UpdateClientData{})
	MsgRouter.Register(&msg.ResponseUpdateClientData{})
	MsgRouter.Register(&msg.ClientData{})
	MsgRouter.Register(&msg.ResponseClientData{})

	MsgRouter.Register(&msg.UpdateChapter{})
	MsgRouter.Register(&msg.RequestChapter{})
	MsgRouter.Register(&msg.ResponseUpdateChapter{})
	MsgRouter.Register(&msg.ResponseRequestChapter{})

	MsgRouter.Register(&msg.UpdateSummary{})
	MsgRouter.Register(&msg.RequestSummary{})
	MsgRouter.Register(&msg.ResponseUpdateSummary{})
	MsgRouter.Register(&msg.ResponseRequestSummary{})

	MsgRouter.Register(&msg.GmPlayer{})
	MsgRouter.Register(&msg.ResponseGmPlayer{})
	MsgRouter.Register(&msg.GmAddExp{})
	MsgRouter.Register(&msg.ResponseGmAddExp{})
	MsgRouter.Register(&msg.GmAddStama{})
	MsgRouter.Register(&msg.ResponseGmAddStama{})
	MsgRouter.Register(&msg.GmUpdateRole{})
	MsgRouter.Register(&msg.ResponseGmUpdateRole{})
	MsgRouter.Register(&msg.GmAddMaterial{})
	MsgRouter.Register(&msg.ResponseGmAddMaterial{})
	MsgRouter.Register(&msg.GmUpdateChapter{})
	MsgRouter.Register(&msg.ResponseGmUpdateChapter{})
	MsgRouter.Register(&msg.GmUpdateTalent{})
	MsgRouter.Register(&msg.ResponseGmUpdateTalent{})
	MsgRouter.Register(&msg.GmPlacard{})
	MsgRouter.Register(&msg.ResponseGmPlacard{})
	MsgRouter.Register(&msg.GmUpdateElRank{})
	MsgRouter.Register(&msg.ResponseGmUpdateElRank{})

	MsgRouter.Register(&msg.GmQueryModelStart{})
	MsgRouter.Register(&msg.ResponseGmQueryModelStart{})
	MsgRouter.Register(&msg.GmDoubleAd{})
	MsgRouter.Register(&msg.ResponseGmDoubleAd{})
	MsgRouter.Register(&msg.GmIosPay{})
	MsgRouter.Register(&msg.ResponseGmIosPay{})

	MsgRouter.Register(&msg.GmQueryEndlessRank{})
	MsgRouter.Register(&msg.ResponseGmQueryEndlessRank{})

	MsgRouter.Register(&msg.GMLetter{})
	MsgRouter.Register(&msg.ResponseGMLetter{})
	MsgRouter.Register(&msg.GmGetGuide{})
	MsgRouter.Register(&msg.ResponseGmGetGuide{})
	MsgRouter.Register(&msg.GmSetGuide{})
	MsgRouter.Register(&msg.ResponseGmSetGuide{})
	MsgRouter.Register(&msg.GmDelRank{})
	MsgRouter.Register(&msg.ResponseGmDelRank{})

	MsgRouter.Register(&msg.RedeemGoodsIs{})
	MsgRouter.Register(&msg.ResponseRedeemGoodsIs{})

	PbfRouter.Register(&pbfmsg.ServerLogin{})
	PbfRouter.Register(&pbfmsg.ResponseServerLogin{})
	PbfRouter.Register(&pbfmsg.RouteMsg{})
	PbfRouter.Register(&pbfmsg.ResponseRouteMsg{})
	PbfRouter.Register(&pbfmsg.NewClientToken{}) //

	PbfRouter.SetHandler(&pbfmsg.RouteMsg{}, handleRouteMsg)
}

// handleRouteToPlayer2
func handleRouteMsg(args []interface{}) {
	//获取世界服务器，发送过去
	request := args[0].(*pbfmsg.RouteMsg)
	a := args[1].(*cluster.ServerAgent)
	var msg interface{}
	var err error
	if len(request.MsgId) > 0 {
		msg, err = MsgRouter.Unmarshal3(request.MsgId, request.Data)
	} else {
		msg, err = MsgRouter.Unmarshal(request.Data)
	}
	if err != nil {
		logrus.Warnf("unmarshal message error: %v", err)
		return
	} else {
		logrus.WithField("from", "handleRouteMsg").Infoln(msg)
	}
	err = MsgRouter.Route(msg, a)
	if err != nil {
		logrus.Warnf("route message error: %v", err)
		return
	}
}
