package grave_master

import (
	"errors"
	"gadmin/internal/admin/gm_rpc/rpc_share"
	"leafstalk/conf"
	"leafstalk/covenant/msg"
	"leafstalk/log"
	"leafstalk/module"
	"leafstalk/module/handler"
	"leafstalk/router/json"
	"reflect"

	"github.com/sirupsen/logrus"
)

type Module struct {
	*module.Skeleton
	config    *conf.Config
	Processor *json.Processor
}

var (
	skeleton *module.Skeleton
)

// NewModule 构造
func NewModule(processor *json.Processor, config *conf.Config) *Module {
	mod := new(Module)
	mod.config = config

	//构造一个数据处理模块
	mod.Skeleton = module.NewSkeleton(config)
	mod.Processor = processor
	skeleton = mod.Skeleton
	return mod
}

// GetHandlerServer 获取
func (m *Module) GetHandlerServer() *handler.Server {
	return m.HandlerServer
}

// OnInit 1
func (m *Module) OnInit() {
	m.handleMsg(&msg.ResponseGmPlayer{}, handleGmPlayer)
	m.handleMsg(&msg.ResponseGmAddExp{}, handleGmAddExpResp)
	m.handleMsg(&msg.ResponseGmAddStama{}, handleGmAddStaminaResp)
	m.handleMsg(&msg.ResponseGmUpdateRole{}, handleGmUpdateRoleResp)
	m.handleMsg(&msg.ResponseGmAddMaterial{}, handleGmAddMaterialResp)
	m.handleMsg(&msg.ResponseGmUpdateChapter{}, handleGmUpdateChapterResp)
	m.handleMsg(&msg.ResponseGmUpdateTalent{}, handleGmUpdateTalentResp)
	m.handleMsg(&msg.ResponseGmPlacard{}, handlerGmPlacardResp)
	m.handleMsg(&msg.ResponseGmUpdateElRank{}, handlerGmUpdateElRankResp)
	m.handleMsg(&msg.ResponseGmQueryModelStart{}, handlerGmQueryModelStart)
	m.handleMsg(&msg.ResponseGmGetGuide{}, handlerGmGetGuide)
	m.handleMsg(&msg.ResponseGmSetGuide{}, handlerGmSetGuide)
	m.handleMsg(&msg.ResponseGmDelRank{}, handlerGmDelRank)

	m.handleMsg(&msg.ResponseGmQueryEndlessRank{}, handlerGmQueryEndlessRank)

	m.handleMsg(&msg.ResponseGMLetter{}, handlerGMLetter)

	m.handleMsg(&msg.RedeemGoodsIs{}, handlerRedeemGoodsIs)

	//m.Skeleton.RegisterChanRPC("BuyGoodsStatics", handleBuyGoodsStatics)
	//m.Skeleton.RegisterChanRPC("FreeGoodsStatics", handleFreeGoodsStatics)
	//m.Skeleton.RegisterChanRPC("VideoGiftStatics", handleVideoGiftStatics)
}

// OnDestroy 1
func (m *Module) OnDestroy() {
	//GetLogicApp().Destory()
}

// handleMsg 消息结构体注册处理函数
func (m *Module) handleMsg(msg interface{}, h interface{}) {
	//设置处理函数
	m.RegisterChanRPC(reflect.TypeOf(msg), h)

	//消息路由目标是本模块
	m.Processor.SetRouter(msg, m.HandlerServer)
}

func (m *Module) handleRpcMsg(msg interface{}, h interface{}) error {
	msgType := reflect.TypeOf(msg)
	if msgType == nil || msgType.Kind() != reflect.Ptr {
		return errors.New("json message pointer required")
	}
	msgID := msgType.Elem().Name()
	m.Skeleton.RegisterChanRPC(msgID, h)
	return nil
}

func handleGmPlayer(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmPlayer)
	logrus.Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handleGmAddExpResp(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmAddExp)
	logrus.Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handleGmAddStaminaResp(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmAddStama)
	log.Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handleGmUpdateRoleResp(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmUpdateRole)
	logrus.Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handleGmAddMaterialResp(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmAddMaterial)
	logrus.Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handleGmUpdateTalentResp(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmUpdateTalent)
	logrus.Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handleGmUpdateChapterResp(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmUpdateChapter)
	logrus.Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handlerGmPlacardResp(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmPlacard)
	logrus.Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handlerGmUpdateElRankResp(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmUpdateElRank)
	logrus.WithField("from", "handlerGmUpdateElRankResp").Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handlerGmQueryModelStart(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmQueryModelStart)
	logrus.WithField("from", "GmQueryModelStart").Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handlerGmGetGuide(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmGetGuide)
	logrus.WithField("from", "GmGetGuide").Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handlerGmSetGuide(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmSetGuide)
	logrus.WithField("from", "GmSetGuide").Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handlerGmDelRank(args []interface{}) {
	resp := args[0].(*msg.ResponseGmDelRank)
	logrus.WithField("from", "GmDelRank").Infoln(resp.MsgId)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

// func handlerGmModelStart(args []interface{}) {
// 	//log.Infoln("handleResponseQueryPlayer", args[0])
// 	resp := args[0].(*msg.ResponseGmModelStart)

// 	logrus.WithField("from", "GmModelStart").Infoln(resp)
// 	rpc_share.LogChan <- rpc_share.LogMsg{
// 		MsgID: resp.MsgId,
// 		Data:  resp,
// 	}
// }

func handlerGmQueryEndlessRank(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGmQueryEndlessRank)

	logrus.WithField("from", "GmQueryEndlessRank").Infoln(resp)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handlerGMLetter(args []interface{}) {
	//log.Infoln("handleResponseQueryPlayer", args[0])
	resp := args[0].(*msg.ResponseGMLetter)

	logrus.WithField("from", "handlerGMLetter").Infoln(resp)
	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: resp.MsgId,
		Data:  resp,
	}
}

func handlerRedeemGoodsIs(args []interface{}) {
	//var request = args[0].(*msg.RedeemGoodsIs)
	//logrus.WithField("from", "grave_master handlerRedeemGoodsIs").Infof("request:%+v", request)
	//
	//exchange, err := service.Cdk.Exchange(context.TODO(), request)
	//if err != nil {
	//	logrus.WithField("from", "grave_master handlerRedeemGoodsIs Exchange err ").Warnln(err)
	//}
	//
	//if err = gate.SendToWorld(request.PlayerId, &exchange); err != nil {
	//	logrus.WithField("from", "grave_master handlerRedeemGoodsIs SendToWorld err ").Warnln(err)
	//}
}
