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

{{#packageName}}
package {{packageName}};
{{/packageName}}

import com.google.protobuf.Message;
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.reactive.handler.ManyToManyMethodHandler;
import org.apache.dubbo.reactive.handler.ManyToOneMethodHandler;
import org.apache.dubbo.reactive.handler.OneToManyMethodHandler;
import org.apache.dubbo.reactive.calls.ReactorClientCalls;
import org.apache.dubbo.reactive.handler.OneToOneMethodHandler;

import org.apache.dubbo.rpc.stub.StubInvoker;
import org.apache.dubbo.rpc.stub.StubMethodHandler;
import org.apache.dubbo.rpc.stub.StubSuppliers;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

public final class {{className}} {

    private {{className}}() {}

    public static final String SERVICE_NAME = {{interfaceClassName}}.SERVICE_NAME;

    private static final StubServiceDescriptor serviceDescriptor = new StubServiceDescriptor(SERVICE_NAME,{{interfaceClassName}}.class);

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

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

{{#unaryMethods}}
    {{#javaDoc}}
        {{{javaDoc}}}
    {{/javaDoc}}
    private static final StubMethodDescriptor {{methodName}}Method = new StubMethodDescriptor("{{originMethodName}}",
        {{inputType}}.class, {{outputType}}.class, MethodDescriptor.RpcType.UNARY,
        obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), {{inputType}}::parseFrom,
        {{outputType}}::parseFrom);
{{/unaryMethods}}

{{#serverStreamingMethods}}
    {{#javaDoc}}
        {{{javaDoc}}}
    {{/javaDoc}}
    private static final StubMethodDescriptor {{methodName}}Method = new StubMethodDescriptor("{{originMethodName}}",
        {{inputType}}.class, {{outputType}}.class, MethodDescriptor.RpcType.SERVER_STREAM,
        obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), {{inputType}}::parseFrom,
        {{outputType}}::parseFrom);
{{/serverStreamingMethods}}

{{#clientStreamingMethods}}
    {{#javaDoc}}
        {{{javaDoc}}}
    {{/javaDoc}}
    private static final StubMethodDescriptor {{methodName}}Method = new StubMethodDescriptor("{{originMethodName}}",
        {{inputType}}.class, {{outputType}}.class, MethodDescriptor.RpcType.CLIENT_STREAM,
        obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), {{inputType}}::parseFrom,
        {{outputType}}::parseFrom);
{{/clientStreamingMethods}}

{{#biStreamingWithoutClientStreamMethods}}
    {{#javaDoc}}
        {{{javaDoc}}}
    {{/javaDoc}}
    private static final StubMethodDescriptor {{methodName}}Method = new StubMethodDescriptor("{{originMethodName}}",
        {{inputType}}.class, {{outputType}}.class, MethodDescriptor.RpcType.BI_STREAM,
        obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), {{inputType}}::parseFrom,
        {{outputType}}::parseFrom);
{{/biStreamingWithoutClientStreamMethods}}

    static{
    {{#unaryMethods}}
        serviceDescriptor.addMethod({{methodName}}Method);
    {{/unaryMethods}}
    {{#serverStreamingMethods}}
        serviceDescriptor.addMethod({{methodName}}Method);
    {{/serverStreamingMethods}}
    {{#clientStreamingMethods}}
        serviceDescriptor.addMethod({{methodName}}Method);
    {{/clientStreamingMethods}}
    {{#biStreamingWithoutClientStreamMethods}}
        serviceDescriptor.addMethod({{methodName}}Method);
    {{/biStreamingWithoutClientStreamMethods}}
    }

    public static class {{interfaceClassName}}Stub implements {{interfaceClassName}}{

        private final Invoker<{{interfaceClassName}}> invoker;

        public {{interfaceClassName}}Stub(Invoker<{{interfaceClassName}}> invoker) {
            this.invoker = invoker;
        }

    {{#methods}}
        {{#javaDoc}}
            {{{javaDoc}}}
        {{/javaDoc}}
        {{#deprecated}}
            @java.lang.Deprecated
        {{/deprecated}}
        public {{#isManyOutput}}Flux{{/isManyOutput}}{{^isManyOutput}}Mono{{/isManyOutput}}<{{outputType}}> {{methodName}}({{#isManyInput}}Flux{{/isManyInput}}{{^isManyInput}}Mono{{/isManyInput}}<{{inputType}}> request) {
            return ReactorClientCalls.{{reactiveCallsMethodName}}(invoker, request, {{methodNameCamelCase}}Method);
        }
    {{/methods}}
    }

    public static abstract class {{interfaceClassName}}ImplBase implements {{interfaceClassName}}, ServerService<{{interfaceClassName}}> {

        @Override
        public final Invoker<{{interfaceClassName}}> getInvoker(URL url) {
            PathResolver pathResolver = url.getOrDefaultFrameworkModel()
            .getExtensionLoader(PathResolver.class)
            .getDefaultExtension();
            Map<String,StubMethodHandler<?, ?>> handlers = new HashMap<>();

            {{#methods}}
                pathResolver.addNativeStub( "/" + SERVICE_NAME + "/{{originMethodName}}");
                // for compatibility
                pathResolver.addNativeStub( "/" + JAVA_SERVICE_NAME + "/{{originMethodName}}");
            {{/methods}}

            {{#unaryMethods}}
                handlers.put({{methodName}}Method.getMethodName(), new OneToOneMethodHandler<>(this::{{methodName}}));
            {{/unaryMethods}}
            {{#serverStreamingMethods}}
                handlers.put({{methodName}}Method.getMethodName(), new OneToManyMethodHandler<>(this::{{methodName}}));
            {{/serverStreamingMethods}}
            {{#clientStreamingMethods}}
                handlers.put({{methodName}}Method.getMethodName(), new ManyToOneMethodHandler<>(this::{{methodName}}));
            {{/clientStreamingMethods}}
            {{#biStreamingWithoutClientStreamMethods}}
                handlers.put({{methodName}}Method.getMethodName(), new ManyToManyMethodHandler<>(this::{{methodName}}));
            {{/biStreamingWithoutClientStreamMethods}}

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

    {{#methods}}
        {{#javaDoc}}
            {{{javaDoc}}}
        {{/javaDoc}}
        {{#deprecated}}
            @java.lang.Deprecated
        {{/deprecated}}
        public {{#isManyOutput}}Flux{{/isManyOutput}}{{^isManyOutput}}Mono{{/isManyOutput}}<{{outputType}}> {{methodName}}({{#isManyInput}}Flux{{/isManyInput}}{{^isManyInput}}Mono{{/isManyInput}}<{{inputType}}> request) {
            throw unimplementedMethodException({{methodName}}Method);
        }
    {{/methods}}

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