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

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

    public static final String SERVICE_NAME = DoctorService.SERVICE_NAME;

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

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

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

    private static final StubMethodDescriptor getDoctorInfoMethod = new StubMethodDescriptor("getDoctorInfo",
    DoctorRequest.class, DoctorInfoResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), DoctorRequest::parseFrom,
    DoctorInfoResponse::parseFrom);

    private static final StubMethodDescriptor getDoctorInfoAsyncMethod = new StubMethodDescriptor("getDoctorInfo",
    DoctorRequest.class, CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), DoctorRequest::parseFrom,
    DoctorInfoResponse::parseFrom);

    private static final StubMethodDescriptor getDoctorInfoProxyAsyncMethod = new StubMethodDescriptor("getDoctorInfoAsync",
    DoctorRequest.class, DoctorInfoResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), DoctorRequest::parseFrom,
    DoctorInfoResponse::parseFrom);

    private static final StubMethodDescriptor getDoctorHisCodeMethod = new StubMethodDescriptor("getDoctorHisCode",
    GetDoctorHisCodeRequest.class, com.ywt.gapi.doctor.GetDoctorHisCodeResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), GetDoctorHisCodeRequest::parseFrom,
    com.ywt.gapi.doctor.GetDoctorHisCodeResponse::parseFrom);

    private static final StubMethodDescriptor getDoctorHisCodeAsyncMethod = new StubMethodDescriptor("getDoctorHisCode",
    GetDoctorHisCodeRequest.class, CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), GetDoctorHisCodeRequest::parseFrom,
    com.ywt.gapi.doctor.GetDoctorHisCodeResponse::parseFrom);

    private static final StubMethodDescriptor getDoctorHisCodeProxyAsyncMethod = new StubMethodDescriptor("getDoctorHisCodeAsync",
    GetDoctorHisCodeRequest.class, com.ywt.gapi.doctor.GetDoctorHisCodeResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), GetDoctorHisCodeRequest::parseFrom,
    com.ywt.gapi.doctor.GetDoctorHisCodeResponse::parseFrom);





    public static class DoctorServiceStub implements DoctorService{
        private final Invoker<DoctorService> invoker;

        public DoctorServiceStub(Invoker<DoctorService> invoker) {
            this.invoker = invoker;
        }

        @Override
        public DoctorInfoResponse getDoctorInfo(DoctorRequest request){
            return StubInvocationUtil.unaryCall(invoker, getDoctorInfoMethod, request);
        }

        public CompletableFuture<DoctorInfoResponse> getDoctorInfoAsync(DoctorRequest request){
            return StubInvocationUtil.unaryCall(invoker, getDoctorInfoAsyncMethod, request);
        }

        @Override
        public void getDoctorInfo(DoctorRequest request, StreamObserver<DoctorInfoResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, getDoctorInfoMethod , request, responseObserver);
        }
        @Override
        public com.ywt.gapi.doctor.GetDoctorHisCodeResponse getDoctorHisCode(GetDoctorHisCodeRequest request){
            return StubInvocationUtil.unaryCall(invoker, getDoctorHisCodeMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.doctor.GetDoctorHisCodeResponse> getDoctorHisCodeAsync(GetDoctorHisCodeRequest request){
            return StubInvocationUtil.unaryCall(invoker, getDoctorHisCodeAsyncMethod, request);
        }

        @Override
        public void getDoctorHisCode(GetDoctorHisCodeRequest request, StreamObserver<com.ywt.gapi.doctor.GetDoctorHisCodeResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, getDoctorHisCodeMethod , request, responseObserver);
        }



    }

    public static abstract class DoctorServiceImplBase implements DoctorService, ServerService<DoctorService> {

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

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/getDoctorInfo" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/getDoctorInfoAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/getDoctorHisCode" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/getDoctorHisCodeAsync" );

            BiConsumer<DoctorRequest, StreamObserver<DoctorInfoResponse>> getDoctorInfoFunc = this::getDoctorInfo;
            handlers.put(getDoctorInfoMethod.getMethodName(), new UnaryStubMethodHandler<>(getDoctorInfoFunc));
            BiConsumer<DoctorRequest, StreamObserver<DoctorInfoResponse>> getDoctorInfoAsyncFunc = syncToAsync(this::getDoctorInfo);
            handlers.put(getDoctorInfoProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(getDoctorInfoAsyncFunc));
            BiConsumer<GetDoctorHisCodeRequest, StreamObserver<com.ywt.gapi.doctor.GetDoctorHisCodeResponse>> getDoctorHisCodeFunc = this::getDoctorHisCode;
            handlers.put(getDoctorHisCodeMethod.getMethodName(), new UnaryStubMethodHandler<>(getDoctorHisCodeFunc));
            BiConsumer<GetDoctorHisCodeRequest, StreamObserver<com.ywt.gapi.doctor.GetDoctorHisCodeResponse>> getDoctorHisCodeAsyncFunc = syncToAsync(this::getDoctorHisCode);
            handlers.put(getDoctorHisCodeProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(getDoctorHisCodeAsyncFunc));




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


        @Override
        public DoctorInfoResponse getDoctorInfo(DoctorRequest request){
            throw unimplementedMethodException(getDoctorInfoMethod);
        }

        @Override
        public com.ywt.gapi.doctor.GetDoctorHisCodeResponse getDoctorHisCode(GetDoctorHisCodeRequest request){
            throw unimplementedMethodException(getDoctorHisCodeMethod);
        }





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

}
