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

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

    public static final String SERVICE_NAME = EmailService.SERVICE_NAME;

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

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

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

    /**
         * <pre>
         *  调用终端
         * </pre>
         */
    private static final StubMethodDescriptor sendMerchantEmailMethod = new StubMethodDescriptor("sendMerchantEmail",
    com.ywt.gapi.message.SendMerchantEmailRequest.class, com.ywt.gapi.message.SendMerchantEmailResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.message.SendMerchantEmailRequest::parseFrom,
    com.ywt.gapi.message.SendMerchantEmailResponse::parseFrom);

    private static final StubMethodDescriptor sendMerchantEmailAsyncMethod = new StubMethodDescriptor("sendMerchantEmail",
    com.ywt.gapi.message.SendMerchantEmailRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.message.SendMerchantEmailRequest::parseFrom,
    com.ywt.gapi.message.SendMerchantEmailResponse::parseFrom);

    private static final StubMethodDescriptor sendMerchantEmailProxyAsyncMethod = new StubMethodDescriptor("sendMerchantEmailAsync",
    com.ywt.gapi.message.SendMerchantEmailRequest.class, com.ywt.gapi.message.SendMerchantEmailResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.message.SendMerchantEmailRequest::parseFrom,
    com.ywt.gapi.message.SendMerchantEmailResponse::parseFrom);

    /**
         * <pre>
         *  调用服务名称
         * </pre>
         */
    private static final StubMethodDescriptor sendErrorEmailMethod = new StubMethodDescriptor("sendErrorEmail",
    com.ywt.gapi.message.SendErrorEmailRequest.class, com.ywt.gapi.Result.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.message.SendErrorEmailRequest::parseFrom,
    com.ywt.gapi.Result::parseFrom);

    private static final StubMethodDescriptor sendErrorEmailAsyncMethod = new StubMethodDescriptor("sendErrorEmail",
    com.ywt.gapi.message.SendErrorEmailRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.message.SendErrorEmailRequest::parseFrom,
    com.ywt.gapi.Result::parseFrom);

    private static final StubMethodDescriptor sendErrorEmailProxyAsyncMethod = new StubMethodDescriptor("sendErrorEmailAsync",
    com.ywt.gapi.message.SendErrorEmailRequest.class, com.ywt.gapi.Result.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.message.SendErrorEmailRequest::parseFrom,
    com.ywt.gapi.Result::parseFrom);





    public static class EmailServiceStub implements EmailService{
        private final Invoker<EmailService> invoker;

        public EmailServiceStub(Invoker<EmailService> invoker) {
            this.invoker = invoker;
        }

            /**
         * <pre>
         *  调用终端
         * </pre>
         */
        @Override
        public com.ywt.gapi.message.SendMerchantEmailResponse sendMerchantEmail(com.ywt.gapi.message.SendMerchantEmailRequest request){
            return StubInvocationUtil.unaryCall(invoker, sendMerchantEmailMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.message.SendMerchantEmailResponse> sendMerchantEmailAsync(com.ywt.gapi.message.SendMerchantEmailRequest request){
            return StubInvocationUtil.unaryCall(invoker, sendMerchantEmailAsyncMethod, request);
        }

            /**
         * <pre>
         *  调用终端
         * </pre>
         */
        @Override
        public void sendMerchantEmail(com.ywt.gapi.message.SendMerchantEmailRequest request, StreamObserver<com.ywt.gapi.message.SendMerchantEmailResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, sendMerchantEmailMethod , request, responseObserver);
        }
            /**
         * <pre>
         *  调用服务名称
         * </pre>
         */
        @Override
        public com.ywt.gapi.Result sendErrorEmail(com.ywt.gapi.message.SendErrorEmailRequest request){
            return StubInvocationUtil.unaryCall(invoker, sendErrorEmailMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.Result> sendErrorEmailAsync(com.ywt.gapi.message.SendErrorEmailRequest request){
            return StubInvocationUtil.unaryCall(invoker, sendErrorEmailAsyncMethod, request);
        }

            /**
         * <pre>
         *  调用服务名称
         * </pre>
         */
        @Override
        public void sendErrorEmail(com.ywt.gapi.message.SendErrorEmailRequest request, StreamObserver<com.ywt.gapi.Result> responseObserver){
            StubInvocationUtil.unaryCall(invoker, sendErrorEmailMethod , request, responseObserver);
        }



    }

    public static abstract class EmailServiceImplBase implements EmailService, ServerService<EmailService> {

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

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/sendMerchantEmail" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/sendMerchantEmailAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/sendErrorEmail" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/sendErrorEmailAsync" );

            BiConsumer<com.ywt.gapi.message.SendMerchantEmailRequest, StreamObserver<com.ywt.gapi.message.SendMerchantEmailResponse>> sendMerchantEmailFunc = this::sendMerchantEmail;
            handlers.put(sendMerchantEmailMethod.getMethodName(), new UnaryStubMethodHandler<>(sendMerchantEmailFunc));
            BiConsumer<com.ywt.gapi.message.SendMerchantEmailRequest, StreamObserver<com.ywt.gapi.message.SendMerchantEmailResponse>> sendMerchantEmailAsyncFunc = syncToAsync(this::sendMerchantEmail);
            handlers.put(sendMerchantEmailProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(sendMerchantEmailAsyncFunc));
            BiConsumer<com.ywt.gapi.message.SendErrorEmailRequest, StreamObserver<com.ywt.gapi.Result>> sendErrorEmailFunc = this::sendErrorEmail;
            handlers.put(sendErrorEmailMethod.getMethodName(), new UnaryStubMethodHandler<>(sendErrorEmailFunc));
            BiConsumer<com.ywt.gapi.message.SendErrorEmailRequest, StreamObserver<com.ywt.gapi.Result>> sendErrorEmailAsyncFunc = syncToAsync(this::sendErrorEmail);
            handlers.put(sendErrorEmailProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(sendErrorEmailAsyncFunc));




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


        @Override
        public com.ywt.gapi.message.SendMerchantEmailResponse sendMerchantEmail(com.ywt.gapi.message.SendMerchantEmailRequest request){
            throw unimplementedMethodException(sendMerchantEmailMethod);
        }

        @Override
        public com.ywt.gapi.Result sendErrorEmail(com.ywt.gapi.message.SendErrorEmailRequest request){
            throw unimplementedMethodException(sendErrorEmailMethod);
        }





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

}
