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

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

    public static final String SERVICE_NAME = OrderCenterService.SERVICE_NAME;

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

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

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

    private static final StubMethodDescriptor createOrderMethod = new StubMethodDescriptor("createOrder",
    CreateOrderRequest.class, CreateOrderResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), CreateOrderRequest::parseFrom,
    CreateOrderResponse::parseFrom);

    private static final StubMethodDescriptor createOrderAsyncMethod = new StubMethodDescriptor("createOrder",
    CreateOrderRequest.class, CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), CreateOrderRequest::parseFrom,
    CreateOrderResponse::parseFrom);

    private static final StubMethodDescriptor createOrderProxyAsyncMethod = new StubMethodDescriptor("createOrderAsync",
    CreateOrderRequest.class, CreateOrderResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), CreateOrderRequest::parseFrom,
    CreateOrderResponse::parseFrom);

    private static final StubMethodDescriptor refundMethod = new StubMethodDescriptor("refund",
    RefundRequest.class, RefundResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), RefundRequest::parseFrom,
    RefundResponse::parseFrom);

    private static final StubMethodDescriptor refundAsyncMethod = new StubMethodDescriptor("refund",
    RefundRequest.class, CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), RefundRequest::parseFrom,
    RefundResponse::parseFrom);

    private static final StubMethodDescriptor refundProxyAsyncMethod = new StubMethodDescriptor("refundAsync",
    RefundRequest.class, RefundResponse.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
    obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), RefundRequest::parseFrom,
    RefundResponse::parseFrom);





    public static class OrderCenterServiceStub implements OrderCenterService{
        private final Invoker<OrderCenterService> invoker;

        public OrderCenterServiceStub(Invoker<OrderCenterService> invoker) {
            this.invoker = invoker;
        }

        @Override
        public CreateOrderResponse createOrder(CreateOrderRequest request){
            return StubInvocationUtil.unaryCall(invoker, createOrderMethod, request);
        }

        public CompletableFuture<CreateOrderResponse> createOrderAsync(CreateOrderRequest request){
            return StubInvocationUtil.unaryCall(invoker, createOrderAsyncMethod, request);
        }

        @Override
        public void createOrder(CreateOrderRequest request, StreamObserver<CreateOrderResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, createOrderMethod , request, responseObserver);
        }
        @Override
        public RefundResponse refund(RefundRequest request){
            return StubInvocationUtil.unaryCall(invoker, refundMethod, request);
        }

        public CompletableFuture<RefundResponse> refundAsync(RefundRequest request){
            return StubInvocationUtil.unaryCall(invoker, refundAsyncMethod, request);
        }

        @Override
        public void refund(RefundRequest request, StreamObserver<RefundResponse> responseObserver){
            StubInvocationUtil.unaryCall(invoker, refundMethod , request, responseObserver);
        }



    }

    public static abstract class OrderCenterServiceImplBase implements OrderCenterService, ServerService<OrderCenterService> {

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

            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/createOrder" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/createOrderAsync" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/refund" );
            pathResolver.addNativeStub( "/" + SERVICE_NAME + "/refundAsync" );

            BiConsumer<CreateOrderRequest, StreamObserver<CreateOrderResponse>> createOrderFunc = this::createOrder;
            handlers.put(createOrderMethod.getMethodName(), new UnaryStubMethodHandler<>(createOrderFunc));
            BiConsumer<CreateOrderRequest, StreamObserver<CreateOrderResponse>> createOrderAsyncFunc = syncToAsync(this::createOrder);
            handlers.put(createOrderProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(createOrderAsyncFunc));
            BiConsumer<RefundRequest, StreamObserver<RefundResponse>> refundFunc = this::refund;
            handlers.put(refundMethod.getMethodName(), new UnaryStubMethodHandler<>(refundFunc));
            BiConsumer<RefundRequest, StreamObserver<RefundResponse>> refundAsyncFunc = syncToAsync(this::refund);
            handlers.put(refundProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(refundAsyncFunc));




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


        @Override
        public CreateOrderResponse createOrder(CreateOrderRequest request){
            throw unimplementedMethodException(createOrderMethod);
        }

        @Override
        public RefundResponse refund(RefundRequest request){
            throw unimplementedMethodException(refundMethod);
        }





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

}
