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

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

    public static final String SERVICE_NAME = YiDaService.SERVICE_NAME;

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

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

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

    private static final StubMethodDescriptor inferenceMethod = new StubMethodDescriptor("inference",
    com.ywt.gapi.nfyy.InferenceRequest.class, com.ywt.gapi.nfyy.InferenceResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.nfyy.InferenceRequest::parseFrom,
    com.ywt.gapi.nfyy.InferenceResponse::parseFrom);

    private static final StubMethodDescriptor inferenceAsyncMethod = new StubMethodDescriptor("inference",
    com.ywt.gapi.nfyy.InferenceRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.nfyy.InferenceRequest::parseFrom,
    com.ywt.gapi.nfyy.InferenceResponse::parseFrom);

    private static final StubMethodDescriptor inferenceProxyAsyncMethod = new StubMethodDescriptor("inferenceAsync",
    com.ywt.gapi.nfyy.InferenceRequest.class, com.ywt.gapi.nfyy.InferenceResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.nfyy.InferenceRequest::parseFrom,
    com.ywt.gapi.nfyy.InferenceResponse::parseFrom);

    private static final StubMethodDescriptor statsDoctorMethod = new StubMethodDescriptor("statsDoctor",
    com.ywt.gapi.nfyy.StatsDoctorRequest.class, com.ywt.gapi.nfyy.StatsDoctorResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.nfyy.StatsDoctorRequest::parseFrom,
    com.ywt.gapi.nfyy.StatsDoctorResponse::parseFrom);

    private static final StubMethodDescriptor statsDoctorAsyncMethod = new StubMethodDescriptor("statsDoctor",
    com.ywt.gapi.nfyy.StatsDoctorRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.nfyy.StatsDoctorRequest::parseFrom,
    com.ywt.gapi.nfyy.StatsDoctorResponse::parseFrom);

    private static final StubMethodDescriptor statsDoctorProxyAsyncMethod = new StubMethodDescriptor("statsDoctorAsync",
    com.ywt.gapi.nfyy.StatsDoctorRequest.class, com.ywt.gapi.nfyy.StatsDoctorResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.nfyy.StatsDoctorRequest::parseFrom,
    com.ywt.gapi.nfyy.StatsDoctorResponse::parseFrom);





    public static class YiDaServiceStub implements YiDaService{
        private final Invoker<YiDaService> invoker;

        public YiDaServiceStub(Invoker<YiDaService> invoker) {
            this.invoker = invoker;
        }

        @Override
        public com.ywt.gapi.nfyy.InferenceResponse inference(com.ywt.gapi.nfyy.InferenceRequest request){
            return StubInvocationUtil.unaryCall(invoker, inferenceMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.nfyy.InferenceResponse> inferenceAsync(com.ywt.gapi.nfyy.InferenceRequest request){
            return StubInvocationUtil.unaryCall(invoker, inferenceAsyncMethod, request);
        }

        @Override
        public void inference(com.ywt.gapi.nfyy.InferenceRequest request, StreamObserver<com.ywt.gapi.nfyy.InferenceResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, inferenceMethod , request, responseObserver);
        }
        @Override
        public com.ywt.gapi.nfyy.StatsDoctorResponse statsDoctor(com.ywt.gapi.nfyy.StatsDoctorRequest request){
            return StubInvocationUtil.unaryCall(invoker, statsDoctorMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.nfyy.StatsDoctorResponse> statsDoctorAsync(com.ywt.gapi.nfyy.StatsDoctorRequest request){
            return StubInvocationUtil.unaryCall(invoker, statsDoctorAsyncMethod, request);
        }

        @Override
        public void statsDoctor(com.ywt.gapi.nfyy.StatsDoctorRequest request, StreamObserver<com.ywt.gapi.nfyy.StatsDoctorResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, statsDoctorMethod , request, responseObserver);
        }



    }

    public static abstract class YiDaServiceImplBase implements YiDaService, ServerService<YiDaService> {

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

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/inference" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/inferenceAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/statsDoctor" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/statsDoctorAsync" );

            BiConsumer<com.ywt.gapi.nfyy.InferenceRequest, StreamObserver<com.ywt.gapi.nfyy.InferenceResponse>> inferenceFunc = this::inference;
            handlers.put(inferenceMethod.getMethodName(), new UnaryStubMethodHandler<>(inferenceFunc));
            BiConsumer<com.ywt.gapi.nfyy.InferenceRequest, StreamObserver<com.ywt.gapi.nfyy.InferenceResponse>> inferenceAsyncFunc = syncToAsync(this::inference);
            handlers.put(inferenceProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(inferenceAsyncFunc));
            BiConsumer<com.ywt.gapi.nfyy.StatsDoctorRequest, StreamObserver<com.ywt.gapi.nfyy.StatsDoctorResponse>> statsDoctorFunc = this::statsDoctor;
            handlers.put(statsDoctorMethod.getMethodName(), new UnaryStubMethodHandler<>(statsDoctorFunc));
            BiConsumer<com.ywt.gapi.nfyy.StatsDoctorRequest, StreamObserver<com.ywt.gapi.nfyy.StatsDoctorResponse>> statsDoctorAsyncFunc = syncToAsync(this::statsDoctor);
            handlers.put(statsDoctorProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(statsDoctorAsyncFunc));




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


        @Override
        public com.ywt.gapi.nfyy.InferenceResponse inference(com.ywt.gapi.nfyy.InferenceRequest request){
            throw unimplementedMethodException(inferenceMethod);
        }

        @Override
        public com.ywt.gapi.nfyy.StatsDoctorResponse statsDoctor(com.ywt.gapi.nfyy.StatsDoctorRequest request){
            throw unimplementedMethodException(statsDoctorMethod);
        }





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

}
