package main

import (
	"../log"
	"../nnet"
	"../nt"
	pb "../protos"
	"github.com/golang/protobuf/proto"
	"go.uber.org/zap"
)

type CallbackProto func(nnet.ISession, *pb.Request)

var (
	MCbProto = map[string]CallbackProto{
		"C_Heartbeat":          do_C_Heartbeat,
		"C_Login":              do_C_Login,
		"C_ChildrenItemsOf":    do_C_ChildrenItemsOf,
		"C_EnterGame":          do_C_EnterGame,
		"C_LeaveGame":          do_C_LeaveGame,
		"C_UserSettings":       do_C_UserSettings,
		"C_UpdatePassword":     do_C_UpdatePassword,
		"C_UpdateBankBalance":  do_C_UpdateBankBalance,
		"C_UpdateBankPassword": do_C_UpdateBankPassword,
		"C_UserBalance":        do_C_UserBalance,
		"C_VerifiCode":         do_C_VerifiCode,
		"C_BindPhone":          do_C_BindPhone,
		"C_BindAlipay":         do_C_BindAlipay,
		"C_PrivateMail":        do_C_PrivateMail,
		"C_RemPrivateMail":     do_C_RemPrivateMail,
		"C_Activity":           do_C_Activity,
		"C_VIPInfo":            do_C_VIPInfo,
		"C_GetVipBonus":        do_C_GetVipBonus,
		"C_Notice":             do_C_Notice,
		"C_QueryMessage":       do_C_QueryMessage,
		"C_Config":             do_C_Config,
		"C_Checkin7":           do_C_Checkin7,
		"C_CheckinGet":         do_C_CheckinGet,
		"C_GameTask":           do_C_GetGameTask,
		"C_ReceiveReward":      do_C_ReceiveTaskReward,
		"C_Alms":               do_C_Alms,
		"C_AlmsGet":            do_C_AlmsGet,
		"C_Unread":				do_C_Unread,
		"C_Marquee":			do_C_Marquee,
		"C_Checkin30Status":	do_C_Checkin30Status,
		"C_GetChechin30":		do_C_GetChechin30,
		"C_GetCheckin30Ext": 	do_C_GetCheckin30Ext,
		"C_UpdatePasswordV": 	do_C_UpdatePasswordV,
		"C_TurnTableStatus": 	do_C_TurnTableStatus,
		"C_TurnTablePlay": 		do_C_TurnTablePlay,
		"C_TurnTableGet": 		do_C_TurnTableGet,
		"C_RedEnvelope":		do_C_RedEnvelope,
	}
)

func dispatch(ses nnet.ISession, pkt *KgPacket) bool {
	// 解析 Request
	var req = &pb.Request{}

	err := proto.Unmarshal(pkt.msg, req)
	if err != nil {
		log.Error("dispatch", zap.Error(err))
		return false
	}
	if req.MsgName != "C_Heartbeat" {
		log.Debug("dispatch", zap.Uint64("guid", req.Guid), zap.Uint32("msgId", req.MsgId),
			zap.String("msgName", req.MsgName), zap.Uint64("sesId", ses.Id()))
	}

	//
	if !nt.DynConf.ValidGuid(req.Guid) {
		log.Error("dispatch", zap.Uint64("guid", req.Guid))
		return false
	}
	if req.MsgName != "C_Login" && !nt.DynConf.ValidGuid(ses.Id()) {
		log.Warn("no login", zap.Uint64("guid", req.Guid), zap.Uint64("sesId", ses.Id()))
		KillUserSession(req.Guid, ses, pb.ReasonDisconn_ERR_DATA)
		return false
	}
	if _func_, ok := MCbProto[req.MsgName]; ok {
		_func_(ses, req)
	} else {
		do_default(ses, req)
	}
	return true
}
