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

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

    public static final String SERVICE_NAME = UserService.SERVICE_NAME;

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

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

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

    private static final StubMethodDescriptor findByUserIdMethod = new StubMethodDescriptor("findByUserId",
    UserRequest.class, UserResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), UserRequest::parseFrom,
    UserResponse::parseFrom);

    private static final StubMethodDescriptor findByUserIdAsyncMethod = new StubMethodDescriptor("findByUserId",
    UserRequest.class, CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), UserRequest::parseFrom,
    UserResponse::parseFrom);

    private static final StubMethodDescriptor findByUserIdProxyAsyncMethod = new StubMethodDescriptor("findByUserIdAsync",
    UserRequest.class, UserResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), UserRequest::parseFrom,
    UserResponse::parseFrom);

    private static final StubMethodDescriptor getSnsUserInfoMethod = new StubMethodDescriptor("getSnsUserInfo",
    GetSnsUserInfoRequest.class, com.ywt.gapi.user.GetSnsUserInfoResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), GetSnsUserInfoRequest::parseFrom,
    com.ywt.gapi.user.GetSnsUserInfoResponse::parseFrom);

    private static final StubMethodDescriptor getSnsUserInfoAsyncMethod = new StubMethodDescriptor("getSnsUserInfo",
    GetSnsUserInfoRequest.class, CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), GetSnsUserInfoRequest::parseFrom,
    com.ywt.gapi.user.GetSnsUserInfoResponse::parseFrom);

    private static final StubMethodDescriptor getSnsUserInfoProxyAsyncMethod = new StubMethodDescriptor("getSnsUserInfoAsync",
    GetSnsUserInfoRequest.class, com.ywt.gapi.user.GetSnsUserInfoResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), GetSnsUserInfoRequest::parseFrom,
    com.ywt.gapi.user.GetSnsUserInfoResponse::parseFrom);





    public static class UserServiceStub implements UserService{
        private final Invoker<UserService> invoker;

        public UserServiceStub(Invoker<UserService> invoker) {
            this.invoker = invoker;
        }

        @Override
        public UserResponse findByUserId(UserRequest request){
            return StubInvocationUtil.unaryCall(invoker, findByUserIdMethod, request);
        }

        public CompletableFuture<UserResponse> findByUserIdAsync(UserRequest request){
            return StubInvocationUtil.unaryCall(invoker, findByUserIdAsyncMethod, request);
        }

        @Override
        public void findByUserId(UserRequest request, StreamObserver<UserResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, findByUserIdMethod , request, responseObserver);
        }
        @Override
        public com.ywt.gapi.user.GetSnsUserInfoResponse getSnsUserInfo(GetSnsUserInfoRequest request){
            return StubInvocationUtil.unaryCall(invoker, getSnsUserInfoMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.user.GetSnsUserInfoResponse> getSnsUserInfoAsync(GetSnsUserInfoRequest request){
            return StubInvocationUtil.unaryCall(invoker, getSnsUserInfoAsyncMethod, request);
        }

        @Override
        public void getSnsUserInfo(GetSnsUserInfoRequest request, StreamObserver<com.ywt.gapi.user.GetSnsUserInfoResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, getSnsUserInfoMethod , request, responseObserver);
        }



    }

    public static abstract class UserServiceImplBase implements UserService, ServerService<UserService> {

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

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/findByUserId" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/findByUserIdAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/getSnsUserInfo" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/getSnsUserInfoAsync" );

            BiConsumer<UserRequest, StreamObserver<UserResponse>> findByUserIdFunc = this::findByUserId;
            handlers.put(findByUserIdMethod.getMethodName(), new UnaryStubMethodHandler<>(findByUserIdFunc));
            BiConsumer<UserRequest, StreamObserver<UserResponse>> findByUserIdAsyncFunc = syncToAsync(this::findByUserId);
            handlers.put(findByUserIdProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(findByUserIdAsyncFunc));
            BiConsumer<GetSnsUserInfoRequest, StreamObserver<com.ywt.gapi.user.GetSnsUserInfoResponse>> getSnsUserInfoFunc = this::getSnsUserInfo;
            handlers.put(getSnsUserInfoMethod.getMethodName(), new UnaryStubMethodHandler<>(getSnsUserInfoFunc));
            BiConsumer<GetSnsUserInfoRequest, StreamObserver<com.ywt.gapi.user.GetSnsUserInfoResponse>> getSnsUserInfoAsyncFunc = syncToAsync(this::getSnsUserInfo);
            handlers.put(getSnsUserInfoProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(getSnsUserInfoAsyncFunc));




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


        @Override
        public UserResponse findByUserId(UserRequest request){
            throw unimplementedMethodException(findByUserIdMethod);
        }

        @Override
        public com.ywt.gapi.user.GetSnsUserInfoResponse getSnsUserInfo(GetSnsUserInfoRequest request){
            throw unimplementedMethodException(getSnsUserInfoMethod);
        }





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

}
