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

package paymentservice

import (
	"context"
	"errors"
	client "github.com/cloudwego/kitex/client"
	kitex "github.com/cloudwego/kitex/pkg/serviceinfo"
	streaming "github.com/cloudwego/kitex/pkg/streaming"
	proto "google.golang.org/protobuf/proto"
	payment "payment/kitex_gen/payment"
)

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

var serviceMethods = map[string]kitex.MethodInfo{
	"Charge": kitex.NewMethodInfo(
		chargeHandler,
		newChargeArgs,
		newChargeResult,
		false,
		kitex.WithStreamingMode(kitex.StreamingUnary),
	),
	"CancelPayment": kitex.NewMethodInfo(
		cancelPaymentHandler,
		newCancelPaymentArgs,
		newCancelPaymentResult,
		false,
		kitex.WithStreamingMode(kitex.StreamingUnary),
	),
	"ChargeByThirdParty": kitex.NewMethodInfo(
		chargeByThirdPartyHandler,
		newChargeByThirdPartyArgs,
		newChargeByThirdPartyResult,
		false,
		kitex.WithStreamingMode(kitex.StreamingUnary),
	),
}

var (
	paymentServiceServiceInfo                = NewServiceInfo()
	paymentServiceServiceInfoForClient       = NewServiceInfoForClient()
	paymentServiceServiceInfoForStreamClient = NewServiceInfoForStreamClient()
)

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

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

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

// NewServiceInfo creates a new ServiceInfo containing all methods
func NewServiceInfo() *kitex.ServiceInfo {
	return newServiceInfo(false, 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 := "PaymentService"
	handlerType := (*payment.PaymentService)(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": "payment",
	}
	if hasStreaming {
		extra["streaming"] = hasStreaming
	}
	svcInfo := &kitex.ServiceInfo{
		ServiceName:     serviceName,
		HandlerType:     handlerType,
		Methods:         methods,
		PayloadCodec:    kitex.Protobuf,
		KiteXGenVersion: "v0.9.1",
		Extra:           extra,
	}
	return svcInfo
}

func chargeHandler(ctx context.Context, handler interface{}, arg, result interface{}) error {
	switch s := arg.(type) {
	case *streaming.Args:
		st := s.Stream
		req := new(payment.ChargeReq)
		if err := st.RecvMsg(req); err != nil {
			return err
		}
		resp, err := handler.(payment.PaymentService).Charge(ctx, req)
		if err != nil {
			return err
		}
		return st.SendMsg(resp)
	case *ChargeArgs:
		success, err := handler.(payment.PaymentService).Charge(ctx, s.Req)
		if err != nil {
			return err
		}
		realResult := result.(*ChargeResult)
		realResult.Success = success
		return nil
	default:
		return errInvalidMessageType
	}
}
func newChargeArgs() interface{} {
	return &ChargeArgs{}
}

func newChargeResult() interface{} {
	return &ChargeResult{}
}

type ChargeArgs struct {
	Req *payment.ChargeReq
}

func (p *ChargeArgs) FastRead(buf []byte, _type int8, number int32) (n int, err error) {
	if !p.IsSetReq() {
		p.Req = new(payment.ChargeReq)
	}
	return p.Req.FastRead(buf, _type, number)
}

func (p *ChargeArgs) FastWrite(buf []byte) (n int) {
	if !p.IsSetReq() {
		return 0
	}
	return p.Req.FastWrite(buf)
}

func (p *ChargeArgs) Size() (n int) {
	if !p.IsSetReq() {
		return 0
	}
	return p.Req.Size()
}

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

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

var ChargeArgs_Req_DEFAULT *payment.ChargeReq

func (p *ChargeArgs) GetReq() *payment.ChargeReq {
	if !p.IsSetReq() {
		return ChargeArgs_Req_DEFAULT
	}
	return p.Req
}

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

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

type ChargeResult struct {
	Success *payment.ChargeResp
}

var ChargeResult_Success_DEFAULT *payment.ChargeResp

func (p *ChargeResult) FastRead(buf []byte, _type int8, number int32) (n int, err error) {
	if !p.IsSetSuccess() {
		p.Success = new(payment.ChargeResp)
	}
	return p.Success.FastRead(buf, _type, number)
}

func (p *ChargeResult) FastWrite(buf []byte) (n int) {
	if !p.IsSetSuccess() {
		return 0
	}
	return p.Success.FastWrite(buf)
}

func (p *ChargeResult) Size() (n int) {
	if !p.IsSetSuccess() {
		return 0
	}
	return p.Success.Size()
}

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

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

func (p *ChargeResult) GetSuccess() *payment.ChargeResp {
	if !p.IsSetSuccess() {
		return ChargeResult_Success_DEFAULT
	}
	return p.Success
}

func (p *ChargeResult) SetSuccess(x interface{}) {
	p.Success = x.(*payment.ChargeResp)
}

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

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

func cancelPaymentHandler(ctx context.Context, handler interface{}, arg, result interface{}) error {
	switch s := arg.(type) {
	case *streaming.Args:
		st := s.Stream
		req := new(payment.CancelReq)
		if err := st.RecvMsg(req); err != nil {
			return err
		}
		resp, err := handler.(payment.PaymentService).CancelPayment(ctx, req)
		if err != nil {
			return err
		}
		return st.SendMsg(resp)
	case *CancelPaymentArgs:
		success, err := handler.(payment.PaymentService).CancelPayment(ctx, s.Req)
		if err != nil {
			return err
		}
		realResult := result.(*CancelPaymentResult)
		realResult.Success = success
		return nil
	default:
		return errInvalidMessageType
	}
}
func newCancelPaymentArgs() interface{} {
	return &CancelPaymentArgs{}
}

func newCancelPaymentResult() interface{} {
	return &CancelPaymentResult{}
}

type CancelPaymentArgs struct {
	Req *payment.CancelReq
}

func (p *CancelPaymentArgs) FastRead(buf []byte, _type int8, number int32) (n int, err error) {
	if !p.IsSetReq() {
		p.Req = new(payment.CancelReq)
	}
	return p.Req.FastRead(buf, _type, number)
}

func (p *CancelPaymentArgs) FastWrite(buf []byte) (n int) {
	if !p.IsSetReq() {
		return 0
	}
	return p.Req.FastWrite(buf)
}

func (p *CancelPaymentArgs) Size() (n int) {
	if !p.IsSetReq() {
		return 0
	}
	return p.Req.Size()
}

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

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

var CancelPaymentArgs_Req_DEFAULT *payment.CancelReq

func (p *CancelPaymentArgs) GetReq() *payment.CancelReq {
	if !p.IsSetReq() {
		return CancelPaymentArgs_Req_DEFAULT
	}
	return p.Req
}

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

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

type CancelPaymentResult struct {
	Success *payment.CancelResp
}

var CancelPaymentResult_Success_DEFAULT *payment.CancelResp

func (p *CancelPaymentResult) FastRead(buf []byte, _type int8, number int32) (n int, err error) {
	if !p.IsSetSuccess() {
		p.Success = new(payment.CancelResp)
	}
	return p.Success.FastRead(buf, _type, number)
}

func (p *CancelPaymentResult) FastWrite(buf []byte) (n int) {
	if !p.IsSetSuccess() {
		return 0
	}
	return p.Success.FastWrite(buf)
}

func (p *CancelPaymentResult) Size() (n int) {
	if !p.IsSetSuccess() {
		return 0
	}
	return p.Success.Size()
}

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

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

func (p *CancelPaymentResult) GetSuccess() *payment.CancelResp {
	if !p.IsSetSuccess() {
		return CancelPaymentResult_Success_DEFAULT
	}
	return p.Success
}

func (p *CancelPaymentResult) SetSuccess(x interface{}) {
	p.Success = x.(*payment.CancelResp)
}

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

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

func chargeByThirdPartyHandler(ctx context.Context, handler interface{}, arg, result interface{}) error {
	switch s := arg.(type) {
	case *streaming.Args:
		st := s.Stream
		req := new(payment.ChargeByThirdPartyReq)
		if err := st.RecvMsg(req); err != nil {
			return err
		}
		resp, err := handler.(payment.PaymentService).ChargeByThirdParty(ctx, req)
		if err != nil {
			return err
		}
		return st.SendMsg(resp)
	case *ChargeByThirdPartyArgs:
		success, err := handler.(payment.PaymentService).ChargeByThirdParty(ctx, s.Req)
		if err != nil {
			return err
		}
		realResult := result.(*ChargeByThirdPartyResult)
		realResult.Success = success
		return nil
	default:
		return errInvalidMessageType
	}
}
func newChargeByThirdPartyArgs() interface{} {
	return &ChargeByThirdPartyArgs{}
}

func newChargeByThirdPartyResult() interface{} {
	return &ChargeByThirdPartyResult{}
}

type ChargeByThirdPartyArgs struct {
	Req *payment.ChargeByThirdPartyReq
}

func (p *ChargeByThirdPartyArgs) FastRead(buf []byte, _type int8, number int32) (n int, err error) {
	if !p.IsSetReq() {
		p.Req = new(payment.ChargeByThirdPartyReq)
	}
	return p.Req.FastRead(buf, _type, number)
}

func (p *ChargeByThirdPartyArgs) FastWrite(buf []byte) (n int) {
	if !p.IsSetReq() {
		return 0
	}
	return p.Req.FastWrite(buf)
}

func (p *ChargeByThirdPartyArgs) Size() (n int) {
	if !p.IsSetReq() {
		return 0
	}
	return p.Req.Size()
}

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

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

var ChargeByThirdPartyArgs_Req_DEFAULT *payment.ChargeByThirdPartyReq

func (p *ChargeByThirdPartyArgs) GetReq() *payment.ChargeByThirdPartyReq {
	if !p.IsSetReq() {
		return ChargeByThirdPartyArgs_Req_DEFAULT
	}
	return p.Req
}

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

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

type ChargeByThirdPartyResult struct {
	Success *payment.ChargeByThirdPartyResp
}

var ChargeByThirdPartyResult_Success_DEFAULT *payment.ChargeByThirdPartyResp

func (p *ChargeByThirdPartyResult) FastRead(buf []byte, _type int8, number int32) (n int, err error) {
	if !p.IsSetSuccess() {
		p.Success = new(payment.ChargeByThirdPartyResp)
	}
	return p.Success.FastRead(buf, _type, number)
}

func (p *ChargeByThirdPartyResult) FastWrite(buf []byte) (n int) {
	if !p.IsSetSuccess() {
		return 0
	}
	return p.Success.FastWrite(buf)
}

func (p *ChargeByThirdPartyResult) Size() (n int) {
	if !p.IsSetSuccess() {
		return 0
	}
	return p.Success.Size()
}

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

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

func (p *ChargeByThirdPartyResult) GetSuccess() *payment.ChargeByThirdPartyResp {
	if !p.IsSetSuccess() {
		return ChargeByThirdPartyResult_Success_DEFAULT
	}
	return p.Success
}

func (p *ChargeByThirdPartyResult) SetSuccess(x interface{}) {
	p.Success = x.(*payment.ChargeByThirdPartyResp)
}

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

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

type kClient struct {
	c client.Client
}

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

func (p *kClient) Charge(ctx context.Context, Req *payment.ChargeReq) (r *payment.ChargeResp, err error) {
	var _args ChargeArgs
	_args.Req = Req
	var _result ChargeResult
	if err = p.c.Call(ctx, "Charge", &_args, &_result); err != nil {
		return
	}
	return _result.GetSuccess(), nil
}

func (p *kClient) CancelPayment(ctx context.Context, Req *payment.CancelReq) (r *payment.CancelResp, err error) {
	var _args CancelPaymentArgs
	_args.Req = Req
	var _result CancelPaymentResult
	if err = p.c.Call(ctx, "CancelPayment", &_args, &_result); err != nil {
		return
	}
	return _result.GetSuccess(), nil
}

func (p *kClient) ChargeByThirdParty(ctx context.Context, Req *payment.ChargeByThirdPartyReq) (r *payment.ChargeByThirdPartyResp, err error) {
	var _args ChargeByThirdPartyArgs
	_args.Req = Req
	var _result ChargeByThirdPartyResult
	if err = p.c.Call(ctx, "ChargeByThirdParty", &_args, &_result); err != nil {
		return
	}
	return _result.GetSuccess(), nil
}
