package svc

import (
	"github.com/zeromicro/go-queue/kq"
	"github.com/zeromicro/go-queue/rabbitmq"
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/zrpc"
	"microGame/app/usercenter/cmd/api/internal/config"
	"microGame/app/usercenter/cmd/api/internal/game"
	"microGame/app/usercenter/cmd/api/internal/middleware"
	"microGame/app/usercenter/cmd/rpc/client/roomservice"
	"microGame/app/usercenter/cmd/rpc/client/userservice"
)

type ServiceContext struct {
	Config              config.Config
	KqPusherClient      *kq.Pusher
	RabbitSender        rabbitmq.Sender
	UserAgentMiddleware rest.Middleware
	UserRpc             userservice.UserService
	RoomRpc             roomservice.RoomService
	Hub                 *game.Hub
}

func NewServiceContext(c config.Config) *ServiceContext {

	hub := game.NewHub()
	go hub.Run()

	return &ServiceContext{
		Config:              c,
		Hub:                 hub,
		KqPusherClient:      kq.NewPusher(c.KqPusherConf.Brokers, c.KqPusherConf.Topic),
		RabbitSender:        rabbitmq.MustNewSender(c.RabbitSenderConf),
		UserAgentMiddleware: middleware.NewUserAgentMiddleware().Handle,
		UserRpc:             userservice.NewUserService(zrpc.MustNewClient(c.UserCenterRpcConf)),
		RoomRpc:             roomservice.NewRoomService(zrpc.MustNewClient(c.UserCenterRpcConf)),
	}
}
