/*
* 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 DubboWeChatMessageServiceTriple {

    public static final String SERVICE_NAME = WeChatMessageService.SERVICE_NAME;

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

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

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

    private static final StubMethodDescriptor sendTemplateMessageMethod = new StubMethodDescriptor("sendTemplateMessage",
    SendTemplateMessageRequest.class, SendTemplateMessageResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), SendTemplateMessageRequest::parseFrom,
    SendTemplateMessageResponse::parseFrom);

    private static final StubMethodDescriptor sendTemplateMessageAsyncMethod = new StubMethodDescriptor("sendTemplateMessage",
    SendTemplateMessageRequest.class, CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), SendTemplateMessageRequest::parseFrom,
    SendTemplateMessageResponse::parseFrom);

    private static final StubMethodDescriptor sendTemplateMessageProxyAsyncMethod = new StubMethodDescriptor("sendTemplateMessageAsync",
    SendTemplateMessageRequest.class, SendTemplateMessageResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), SendTemplateMessageRequest::parseFrom,
    SendTemplateMessageResponse::parseFrom);

    private static final StubMethodDescriptor sendMPSubscribeMessageMethod = new StubMethodDescriptor("sendMPSubscribeMessage",
    SendMPSubscribeMessageRequest.class, SendMPSubscribeMessageResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), SendMPSubscribeMessageRequest::parseFrom,
    SendMPSubscribeMessageResponse::parseFrom);

    private static final StubMethodDescriptor sendMPSubscribeMessageAsyncMethod = new StubMethodDescriptor("sendMPSubscribeMessage",
    SendMPSubscribeMessageRequest.class, CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), SendMPSubscribeMessageRequest::parseFrom,
    SendMPSubscribeMessageResponse::parseFrom);

    private static final StubMethodDescriptor sendMPSubscribeMessageProxyAsyncMethod = new StubMethodDescriptor("sendMPSubscribeMessageAsync",
    SendMPSubscribeMessageRequest.class, SendMPSubscribeMessageResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), SendMPSubscribeMessageRequest::parseFrom,
    SendMPSubscribeMessageResponse::parseFrom);





    public static class WeChatMessageServiceStub implements WeChatMessageService{
        private final Invoker<WeChatMessageService> invoker;

        public WeChatMessageServiceStub(Invoker<WeChatMessageService> invoker) {
            this.invoker = invoker;
        }

        @Override
        public SendTemplateMessageResponse sendTemplateMessage(SendTemplateMessageRequest request){
            return StubInvocationUtil.unaryCall(invoker, sendTemplateMessageMethod, request);
        }

        public CompletableFuture<SendTemplateMessageResponse> sendTemplateMessageAsync(SendTemplateMessageRequest request){
            return StubInvocationUtil.unaryCall(invoker, sendTemplateMessageAsyncMethod, request);
        }

        @Override
        public void sendTemplateMessage(SendTemplateMessageRequest request, StreamObserver<SendTemplateMessageResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, sendTemplateMessageMethod , request, responseObserver);
        }
        @Override
        public SendMPSubscribeMessageResponse sendMPSubscribeMessage(SendMPSubscribeMessageRequest request){
            return StubInvocationUtil.unaryCall(invoker, sendMPSubscribeMessageMethod, request);
        }

        public CompletableFuture<SendMPSubscribeMessageResponse> sendMPSubscribeMessageAsync(SendMPSubscribeMessageRequest request){
            return StubInvocationUtil.unaryCall(invoker, sendMPSubscribeMessageAsyncMethod, request);
        }

        @Override
        public void sendMPSubscribeMessage(SendMPSubscribeMessageRequest request, StreamObserver<SendMPSubscribeMessageResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, sendMPSubscribeMessageMethod , request, responseObserver);
        }



    }

    public static abstract class WeChatMessageServiceImplBase implements WeChatMessageService, ServerService<WeChatMessageService> {

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

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/sendTemplateMessage" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/sendTemplateMessageAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/sendMPSubscribeMessage" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/sendMPSubscribeMessageAsync" );

            BiConsumer<SendTemplateMessageRequest, StreamObserver<SendTemplateMessageResponse>> sendTemplateMessageFunc = this::sendTemplateMessage;
            handlers.put(sendTemplateMessageMethod.getMethodName(), new UnaryStubMethodHandler<>(sendTemplateMessageFunc));
            BiConsumer<SendTemplateMessageRequest, StreamObserver<SendTemplateMessageResponse>> sendTemplateMessageAsyncFunc = syncToAsync(this::sendTemplateMessage);
            handlers.put(sendTemplateMessageProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(sendTemplateMessageAsyncFunc));
            BiConsumer<SendMPSubscribeMessageRequest, StreamObserver<SendMPSubscribeMessageResponse>> sendMPSubscribeMessageFunc = this::sendMPSubscribeMessage;
            handlers.put(sendMPSubscribeMessageMethod.getMethodName(), new UnaryStubMethodHandler<>(sendMPSubscribeMessageFunc));
            BiConsumer<SendMPSubscribeMessageRequest, StreamObserver<SendMPSubscribeMessageResponse>> sendMPSubscribeMessageAsyncFunc = syncToAsync(this::sendMPSubscribeMessage);
            handlers.put(sendMPSubscribeMessageProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(sendMPSubscribeMessageAsyncFunc));




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


        @Override
        public SendTemplateMessageResponse sendTemplateMessage(SendTemplateMessageRequest request){
            throw unimplementedMethodException(sendTemplateMessageMethod);
        }

        @Override
        public SendMPSubscribeMessageResponse sendMPSubscribeMessage(SendMPSubscribeMessageRequest request){
            throw unimplementedMethodException(sendMPSubscribeMessageMethod);
        }





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

}
