// Code generated by Kitex v0.15.1. DO NOT EDIT.

package echo

import (
	"context"
	"errors"
	"fmt"
	pbapi "github.com/cloudwego/kitex-examples/streaming/kitex_gen/pbapi"
	client "github.com/cloudwego/kitex/client"
	kitex "github.com/cloudwego/kitex/pkg/serviceinfo"
	streaming "github.com/cloudwego/kitex/pkg/streaming"
	proto "github.com/cloudwego/prutal"
)

var errInvalidMessageType = errors.New("invalid message type for service method handler")

var serviceMethods = map[string]kitex.MethodInfo{
	"StreamRequestEcho": kitex.NewMethodInfo(
		streamRequestEchoHandler,
		newStreamRequestEchoArgs,
		newStreamRequestEchoResult,
		false,
		kitex.WithStreamingMode(kitex.StreamingClient),
	),
	"StreamResponseEcho": kitex.NewMethodInfo(
		streamResponseEchoHandler,
		newStreamResponseEchoArgs,
		newStreamResponseEchoResult,
		false,
		kitex.WithStreamingMode(kitex.StreamingServer),
	),
	"BidirectionalEcho": kitex.NewMethodInfo(
		bidirectionalEchoHandler,
		newBidirectionalEchoArgs,
		newBidirectionalEchoResult,
		false,
		kitex.WithStreamingMode(kitex.StreamingBidirectional),
	),
}

var (
	echoServiceInfo                = NewServiceInfo()
	echoServiceInfoForClient       = NewServiceInfoForClient()
	echoServiceInfoForStreamClient = NewServiceInfoForStreamClient()
)

// for server
func serviceInfo() *kitex.ServiceInfo {
	return echoServiceInfo
}

// for stream client
func serviceInfoForStreamClient() *kitex.ServiceInfo {
	return echoServiceInfoForStreamClient
}

// for client
func serviceInfoForClient() *kitex.ServiceInfo {
	return echoServiceInfoForClient
}

// NewServiceInfo creates a new ServiceInfo containing all methods
func NewServiceInfo() *kitex.ServiceInfo {
	return newServiceInfo(true, true, true)
}

// NewServiceInfo creates a new ServiceInfo containing non-streaming methods
func NewServiceInfoForClient() *kitex.ServiceInfo {
	return newServiceInfo(false, false, true)
}
func NewServiceInfoForStreamClient() *kitex.ServiceInfo {
	return newServiceInfo(true, true, false)
}

func newServiceInfo(hasStreaming bool, keepStreamingMethods bool, keepNonStreamingMethods bool) *kitex.ServiceInfo {
	serviceName := "Echo"
	handlerType := (*pbapi.Echo)(nil)
	methods := map[string]kitex.MethodInfo{}
	for name, m := range serviceMethods {
		if m.IsStreaming() && !keepStreamingMethods {
			continue
		}
		if !m.IsStreaming() && !keepNonStreamingMethods {
			continue
		}
		methods[name] = m
	}
	extra := map[string]interface{}{
		"PackageName": "pbapi",
	}
	if hasStreaming {
		extra["streaming"] = hasStreaming
	}
	svcInfo := &kitex.ServiceInfo{
		ServiceName:     serviceName,
		HandlerType:     handlerType,
		Methods:         methods,
		PayloadCodec:    kitex.Protobuf,
		KiteXGenVersion: "v0.15.1",
		Extra:           extra,
	}
	return svcInfo
}

func streamRequestEchoHandler(ctx context.Context, handler interface{}, arg, result interface{}) error {
	streamingArgs, ok := arg.(*streaming.Args)
	if !ok {
		return errInvalidMessageType
	}
	st := streamingArgs.Stream
	stream := &echoStreamRequestEchoServer{st}
	return handler.(pbapi.Echo).StreamRequestEcho(stream)
}

type echoStreamRequestEchoClient struct {
	streaming.Stream
}

func (x *echoStreamRequestEchoClient) DoFinish(err error) {
	if finisher, ok := x.Stream.(streaming.WithDoFinish); ok {
		finisher.DoFinish(err)
	} else {
		panic(fmt.Sprintf("streaming.WithDoFinish is not implemented by %T", x.Stream))
	}
}
func (x *echoStreamRequestEchoClient) Send(m *pbapi.Request) error {
	return x.Stream.SendMsg(m)
}
func (x *echoStreamRequestEchoClient) CloseAndRecv() (*pbapi.Response, error) {
	if err := x.Stream.Close(); err != nil {
		return nil, err
	}
	m := new(pbapi.Response)
	return m, x.Stream.RecvMsg(m)
}

type echoStreamRequestEchoServer struct {
	streaming.Stream
}

func (x *echoStreamRequestEchoServer) SendAndClose(m *pbapi.Response) error {
	return x.Stream.SendMsg(m)
}

func (x *echoStreamRequestEchoServer) Recv() (*pbapi.Request, error) {
	m := new(pbapi.Request)
	return m, x.Stream.RecvMsg(m)
}

func newStreamRequestEchoArgs() interface{} {
	return &StreamRequestEchoArgs{}
}

func newStreamRequestEchoResult() interface{} {
	return &StreamRequestEchoResult{}
}

type StreamRequestEchoArgs struct {
	Req *pbapi.Request
}

func (p *StreamRequestEchoArgs) Marshal(out []byte) ([]byte, error) {
	if !p.IsSetReq() {
		return out, nil
	}
	return proto.Marshal(p.Req)
}

func (p *StreamRequestEchoArgs) Unmarshal(in []byte) error {
	msg := new(pbapi.Request)
	if err := proto.Unmarshal(in, msg); err != nil {
		return err
	}
	p.Req = msg
	return nil
}

var StreamRequestEchoArgs_Req_DEFAULT *pbapi.Request

func (p *StreamRequestEchoArgs) GetReq() *pbapi.Request {
	if !p.IsSetReq() {
		return StreamRequestEchoArgs_Req_DEFAULT
	}
	return p.Req
}

func (p *StreamRequestEchoArgs) IsSetReq() bool {
	return p.Req != nil
}

func (p *StreamRequestEchoArgs) GetFirstArgument() interface{} {
	return p.Req
}

type StreamRequestEchoResult struct {
	Success *pbapi.Response
}

var StreamRequestEchoResult_Success_DEFAULT *pbapi.Response

func (p *StreamRequestEchoResult) Marshal(out []byte) ([]byte, error) {
	if !p.IsSetSuccess() {
		return out, nil
	}
	return proto.Marshal(p.Success)
}

func (p *StreamRequestEchoResult) Unmarshal(in []byte) error {
	msg := new(pbapi.Response)
	if err := proto.Unmarshal(in, msg); err != nil {
		return err
	}
	p.Success = msg
	return nil
}

func (p *StreamRequestEchoResult) GetSuccess() *pbapi.Response {
	if !p.IsSetSuccess() {
		return StreamRequestEchoResult_Success_DEFAULT
	}
	return p.Success
}

func (p *StreamRequestEchoResult) SetSuccess(x interface{}) {
	p.Success = x.(*pbapi.Response)
}

func (p *StreamRequestEchoResult) IsSetSuccess() bool {
	return p.Success != nil
}

func (p *StreamRequestEchoResult) GetResult() interface{} {
	return p.Success
}

func streamResponseEchoHandler(ctx context.Context, handler interface{}, arg, result interface{}) error {
	streamingArgs, ok := arg.(*streaming.Args)
	if !ok {
		return errInvalidMessageType
	}
	st := streamingArgs.Stream
	stream := &echoStreamResponseEchoServer{st}
	req := new(pbapi.Request)
	if err := st.RecvMsg(req); err != nil {
		return err
	}
	return handler.(pbapi.Echo).StreamResponseEcho(req, stream)
}

type echoStreamResponseEchoClient struct {
	streaming.Stream
}

func (x *echoStreamResponseEchoClient) DoFinish(err error) {
	if finisher, ok := x.Stream.(streaming.WithDoFinish); ok {
		finisher.DoFinish(err)
	} else {
		panic(fmt.Sprintf("streaming.WithDoFinish is not implemented by %T", x.Stream))
	}
}
func (x *echoStreamResponseEchoClient) Recv() (*pbapi.Response, error) {
	m := new(pbapi.Response)
	return m, x.Stream.RecvMsg(m)
}

type echoStreamResponseEchoServer struct {
	streaming.Stream
}

func (x *echoStreamResponseEchoServer) Send(m *pbapi.Response) error {
	return x.Stream.SendMsg(m)
}

func newStreamResponseEchoArgs() interface{} {
	return &StreamResponseEchoArgs{}
}

func newStreamResponseEchoResult() interface{} {
	return &StreamResponseEchoResult{}
}

type StreamResponseEchoArgs struct {
	Req *pbapi.Request
}

func (p *StreamResponseEchoArgs) Marshal(out []byte) ([]byte, error) {
	if !p.IsSetReq() {
		return out, nil
	}
	return proto.Marshal(p.Req)
}

func (p *StreamResponseEchoArgs) Unmarshal(in []byte) error {
	msg := new(pbapi.Request)
	if err := proto.Unmarshal(in, msg); err != nil {
		return err
	}
	p.Req = msg
	return nil
}

var StreamResponseEchoArgs_Req_DEFAULT *pbapi.Request

func (p *StreamResponseEchoArgs) GetReq() *pbapi.Request {
	if !p.IsSetReq() {
		return StreamResponseEchoArgs_Req_DEFAULT
	}
	return p.Req
}

func (p *StreamResponseEchoArgs) IsSetReq() bool {
	return p.Req != nil
}

func (p *StreamResponseEchoArgs) GetFirstArgument() interface{} {
	return p.Req
}

type StreamResponseEchoResult struct {
	Success *pbapi.Response
}

var StreamResponseEchoResult_Success_DEFAULT *pbapi.Response

func (p *StreamResponseEchoResult) Marshal(out []byte) ([]byte, error) {
	if !p.IsSetSuccess() {
		return out, nil
	}
	return proto.Marshal(p.Success)
}

func (p *StreamResponseEchoResult) Unmarshal(in []byte) error {
	msg := new(pbapi.Response)
	if err := proto.Unmarshal(in, msg); err != nil {
		return err
	}
	p.Success = msg
	return nil
}

func (p *StreamResponseEchoResult) GetSuccess() *pbapi.Response {
	if !p.IsSetSuccess() {
		return StreamResponseEchoResult_Success_DEFAULT
	}
	return p.Success
}

func (p *StreamResponseEchoResult) SetSuccess(x interface{}) {
	p.Success = x.(*pbapi.Response)
}

func (p *StreamResponseEchoResult) IsSetSuccess() bool {
	return p.Success != nil
}

func (p *StreamResponseEchoResult) GetResult() interface{} {
	return p.Success
}

func bidirectionalEchoHandler(ctx context.Context, handler interface{}, arg, result interface{}) error {
	streamingArgs, ok := arg.(*streaming.Args)
	if !ok {
		return errInvalidMessageType
	}
	st := streamingArgs.Stream
	stream := &echoBidirectionalEchoServer{st}
	return handler.(pbapi.Echo).BidirectionalEcho(stream)
}

type echoBidirectionalEchoClient struct {
	streaming.Stream
}

func (x *echoBidirectionalEchoClient) DoFinish(err error) {
	if finisher, ok := x.Stream.(streaming.WithDoFinish); ok {
		finisher.DoFinish(err)
	} else {
		panic(fmt.Sprintf("streaming.WithDoFinish is not implemented by %T", x.Stream))
	}
}
func (x *echoBidirectionalEchoClient) Send(m *pbapi.Request) error {
	return x.Stream.SendMsg(m)
}
func (x *echoBidirectionalEchoClient) Recv() (*pbapi.Response, error) {
	m := new(pbapi.Response)
	return m, x.Stream.RecvMsg(m)
}

type echoBidirectionalEchoServer struct {
	streaming.Stream
}

func (x *echoBidirectionalEchoServer) Send(m *pbapi.Response) error {
	return x.Stream.SendMsg(m)
}

func (x *echoBidirectionalEchoServer) Recv() (*pbapi.Request, error) {
	m := new(pbapi.Request)
	return m, x.Stream.RecvMsg(m)
}

func newBidirectionalEchoArgs() interface{} {
	return &BidirectionalEchoArgs{}
}

func newBidirectionalEchoResult() interface{} {
	return &BidirectionalEchoResult{}
}

type BidirectionalEchoArgs struct {
	Req *pbapi.Request
}

func (p *BidirectionalEchoArgs) Marshal(out []byte) ([]byte, error) {
	if !p.IsSetReq() {
		return out, nil
	}
	return proto.Marshal(p.Req)
}

func (p *BidirectionalEchoArgs) Unmarshal(in []byte) error {
	msg := new(pbapi.Request)
	if err := proto.Unmarshal(in, msg); err != nil {
		return err
	}
	p.Req = msg
	return nil
}

var BidirectionalEchoArgs_Req_DEFAULT *pbapi.Request

func (p *BidirectionalEchoArgs) GetReq() *pbapi.Request {
	if !p.IsSetReq() {
		return BidirectionalEchoArgs_Req_DEFAULT
	}
	return p.Req
}

func (p *BidirectionalEchoArgs) IsSetReq() bool {
	return p.Req != nil
}

func (p *BidirectionalEchoArgs) GetFirstArgument() interface{} {
	return p.Req
}

type BidirectionalEchoResult struct {
	Success *pbapi.Response
}

var BidirectionalEchoResult_Success_DEFAULT *pbapi.Response

func (p *BidirectionalEchoResult) Marshal(out []byte) ([]byte, error) {
	if !p.IsSetSuccess() {
		return out, nil
	}
	return proto.Marshal(p.Success)
}

func (p *BidirectionalEchoResult) Unmarshal(in []byte) error {
	msg := new(pbapi.Response)
	if err := proto.Unmarshal(in, msg); err != nil {
		return err
	}
	p.Success = msg
	return nil
}

func (p *BidirectionalEchoResult) GetSuccess() *pbapi.Response {
	if !p.IsSetSuccess() {
		return BidirectionalEchoResult_Success_DEFAULT
	}
	return p.Success
}

func (p *BidirectionalEchoResult) SetSuccess(x interface{}) {
	p.Success = x.(*pbapi.Response)
}

func (p *BidirectionalEchoResult) IsSetSuccess() bool {
	return p.Success != nil
}

func (p *BidirectionalEchoResult) GetResult() interface{} {
	return p.Success
}

type kClient struct {
	c client.Client
}

func newServiceClient(c client.Client) *kClient {
	return &kClient{
		c: c,
	}
}

func (p *kClient) StreamRequestEcho(ctx context.Context) (Echo_StreamRequestEchoClient, error) {
	streamClient, ok := p.c.(client.Streaming)
	if !ok {
		return nil, fmt.Errorf("client not support streaming")
	}
	res := new(streaming.Result)
	err := streamClient.Stream(ctx, "StreamRequestEcho", nil, res)
	if err != nil {
		return nil, err
	}
	stream := &echoStreamRequestEchoClient{res.Stream}
	return stream, nil
}

func (p *kClient) StreamResponseEcho(ctx context.Context, req *pbapi.Request) (Echo_StreamResponseEchoClient, error) {
	streamClient, ok := p.c.(client.Streaming)
	if !ok {
		return nil, fmt.Errorf("client not support streaming")
	}
	res := new(streaming.Result)
	err := streamClient.Stream(ctx, "StreamResponseEcho", nil, res)
	if err != nil {
		return nil, err
	}
	stream := &echoStreamResponseEchoClient{res.Stream}

	if err := stream.Stream.SendMsg(req); err != nil {
		return nil, err
	}
	if err := stream.Stream.Close(); err != nil {
		return nil, err
	}
	return stream, nil
}

func (p *kClient) BidirectionalEcho(ctx context.Context) (Echo_BidirectionalEchoClient, error) {
	streamClient, ok := p.c.(client.Streaming)
	if !ok {
		return nil, fmt.Errorf("client not support streaming")
	}
	res := new(streaming.Result)
	err := streamClient.Stream(ctx, "BidirectionalEcho", nil, res)
	if err != nil {
		return nil, err
	}
	stream := &echoBidirectionalEchoClient{res.Stream}
	return stream, nil
}
