package grpc

import (
	"fmt"
	"net"

	paymentpb "git.gocasts.ir/ebhomengo/niki/paymentapp/protobuf"
	"git.gocasts.ir/ebhomengo/niki/pkg/grpc"
)

type PaymentGrpcServer struct {
	server  *grpc.RPCServer
	handler *Handler
}

func NewPaymentGrpcServer(server *grpc.RPCServer, handler *Handler) PaymentGrpcServer {
	return PaymentGrpcServer{
		server:  server,
		handler: handler,
	}
}

func (s PaymentGrpcServer) Serve() error {
	listener, err := net.Listen(s.server.Config.NetworkType, fmt.Sprintf(":%d", s.server.Config.Port))
	if err != nil {
		return err
	}
	paymentpb.RegisterPaymentServiceServer(s.server.Server, s.handler)
	if err := s.server.Server.Serve(listener); err != nil {
		return err
	}
	return nil
}

func (s PaymentGrpcServer) Stop() {
	s.server.Stop()
}
