/*
* 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.med_equip;

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 DubboLepuServiceTriple {

    public static final String SERVICE_NAME = LepuService.SERVICE_NAME;

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

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

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

    /**
         * <pre>
         *  unused
         * </pre>
         */
    private static final StubMethodDescriptor allocEquipMethod = new StubMethodDescriptor("allocEquip",
    com.ywt.gapi.med_equip.AllocEquipRequest.class, com.ywt.gapi.med_equip.AllocEquipResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.med_equip.AllocEquipRequest::parseFrom,
    com.ywt.gapi.med_equip.AllocEquipResponse::parseFrom);

    private static final StubMethodDescriptor allocEquipAsyncMethod = new StubMethodDescriptor("allocEquip",
    com.ywt.gapi.med_equip.AllocEquipRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.med_equip.AllocEquipRequest::parseFrom,
    com.ywt.gapi.med_equip.AllocEquipResponse::parseFrom);

    private static final StubMethodDescriptor allocEquipProxyAsyncMethod = new StubMethodDescriptor("allocEquipAsync",
    com.ywt.gapi.med_equip.AllocEquipRequest.class, com.ywt.gapi.med_equip.AllocEquipResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.med_equip.AllocEquipRequest::parseFrom,
    com.ywt.gapi.med_equip.AllocEquipResponse::parseFrom);

    /**
         * <pre>
         *  开单医生 id
         * </pre>
         */
    private static final StubMethodDescriptor recycleEquipMethod = new StubMethodDescriptor("recycleEquip",
    com.ywt.gapi.med_equip.RecycleEquipRequest.class, com.ywt.gapi.med_equip.RecycleEquipResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.med_equip.RecycleEquipRequest::parseFrom,
    com.ywt.gapi.med_equip.RecycleEquipResponse::parseFrom);

    private static final StubMethodDescriptor recycleEquipAsyncMethod = new StubMethodDescriptor("recycleEquip",
    com.ywt.gapi.med_equip.RecycleEquipRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.med_equip.RecycleEquipRequest::parseFrom,
    com.ywt.gapi.med_equip.RecycleEquipResponse::parseFrom);

    private static final StubMethodDescriptor recycleEquipProxyAsyncMethod = new StubMethodDescriptor("recycleEquipAsync",
    com.ywt.gapi.med_equip.RecycleEquipRequest.class, com.ywt.gapi.med_equip.RecycleEquipResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.med_equip.RecycleEquipRequest::parseFrom,
    com.ywt.gapi.med_equip.RecycleEquipResponse::parseFrom);





    public static class LepuServiceStub implements LepuService{
        private final Invoker<LepuService> invoker;

        public LepuServiceStub(Invoker<LepuService> invoker) {
            this.invoker = invoker;
        }

            /**
         * <pre>
         *  unused
         * </pre>
         */
        @Override
        public com.ywt.gapi.med_equip.AllocEquipResponse allocEquip(com.ywt.gapi.med_equip.AllocEquipRequest request){
            return StubInvocationUtil.unaryCall(invoker, allocEquipMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.med_equip.AllocEquipResponse> allocEquipAsync(com.ywt.gapi.med_equip.AllocEquipRequest request){
            return StubInvocationUtil.unaryCall(invoker, allocEquipAsyncMethod, request);
        }

            /**
         * <pre>
         *  unused
         * </pre>
         */
        @Override
        public void allocEquip(com.ywt.gapi.med_equip.AllocEquipRequest request, StreamObserver<com.ywt.gapi.med_equip.AllocEquipResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, allocEquipMethod , request, responseObserver);
        }
            /**
         * <pre>
         *  开单医生 id
         * </pre>
         */
        @Override
        public com.ywt.gapi.med_equip.RecycleEquipResponse recycleEquip(com.ywt.gapi.med_equip.RecycleEquipRequest request){
            return StubInvocationUtil.unaryCall(invoker, recycleEquipMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.med_equip.RecycleEquipResponse> recycleEquipAsync(com.ywt.gapi.med_equip.RecycleEquipRequest request){
            return StubInvocationUtil.unaryCall(invoker, recycleEquipAsyncMethod, request);
        }

            /**
         * <pre>
         *  开单医生 id
         * </pre>
         */
        @Override
        public void recycleEquip(com.ywt.gapi.med_equip.RecycleEquipRequest request, StreamObserver<com.ywt.gapi.med_equip.RecycleEquipResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, recycleEquipMethod , request, responseObserver);
        }



    }

    public static abstract class LepuServiceImplBase implements LepuService, ServerService<LepuService> {

        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<LepuService> getInvoker(URL url) {
            PathResolver pathResolver = url.getOrDefaultFrameworkModel()
            .getExtensionLoader(PathResolver.class)
            .getDefaultExtension();
            Map<String,StubMethodHandler<?, ?>> handlers = new HashMap<>();

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/allocEquip" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/allocEquipAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/recycleEquip" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/recycleEquipAsync" );

            BiConsumer<com.ywt.gapi.med_equip.AllocEquipRequest, StreamObserver<com.ywt.gapi.med_equip.AllocEquipResponse>> allocEquipFunc = this::allocEquip;
            handlers.put(allocEquipMethod.getMethodName(), new UnaryStubMethodHandler<>(allocEquipFunc));
            BiConsumer<com.ywt.gapi.med_equip.AllocEquipRequest, StreamObserver<com.ywt.gapi.med_equip.AllocEquipResponse>> allocEquipAsyncFunc = syncToAsync(this::allocEquip);
            handlers.put(allocEquipProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(allocEquipAsyncFunc));
            BiConsumer<com.ywt.gapi.med_equip.RecycleEquipRequest, StreamObserver<com.ywt.gapi.med_equip.RecycleEquipResponse>> recycleEquipFunc = this::recycleEquip;
            handlers.put(recycleEquipMethod.getMethodName(), new UnaryStubMethodHandler<>(recycleEquipFunc));
            BiConsumer<com.ywt.gapi.med_equip.RecycleEquipRequest, StreamObserver<com.ywt.gapi.med_equip.RecycleEquipResponse>> recycleEquipAsyncFunc = syncToAsync(this::recycleEquip);
            handlers.put(recycleEquipProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(recycleEquipAsyncFunc));




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


        @Override
        public com.ywt.gapi.med_equip.AllocEquipResponse allocEquip(com.ywt.gapi.med_equip.AllocEquipRequest request){
            throw unimplementedMethodException(allocEquipMethod);
        }

        @Override
        public com.ywt.gapi.med_equip.RecycleEquipResponse recycleEquip(com.ywt.gapi.med_equip.RecycleEquipRequest request){
            throw unimplementedMethodException(recycleEquipMethod);
        }





        @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();
        }
    }

}
