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

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

    public static final String SERVICE_NAME = FileService.SERVICE_NAME;

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

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

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

    /**
         * <pre>
         * 文件名
         * </pre>
         */
    private static final StubMethodDescriptor uploadFileMethod = new StubMethodDescriptor("uploadFile",
    com.ywt.gapi.base.file.UploadFileRequest.class, com.ywt.gapi.base.file.UploadFileResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.UploadFileRequest::parseFrom,
    com.ywt.gapi.base.file.UploadFileResponse::parseFrom);

    private static final StubMethodDescriptor uploadFileAsyncMethod = new StubMethodDescriptor("uploadFile",
    com.ywt.gapi.base.file.UploadFileRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.UploadFileRequest::parseFrom,
    com.ywt.gapi.base.file.UploadFileResponse::parseFrom);

    private static final StubMethodDescriptor uploadFileProxyAsyncMethod = new StubMethodDescriptor("uploadFileAsync",
    com.ywt.gapi.base.file.UploadFileRequest.class, com.ywt.gapi.base.file.UploadFileResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.UploadFileRequest::parseFrom,
    com.ywt.gapi.base.file.UploadFileResponse::parseFrom);

    /**
         * <pre>
         * 路径
         * </pre>
         */
    private static final StubMethodDescriptor uploadImageMethod = new StubMethodDescriptor("uploadImage",
    com.ywt.gapi.base.file.UploadImageRequest.class, com.ywt.gapi.base.file.UploadImageResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.UploadImageRequest::parseFrom,
    com.ywt.gapi.base.file.UploadImageResponse::parseFrom);

    private static final StubMethodDescriptor uploadImageAsyncMethod = new StubMethodDescriptor("uploadImage",
    com.ywt.gapi.base.file.UploadImageRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.UploadImageRequest::parseFrom,
    com.ywt.gapi.base.file.UploadImageResponse::parseFrom);

    private static final StubMethodDescriptor uploadImageProxyAsyncMethod = new StubMethodDescriptor("uploadImageAsync",
    com.ywt.gapi.base.file.UploadImageRequest.class, com.ywt.gapi.base.file.UploadImageResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.UploadImageRequest::parseFrom,
    com.ywt.gapi.base.file.UploadImageResponse::parseFrom);

    /**
         * <pre>
         * 文件流
         * </pre>
         */
    private static final StubMethodDescriptor readFileMethod = new StubMethodDescriptor("readFile",
    com.ywt.gapi.base.file.ReadFileRequest.class, com.ywt.gapi.base.file.ReadFileResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.ReadFileRequest::parseFrom,
    com.ywt.gapi.base.file.ReadFileResponse::parseFrom);

    private static final StubMethodDescriptor readFileAsyncMethod = new StubMethodDescriptor("readFile",
    com.ywt.gapi.base.file.ReadFileRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.ReadFileRequest::parseFrom,
    com.ywt.gapi.base.file.ReadFileResponse::parseFrom);

    private static final StubMethodDescriptor readFileProxyAsyncMethod = new StubMethodDescriptor("readFileAsync",
    com.ywt.gapi.base.file.ReadFileRequest.class, com.ywt.gapi.base.file.ReadFileResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.ReadFileRequest::parseFrom,
    com.ywt.gapi.base.file.ReadFileResponse::parseFrom);

    /**
         * <pre>
         * bucket，不传默认为 ywt-files
         * </pre>
         */
    private static final StubMethodDescriptor readFileFromBucketMethod = new StubMethodDescriptor("readFileFromBucket",
    com.ywt.gapi.base.file.ReadFileFromBucketRequest.class, com.ywt.gapi.base.file.ReadFileResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.ReadFileFromBucketRequest::parseFrom,
    com.ywt.gapi.base.file.ReadFileResponse::parseFrom);

    private static final StubMethodDescriptor readFileFromBucketAsyncMethod = new StubMethodDescriptor("readFileFromBucket",
    com.ywt.gapi.base.file.ReadFileFromBucketRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.ReadFileFromBucketRequest::parseFrom,
    com.ywt.gapi.base.file.ReadFileResponse::parseFrom);

    private static final StubMethodDescriptor readFileFromBucketProxyAsyncMethod = new StubMethodDescriptor("readFileFromBucketAsync",
    com.ywt.gapi.base.file.ReadFileFromBucketRequest.class, com.ywt.gapi.base.file.ReadFileResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.base.file.ReadFileFromBucketRequest::parseFrom,
    com.ywt.gapi.base.file.ReadFileResponse::parseFrom);





    public static class FileServiceStub implements FileService{
        private final Invoker<FileService> invoker;

        public FileServiceStub(Invoker<FileService> invoker) {
            this.invoker = invoker;
        }

            /**
         * <pre>
         * 文件名
         * </pre>
         */
        @Override
        public com.ywt.gapi.base.file.UploadFileResponse uploadFile(com.ywt.gapi.base.file.UploadFileRequest request){
            return StubInvocationUtil.unaryCall(invoker, uploadFileMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.base.file.UploadFileResponse> uploadFileAsync(com.ywt.gapi.base.file.UploadFileRequest request){
            return StubInvocationUtil.unaryCall(invoker, uploadFileAsyncMethod, request);
        }

            /**
         * <pre>
         * 文件名
         * </pre>
         */
        @Override
        public void uploadFile(com.ywt.gapi.base.file.UploadFileRequest request, StreamObserver<com.ywt.gapi.base.file.UploadFileResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, uploadFileMethod , request, responseObserver);
        }
            /**
         * <pre>
         * 路径
         * </pre>
         */
        @Override
        public com.ywt.gapi.base.file.UploadImageResponse uploadImage(com.ywt.gapi.base.file.UploadImageRequest request){
            return StubInvocationUtil.unaryCall(invoker, uploadImageMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.base.file.UploadImageResponse> uploadImageAsync(com.ywt.gapi.base.file.UploadImageRequest request){
            return StubInvocationUtil.unaryCall(invoker, uploadImageAsyncMethod, request);
        }

            /**
         * <pre>
         * 路径
         * </pre>
         */
        @Override
        public void uploadImage(com.ywt.gapi.base.file.UploadImageRequest request, StreamObserver<com.ywt.gapi.base.file.UploadImageResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, uploadImageMethod , request, responseObserver);
        }
            /**
         * <pre>
         * 文件流
         * </pre>
         */
        @Override
        public com.ywt.gapi.base.file.ReadFileResponse readFile(com.ywt.gapi.base.file.ReadFileRequest request){
            return StubInvocationUtil.unaryCall(invoker, readFileMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.base.file.ReadFileResponse> readFileAsync(com.ywt.gapi.base.file.ReadFileRequest request){
            return StubInvocationUtil.unaryCall(invoker, readFileAsyncMethod, request);
        }

            /**
         * <pre>
         * 文件流
         * </pre>
         */
        @Override
        public void readFile(com.ywt.gapi.base.file.ReadFileRequest request, StreamObserver<com.ywt.gapi.base.file.ReadFileResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, readFileMethod , request, responseObserver);
        }
            /**
         * <pre>
         * bucket，不传默认为 ywt-files
         * </pre>
         */
        @Override
        public com.ywt.gapi.base.file.ReadFileResponse readFileFromBucket(com.ywt.gapi.base.file.ReadFileFromBucketRequest request){
            return StubInvocationUtil.unaryCall(invoker, readFileFromBucketMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.base.file.ReadFileResponse> readFileFromBucketAsync(com.ywt.gapi.base.file.ReadFileFromBucketRequest request){
            return StubInvocationUtil.unaryCall(invoker, readFileFromBucketAsyncMethod, request);
        }

            /**
         * <pre>
         * bucket，不传默认为 ywt-files
         * </pre>
         */
        @Override
        public void readFileFromBucket(com.ywt.gapi.base.file.ReadFileFromBucketRequest request, StreamObserver<com.ywt.gapi.base.file.ReadFileResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, readFileFromBucketMethod , request, responseObserver);
        }



    }

    public static abstract class FileServiceImplBase implements FileService, ServerService<FileService> {

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

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/uploadFile" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/uploadFileAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/uploadImage" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/uploadImageAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/readFile" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/readFileAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/readFileFromBucket" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/readFileFromBucketAsync" );

            BiConsumer<com.ywt.gapi.base.file.UploadFileRequest, StreamObserver<com.ywt.gapi.base.file.UploadFileResponse>> uploadFileFunc = this::uploadFile;
            handlers.put(uploadFileMethod.getMethodName(), new UnaryStubMethodHandler<>(uploadFileFunc));
            BiConsumer<com.ywt.gapi.base.file.UploadFileRequest, StreamObserver<com.ywt.gapi.base.file.UploadFileResponse>> uploadFileAsyncFunc = syncToAsync(this::uploadFile);
            handlers.put(uploadFileProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(uploadFileAsyncFunc));
            BiConsumer<com.ywt.gapi.base.file.UploadImageRequest, StreamObserver<com.ywt.gapi.base.file.UploadImageResponse>> uploadImageFunc = this::uploadImage;
            handlers.put(uploadImageMethod.getMethodName(), new UnaryStubMethodHandler<>(uploadImageFunc));
            BiConsumer<com.ywt.gapi.base.file.UploadImageRequest, StreamObserver<com.ywt.gapi.base.file.UploadImageResponse>> uploadImageAsyncFunc = syncToAsync(this::uploadImage);
            handlers.put(uploadImageProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(uploadImageAsyncFunc));
            BiConsumer<com.ywt.gapi.base.file.ReadFileRequest, StreamObserver<com.ywt.gapi.base.file.ReadFileResponse>> readFileFunc = this::readFile;
            handlers.put(readFileMethod.getMethodName(), new UnaryStubMethodHandler<>(readFileFunc));
            BiConsumer<com.ywt.gapi.base.file.ReadFileRequest, StreamObserver<com.ywt.gapi.base.file.ReadFileResponse>> readFileAsyncFunc = syncToAsync(this::readFile);
            handlers.put(readFileProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(readFileAsyncFunc));
            BiConsumer<com.ywt.gapi.base.file.ReadFileFromBucketRequest, StreamObserver<com.ywt.gapi.base.file.ReadFileResponse>> readFileFromBucketFunc = this::readFileFromBucket;
            handlers.put(readFileFromBucketMethod.getMethodName(), new UnaryStubMethodHandler<>(readFileFromBucketFunc));
            BiConsumer<com.ywt.gapi.base.file.ReadFileFromBucketRequest, StreamObserver<com.ywt.gapi.base.file.ReadFileResponse>> readFileFromBucketAsyncFunc = syncToAsync(this::readFileFromBucket);
            handlers.put(readFileFromBucketProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(readFileFromBucketAsyncFunc));




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


        @Override
        public com.ywt.gapi.base.file.UploadFileResponse uploadFile(com.ywt.gapi.base.file.UploadFileRequest request){
            throw unimplementedMethodException(uploadFileMethod);
        }

        @Override
        public com.ywt.gapi.base.file.UploadImageResponse uploadImage(com.ywt.gapi.base.file.UploadImageRequest request){
            throw unimplementedMethodException(uploadImageMethod);
        }

        @Override
        public com.ywt.gapi.base.file.ReadFileResponse readFile(com.ywt.gapi.base.file.ReadFileRequest request){
            throw unimplementedMethodException(readFileMethod);
        }

        @Override
        public com.ywt.gapi.base.file.ReadFileResponse readFileFromBucket(com.ywt.gapi.base.file.ReadFileFromBucketRequest request){
            throw unimplementedMethodException(readFileFromBucketMethod);
        }





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

}
