/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

    package com.ywt.gapi.third.sinopharm;

import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.PathResolver;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.ServerService;
import org.apache.dubbo.rpc.TriRpcStatus;
import org.apache.dubbo.rpc.model.MethodDescriptor;
import org.apache.dubbo.rpc.model.ServiceDescriptor;
import org.apache.dubbo.rpc.model.StubMethodDescriptor;
import org.apache.dubbo.rpc.model.StubServiceDescriptor;
import org.apache.dubbo.rpc.stub.BiStreamMethodHandler;
import org.apache.dubbo.rpc.stub.ServerStreamMethodHandler;
import org.apache.dubbo.rpc.stub.StubInvocationUtil;
import org.apache.dubbo.rpc.stub.StubInvoker;
import org.apache.dubbo.rpc.stub.StubMethodHandler;
import org.apache.dubbo.rpc.stub.StubSuppliers;
import org.apache.dubbo.rpc.stub.UnaryStubMethodHandler;

import com.google.protobuf.Message;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.concurrent.CompletableFuture;

public final class DubboSinopharmServiceTriple {

    public static final String SERVICE_NAME = SinopharmService.SERVICE_NAME;

    private static final StubServiceDescriptor serviceDescriptor = new StubServiceDescriptor(SERVICE_NAME,SinopharmService.class);

    static {
        org.apache.dubbo.rpc.protocol.tri.service.SchemaDescriptorRegistry.addSchemaDescriptor(SERVICE_NAME,SinopharmProto.getDescriptor());
        StubSuppliers.addSupplier(SERVICE_NAME, DubboSinopharmServiceTriple::newStub);
        StubSuppliers.addSupplier(SinopharmService.JAVA_SERVICE_NAME,  DubboSinopharmServiceTriple::newStub);
        StubSuppliers.addDescriptor(SERVICE_NAME, serviceDescriptor);
        StubSuppliers.addDescriptor(SinopharmService.JAVA_SERVICE_NAME, serviceDescriptor);
    }

    @SuppressWarnings("all")
    public static SinopharmService newStub(Invoker<?> invoker) {
        return new SinopharmServiceStub((Invoker<SinopharmService>)invoker);
    }

    private static final StubMethodDescriptor drugMatchingMethod = new StubMethodDescriptor("drugMatching",
    com.ywt.gapi.third.sinopharm.DrugMatchingRequest.class, com.ywt.gapi.third.sinopharm.DrugMatchingResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.DrugMatchingRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.DrugMatchingResponse::parseFrom);

    private static final StubMethodDescriptor drugMatchingAsyncMethod = new StubMethodDescriptor("drugMatching",
    com.ywt.gapi.third.sinopharm.DrugMatchingRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.DrugMatchingRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.DrugMatchingResponse::parseFrom);

    private static final StubMethodDescriptor drugMatchingProxyAsyncMethod = new StubMethodDescriptor("drugMatchingAsync",
    com.ywt.gapi.third.sinopharm.DrugMatchingRequest.class, com.ywt.gapi.third.sinopharm.DrugMatchingResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.DrugMatchingRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.DrugMatchingResponse::parseFrom);

    private static final StubMethodDescriptor confirmOrderMethod = new StubMethodDescriptor("confirmOrder",
    com.ywt.gapi.third.sinopharm.ConfirmOrderRequest.class, com.ywt.gapi.third.sinopharm.ConfirmOrderResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.ConfirmOrderRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.ConfirmOrderResponse::parseFrom);

    private static final StubMethodDescriptor confirmOrderAsyncMethod = new StubMethodDescriptor("confirmOrder",
    com.ywt.gapi.third.sinopharm.ConfirmOrderRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.ConfirmOrderRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.ConfirmOrderResponse::parseFrom);

    private static final StubMethodDescriptor confirmOrderProxyAsyncMethod = new StubMethodDescriptor("confirmOrderAsync",
    com.ywt.gapi.third.sinopharm.ConfirmOrderRequest.class, com.ywt.gapi.third.sinopharm.ConfirmOrderResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.ConfirmOrderRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.ConfirmOrderResponse::parseFrom);

    private static final StubMethodDescriptor pushInvoiceMethod = new StubMethodDescriptor("pushInvoice",
    com.ywt.gapi.third.sinopharm.PushInvoiceRequest.class, com.ywt.gapi.third.sinopharm.PushInvoiceResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.PushInvoiceRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.PushInvoiceResponse::parseFrom);

    private static final StubMethodDescriptor pushInvoiceAsyncMethod = new StubMethodDescriptor("pushInvoice",
    com.ywt.gapi.third.sinopharm.PushInvoiceRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.PushInvoiceRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.PushInvoiceResponse::parseFrom);

    private static final StubMethodDescriptor pushInvoiceProxyAsyncMethod = new StubMethodDescriptor("pushInvoiceAsync",
    com.ywt.gapi.third.sinopharm.PushInvoiceRequest.class, com.ywt.gapi.third.sinopharm.PushInvoiceResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.PushInvoiceRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.PushInvoiceResponse::parseFrom);

    private static final StubMethodDescriptor confirmOpenHisAPIOrderMethod = new StubMethodDescriptor("confirmOpenHisAPIOrder",
    com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest.class, com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse::parseFrom);

    private static final StubMethodDescriptor confirmOpenHisAPIOrderAsyncMethod = new StubMethodDescriptor("confirmOpenHisAPIOrder",
    com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse::parseFrom);

    private static final StubMethodDescriptor confirmOpenHisAPIOrderProxyAsyncMethod = new StubMethodDescriptor("confirmOpenHisAPIOrderAsync",
    com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest.class, com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest::parseFrom,
    com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse::parseFrom);





    public static class SinopharmServiceStub implements SinopharmService{
        private final Invoker<SinopharmService> invoker;

        public SinopharmServiceStub(Invoker<SinopharmService> invoker) {
            this.invoker = invoker;
        }

        @Override
        public com.ywt.gapi.third.sinopharm.DrugMatchingResponse drugMatching(com.ywt.gapi.third.sinopharm.DrugMatchingRequest request){
            return StubInvocationUtil.unaryCall(invoker, drugMatchingMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.third.sinopharm.DrugMatchingResponse> drugMatchingAsync(com.ywt.gapi.third.sinopharm.DrugMatchingRequest request){
            return StubInvocationUtil.unaryCall(invoker, drugMatchingAsyncMethod, request);
        }

        @Override
        public void drugMatching(com.ywt.gapi.third.sinopharm.DrugMatchingRequest request, StreamObserver<com.ywt.gapi.third.sinopharm.DrugMatchingResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, drugMatchingMethod , request, responseObserver);
        }
        @Override
        public com.ywt.gapi.third.sinopharm.ConfirmOrderResponse confirmOrder(com.ywt.gapi.third.sinopharm.ConfirmOrderRequest request){
            return StubInvocationUtil.unaryCall(invoker, confirmOrderMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.third.sinopharm.ConfirmOrderResponse> confirmOrderAsync(com.ywt.gapi.third.sinopharm.ConfirmOrderRequest request){
            return StubInvocationUtil.unaryCall(invoker, confirmOrderAsyncMethod, request);
        }

        @Override
        public void confirmOrder(com.ywt.gapi.third.sinopharm.ConfirmOrderRequest request, StreamObserver<com.ywt.gapi.third.sinopharm.ConfirmOrderResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, confirmOrderMethod , request, responseObserver);
        }
        @Override
        public com.ywt.gapi.third.sinopharm.PushInvoiceResponse pushInvoice(com.ywt.gapi.third.sinopharm.PushInvoiceRequest request){
            return StubInvocationUtil.unaryCall(invoker, pushInvoiceMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.third.sinopharm.PushInvoiceResponse> pushInvoiceAsync(com.ywt.gapi.third.sinopharm.PushInvoiceRequest request){
            return StubInvocationUtil.unaryCall(invoker, pushInvoiceAsyncMethod, request);
        }

        @Override
        public void pushInvoice(com.ywt.gapi.third.sinopharm.PushInvoiceRequest request, StreamObserver<com.ywt.gapi.third.sinopharm.PushInvoiceResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, pushInvoiceMethod , request, responseObserver);
        }
        @Override
        public com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse confirmOpenHisAPIOrder(com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest request){
            return StubInvocationUtil.unaryCall(invoker, confirmOpenHisAPIOrderMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse> confirmOpenHisAPIOrderAsync(com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest request){
            return StubInvocationUtil.unaryCall(invoker, confirmOpenHisAPIOrderAsyncMethod, request);
        }

        @Override
        public void confirmOpenHisAPIOrder(com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest request, StreamObserver<com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, confirmOpenHisAPIOrderMethod , request, responseObserver);
        }



    }

    public static abstract class SinopharmServiceImplBase implements SinopharmService, ServerService<SinopharmService> {

        private <T, R> BiConsumer<T, StreamObserver<R>> syncToAsync(java.util.function.Function<T, R> syncFun) {
            return new BiConsumer<T, StreamObserver<R>>() {
                @Override
                public void accept(T t, StreamObserver<R> observer) {
                    try {
                        R ret = syncFun.apply(t);
                        observer.onNext(ret);
                        observer.onCompleted();
                    } catch (Throwable e) {
                        observer.onError(e);
                    }
                }
            };
        }

        @Override
        public final Invoker<SinopharmService> getInvoker(URL url) {
            PathResolver pathResolver = url.getOrDefaultFrameworkModel()
            .getExtensionLoader(PathResolver.class)
            .getDefaultExtension();
            Map<String,StubMethodHandler<?, ?>> handlers = new HashMap<>();

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/drugMatching" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/drugMatchingAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/confirmOrder" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/confirmOrderAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/pushInvoice" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/pushInvoiceAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/confirmOpenHisAPIOrder" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/confirmOpenHisAPIOrderAsync" );

            BiConsumer<com.ywt.gapi.third.sinopharm.DrugMatchingRequest, StreamObserver<com.ywt.gapi.third.sinopharm.DrugMatchingResponse>> drugMatchingFunc = this::drugMatching;
            handlers.put(drugMatchingMethod.getMethodName(), new UnaryStubMethodHandler<>(drugMatchingFunc));
            BiConsumer<com.ywt.gapi.third.sinopharm.DrugMatchingRequest, StreamObserver<com.ywt.gapi.third.sinopharm.DrugMatchingResponse>> drugMatchingAsyncFunc = syncToAsync(this::drugMatching);
            handlers.put(drugMatchingProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(drugMatchingAsyncFunc));
            BiConsumer<com.ywt.gapi.third.sinopharm.ConfirmOrderRequest, StreamObserver<com.ywt.gapi.third.sinopharm.ConfirmOrderResponse>> confirmOrderFunc = this::confirmOrder;
            handlers.put(confirmOrderMethod.getMethodName(), new UnaryStubMethodHandler<>(confirmOrderFunc));
            BiConsumer<com.ywt.gapi.third.sinopharm.ConfirmOrderRequest, StreamObserver<com.ywt.gapi.third.sinopharm.ConfirmOrderResponse>> confirmOrderAsyncFunc = syncToAsync(this::confirmOrder);
            handlers.put(confirmOrderProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(confirmOrderAsyncFunc));
            BiConsumer<com.ywt.gapi.third.sinopharm.PushInvoiceRequest, StreamObserver<com.ywt.gapi.third.sinopharm.PushInvoiceResponse>> pushInvoiceFunc = this::pushInvoice;
            handlers.put(pushInvoiceMethod.getMethodName(), new UnaryStubMethodHandler<>(pushInvoiceFunc));
            BiConsumer<com.ywt.gapi.third.sinopharm.PushInvoiceRequest, StreamObserver<com.ywt.gapi.third.sinopharm.PushInvoiceResponse>> pushInvoiceAsyncFunc = syncToAsync(this::pushInvoice);
            handlers.put(pushInvoiceProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(pushInvoiceAsyncFunc));
            BiConsumer<com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest, StreamObserver<com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse>> confirmOpenHisAPIOrderFunc = this::confirmOpenHisAPIOrder;
            handlers.put(confirmOpenHisAPIOrderMethod.getMethodName(), new UnaryStubMethodHandler<>(confirmOpenHisAPIOrderFunc));
            BiConsumer<com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest, StreamObserver<com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse>> confirmOpenHisAPIOrderAsyncFunc = syncToAsync(this::confirmOpenHisAPIOrder);
            handlers.put(confirmOpenHisAPIOrderProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(confirmOpenHisAPIOrderAsyncFunc));




            return new StubInvoker<>(this, url, SinopharmService.class, handlers);
        }


        @Override
        public com.ywt.gapi.third.sinopharm.DrugMatchingResponse drugMatching(com.ywt.gapi.third.sinopharm.DrugMatchingRequest request){
            throw unimplementedMethodException(drugMatchingMethod);
        }

        @Override
        public com.ywt.gapi.third.sinopharm.ConfirmOrderResponse confirmOrder(com.ywt.gapi.third.sinopharm.ConfirmOrderRequest request){
            throw unimplementedMethodException(confirmOrderMethod);
        }

        @Override
        public com.ywt.gapi.third.sinopharm.PushInvoiceResponse pushInvoice(com.ywt.gapi.third.sinopharm.PushInvoiceRequest request){
            throw unimplementedMethodException(pushInvoiceMethod);
        }

        @Override
        public com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIResponse confirmOpenHisAPIOrder(com.ywt.gapi.third.sinopharm.ConfirmOrderOpenHisAPIRequest request){
            throw unimplementedMethodException(confirmOpenHisAPIOrderMethod);
        }





        @Override
        public final ServiceDescriptor getServiceDescriptor() {
            return serviceDescriptor;
        }
        private RpcException unimplementedMethodException(StubMethodDescriptor methodDescriptor) {
            return TriRpcStatus.UNIMPLEMENTED.withDescription(String.format("Method %s is unimplemented",
                "/" + serviceDescriptor.getInterfaceName() + "/" + methodDescriptor.getMethodName())).asException();
        }
    }

}
