package internal

import (
	"reflect"
	"server/events"
	"server/msg"

	"github.com/name5566/leaf/gate"
	"github.com/name5566/leaf/log"
)

func init() {
	Handler(&msg.ResPlayerOptAction{}, handlePlayerOptAction)
	Handler(&msg.ResHeartBeat{}, handleHeartBeat)
}

func Handler(m interface{}, h interface{}) {
	skeleton.RegisterChanRPC(reflect.TypeOf(m), h)
}

func handlePlayerOptAction(args []interface{}) {
	m := args[0].(*msg.ResPlayerOptAction)
	a := args[1].(gate.Agent)
	switch m.GameId {
	case "teen_patti":
		events.EventChan <- events.Event{
			Type:  events.EventTeenPattiPlayerOptAction,
			Data:  m,
			Agent: a,
		}
	}
}

func handleHeartBeat(args []interface{}) {
	m := args[0].(*msg.ResHeartBeat)
	a := args[1].(gate.Agent)
	log.Debug("handleHeartBeat: %s", m.Msg)
	a.WriteMsg(&msg.ReqHeartBeat{Msg: "pong"})
}
