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

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

    public static final String SERVICE_NAME = TaiheService.SERVICE_NAME;

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

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

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

    /**
         * <pre>
         * 就诊ID||预约类型
         * </pre>
         */
    private static final StubMethodDescriptor phyPayOrderMethod = new StubMethodDescriptor("phyPayOrder",
    PhyPayOrderRequest.class, PhyPayOrderResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), PhyPayOrderRequest::parseFrom,
    PhyPayOrderResponse::parseFrom);

    private static final StubMethodDescriptor phyPayOrderAsyncMethod = new StubMethodDescriptor("phyPayOrder",
    PhyPayOrderRequest.class, CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), PhyPayOrderRequest::parseFrom,
    PhyPayOrderResponse::parseFrom);

    private static final StubMethodDescriptor phyPayOrderProxyAsyncMethod = new StubMethodDescriptor("phyPayOrderAsync",
    PhyPayOrderRequest.class, PhyPayOrderResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), PhyPayOrderRequest::parseFrom,
    PhyPayOrderResponse::parseFrom);

    /**
         * <pre>
         * 登记号 非必须
         * </pre>
         */
    private static final StubMethodDescriptor getScheduleListMethod = new StubMethodDescriptor("getScheduleList",
    GetScheduleListRequest.class, com.ywt.gapi.third.taihe.GetScheduleListResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), GetScheduleListRequest::parseFrom,
    com.ywt.gapi.third.taihe.GetScheduleListResponse::parseFrom);

    private static final StubMethodDescriptor getScheduleListAsyncMethod = new StubMethodDescriptor("getScheduleList",
    GetScheduleListRequest.class, CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), GetScheduleListRequest::parseFrom,
    com.ywt.gapi.third.taihe.GetScheduleListResponse::parseFrom);

    private static final StubMethodDescriptor getScheduleListProxyAsyncMethod = new StubMethodDescriptor("getScheduleListAsync",
    GetScheduleListRequest.class, com.ywt.gapi.third.taihe.GetScheduleListResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), GetScheduleListRequest::parseFrom,
    com.ywt.gapi.third.taihe.GetScheduleListResponse::parseFrom);





    public static class TaiheServiceStub implements TaiheService{
        private final Invoker<TaiheService> invoker;

        public TaiheServiceStub(Invoker<TaiheService> invoker) {
            this.invoker = invoker;
        }

            /**
         * <pre>
         * 就诊ID||预约类型
         * </pre>
         */
        @Override
        public PhyPayOrderResponse phyPayOrder(PhyPayOrderRequest request){
            return StubInvocationUtil.unaryCall(invoker, phyPayOrderMethod, request);
        }

        public CompletableFuture<PhyPayOrderResponse> phyPayOrderAsync(PhyPayOrderRequest request){
            return StubInvocationUtil.unaryCall(invoker, phyPayOrderAsyncMethod, request);
        }

            /**
         * <pre>
         * 就诊ID||预约类型
         * </pre>
         */
        @Override
        public void phyPayOrder(PhyPayOrderRequest request, StreamObserver<PhyPayOrderResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, phyPayOrderMethod , request, responseObserver);
        }
            /**
         * <pre>
         * 登记号 非必须
         * </pre>
         */
        @Override
        public com.ywt.gapi.third.taihe.GetScheduleListResponse getScheduleList(GetScheduleListRequest request){
            return StubInvocationUtil.unaryCall(invoker, getScheduleListMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.third.taihe.GetScheduleListResponse> getScheduleListAsync(GetScheduleListRequest request){
            return StubInvocationUtil.unaryCall(invoker, getScheduleListAsyncMethod, request);
        }

            /**
         * <pre>
         * 登记号 非必须
         * </pre>
         */
        @Override
        public void getScheduleList(GetScheduleListRequest request, StreamObserver<com.ywt.gapi.third.taihe.GetScheduleListResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, getScheduleListMethod , request, responseObserver);
        }



    }

    public static abstract class TaiheServiceImplBase implements TaiheService, ServerService<TaiheService> {

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

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/phyPayOrder" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/phyPayOrderAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/getScheduleList" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/getScheduleListAsync" );

            BiConsumer<PhyPayOrderRequest, StreamObserver<PhyPayOrderResponse>> phyPayOrderFunc = this::phyPayOrder;
            handlers.put(phyPayOrderMethod.getMethodName(), new UnaryStubMethodHandler<>(phyPayOrderFunc));
            BiConsumer<PhyPayOrderRequest, StreamObserver<PhyPayOrderResponse>> phyPayOrderAsyncFunc = syncToAsync(this::phyPayOrder);
            handlers.put(phyPayOrderProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(phyPayOrderAsyncFunc));
            BiConsumer<GetScheduleListRequest, StreamObserver<com.ywt.gapi.third.taihe.GetScheduleListResponse>> getScheduleListFunc = this::getScheduleList;
            handlers.put(getScheduleListMethod.getMethodName(), new UnaryStubMethodHandler<>(getScheduleListFunc));
            BiConsumer<GetScheduleListRequest, StreamObserver<com.ywt.gapi.third.taihe.GetScheduleListResponse>> getScheduleListAsyncFunc = syncToAsync(this::getScheduleList);
            handlers.put(getScheduleListProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(getScheduleListAsyncFunc));




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


        @Override
        public PhyPayOrderResponse phyPayOrder(PhyPayOrderRequest request){
            throw unimplementedMethodException(phyPayOrderMethod);
        }

        @Override
        public com.ywt.gapi.third.taihe.GetScheduleListResponse getScheduleList(GetScheduleListRequest request){
            throw unimplementedMethodException(getScheduleListMethod);
        }





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

}
