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

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

    public static final String SERVICE_NAME = OperationScheduleService.SERVICE_NAME;

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

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

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

    private static final StubMethodDescriptor queryScheduleListMethod = new StubMethodDescriptor("queryScheduleList",
    com.ywt.gapi.operation_schedule.QueryScheduleListRequest.class, com.ywt.gapi.operation_schedule.QueryScheduleListResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.operation_schedule.QueryScheduleListRequest::parseFrom,
    com.ywt.gapi.operation_schedule.QueryScheduleListResponse::parseFrom);

    private static final StubMethodDescriptor queryScheduleListAsyncMethod = new StubMethodDescriptor("queryScheduleList",
    com.ywt.gapi.operation_schedule.QueryScheduleListRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.operation_schedule.QueryScheduleListRequest::parseFrom,
    com.ywt.gapi.operation_schedule.QueryScheduleListResponse::parseFrom);

    private static final StubMethodDescriptor queryScheduleListProxyAsyncMethod = new StubMethodDescriptor("queryScheduleListAsync",
    com.ywt.gapi.operation_schedule.QueryScheduleListRequest.class, com.ywt.gapi.operation_schedule.QueryScheduleListResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.operation_schedule.QueryScheduleListRequest::parseFrom,
    com.ywt.gapi.operation_schedule.QueryScheduleListResponse::parseFrom);

    private static final StubMethodDescriptor sendOperationScheduleMethod = new StubMethodDescriptor("sendOperationSchedule",
    com.ywt.gapi.operation_schedule.OperationMessageRequest.class, com.ywt.gapi.operation_schedule.OperationMessageResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.operation_schedule.OperationMessageRequest::parseFrom,
    com.ywt.gapi.operation_schedule.OperationMessageResponse::parseFrom);

    private static final StubMethodDescriptor sendOperationScheduleAsyncMethod = new StubMethodDescriptor("sendOperationSchedule",
    com.ywt.gapi.operation_schedule.OperationMessageRequest.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.operation_schedule.OperationMessageRequest::parseFrom,
    com.ywt.gapi.operation_schedule.OperationMessageResponse::parseFrom);

    private static final StubMethodDescriptor sendOperationScheduleProxyAsyncMethod = new StubMethodDescriptor("sendOperationScheduleAsync",
    com.ywt.gapi.operation_schedule.OperationMessageRequest.class, com.ywt.gapi.operation_schedule.OperationMessageResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), com.ywt.gapi.operation_schedule.OperationMessageRequest::parseFrom,
    com.ywt.gapi.operation_schedule.OperationMessageResponse::parseFrom);





    public static class OperationScheduleServiceStub implements OperationScheduleService{
        private final Invoker<OperationScheduleService> invoker;

        public OperationScheduleServiceStub(Invoker<OperationScheduleService> invoker) {
            this.invoker = invoker;
        }

        @Override
        public com.ywt.gapi.operation_schedule.QueryScheduleListResponse queryScheduleList(com.ywt.gapi.operation_schedule.QueryScheduleListRequest request){
            return StubInvocationUtil.unaryCall(invoker, queryScheduleListMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.operation_schedule.QueryScheduleListResponse> queryScheduleListAsync(com.ywt.gapi.operation_schedule.QueryScheduleListRequest request){
            return StubInvocationUtil.unaryCall(invoker, queryScheduleListAsyncMethod, request);
        }

        @Override
        public void queryScheduleList(com.ywt.gapi.operation_schedule.QueryScheduleListRequest request, StreamObserver<com.ywt.gapi.operation_schedule.QueryScheduleListResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, queryScheduleListMethod , request, responseObserver);
        }
        @Override
        public com.ywt.gapi.operation_schedule.OperationMessageResponse sendOperationSchedule(com.ywt.gapi.operation_schedule.OperationMessageRequest request){
            return StubInvocationUtil.unaryCall(invoker, sendOperationScheduleMethod, request);
        }

        public CompletableFuture<com.ywt.gapi.operation_schedule.OperationMessageResponse> sendOperationScheduleAsync(com.ywt.gapi.operation_schedule.OperationMessageRequest request){
            return StubInvocationUtil.unaryCall(invoker, sendOperationScheduleAsyncMethod, request);
        }

        @Override
        public void sendOperationSchedule(com.ywt.gapi.operation_schedule.OperationMessageRequest request, StreamObserver<com.ywt.gapi.operation_schedule.OperationMessageResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, sendOperationScheduleMethod , request, responseObserver);
        }



    }

    public static abstract class OperationScheduleServiceImplBase implements OperationScheduleService, ServerService<OperationScheduleService> {

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

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/queryScheduleList" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/queryScheduleListAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/sendOperationSchedule" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/sendOperationScheduleAsync" );

            BiConsumer<com.ywt.gapi.operation_schedule.QueryScheduleListRequest, StreamObserver<com.ywt.gapi.operation_schedule.QueryScheduleListResponse>> queryScheduleListFunc = this::queryScheduleList;
            handlers.put(queryScheduleListMethod.getMethodName(), new UnaryStubMethodHandler<>(queryScheduleListFunc));
            BiConsumer<com.ywt.gapi.operation_schedule.QueryScheduleListRequest, StreamObserver<com.ywt.gapi.operation_schedule.QueryScheduleListResponse>> queryScheduleListAsyncFunc = syncToAsync(this::queryScheduleList);
            handlers.put(queryScheduleListProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(queryScheduleListAsyncFunc));
            BiConsumer<com.ywt.gapi.operation_schedule.OperationMessageRequest, StreamObserver<com.ywt.gapi.operation_schedule.OperationMessageResponse>> sendOperationScheduleFunc = this::sendOperationSchedule;
            handlers.put(sendOperationScheduleMethod.getMethodName(), new UnaryStubMethodHandler<>(sendOperationScheduleFunc));
            BiConsumer<com.ywt.gapi.operation_schedule.OperationMessageRequest, StreamObserver<com.ywt.gapi.operation_schedule.OperationMessageResponse>> sendOperationScheduleAsyncFunc = syncToAsync(this::sendOperationSchedule);
            handlers.put(sendOperationScheduleProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(sendOperationScheduleAsyncFunc));




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


        @Override
        public com.ywt.gapi.operation_schedule.QueryScheduleListResponse queryScheduleList(com.ywt.gapi.operation_schedule.QueryScheduleListRequest request){
            throw unimplementedMethodException(queryScheduleListMethod);
        }

        @Override
        public com.ywt.gapi.operation_schedule.OperationMessageResponse sendOperationSchedule(com.ywt.gapi.operation_schedule.OperationMessageRequest request){
            throw unimplementedMethodException(sendOperationScheduleMethod);
        }





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

}
