#!/usr/bin/env python
#
# Copyright (c) 2016 Cisco and/or its affiliates.
# Licensed 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.

import os, util
from string import Template

jvpp_ifc_template = Template("""
package $base_package;


/**
 * <p>Java representation of vpe.api.
 * <br>It was generated by jvpp_impl_gen.py based on $inputfile
 * <br>(python representation of vpe.api generated by vppapigen).
 */
public interface JVpp extends java.lang.AutoCloseable {

    /**
     * Generic connect with $base_package.callback.JVppCallback callback handler
     * providing connecting to VPP
     *
     * @param callback JVppCallback instance providing callback handling
     *
     * @throws java.io.IOException if connection cannot be initiated
     */
    void connect($base_package.callback.JVppCallback callback) throws java.io.IOException;

    /**
     * Generic dispatch method for sending requests to VPP
     *
     * @throws org.openvpp.jvpp.VppInvocationException if send request had failed
     */
    int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException;

    @Override
    void close();

$methods
}
""")

jvpp_impl_template = Template("""
package $base_package;

/**
 * <p>Default implementation of JVpp interface.
 * <br>It was generated by jvpp_impl_gen.py based on $inputfile
 * <br>(python representation of vpe.api generated by vppapigen).
 */
public final class JVppImpl implements $base_package.JVpp {

    private final $base_package.VppConnection connection;

    public JVppImpl(final $base_package.VppConnection connection) {
        this.connection = java.util.Objects.requireNonNull(connection,"Connection is null");
    }

    @Override
    public void connect($base_package.callback.JVppCallback callback) throws java.io.IOException {
        connection.connect(callback);
    }

    @Override
    public void close() {
        connection.close();
    }

    @Override
    public int send($base_package.$dto_package.JVppRequest request)  throws org.openvpp.jvpp.VppInvocationException {
        return request.send(this);
    }

$methods
}
""")

method_template = Template("""    int $name($base_package.$dto_package.$request request) throws org.openvpp.jvpp.VppInvocationException;""")
method_native_template = Template(
    """    private static native int ${name}0($base_package.$dto_package.$request request);""")
method_impl_template = Template("""    public final int $name($base_package.$dto_package.$request request) throws org.openvpp.jvpp.VppInvocationException {
        java.util.Objects.requireNonNull(request,"Null request object");
        connection.checkActive();
        int result=${name}0(request);
        if(result<0){
            throw new org.openvpp.jvpp.VppInvocationException("${name}",result);
        }
        return result;
    }
""")

no_arg_method_template = Template("""    int $name() throws org.openvpp.jvpp.VppInvocationException;""")
no_arg_method_native_template = Template("""    private static native int ${name}0() throws org.openvpp.jvpp.VppInvocationException;""")
no_arg_method_impl_template = Template("""    public final int $name() throws org.openvpp.jvpp.VppInvocationException {
        connection.checkActive();
        int result=${name}0();
        if(result<0){
            throw new org.openvpp.jvpp.VppInvocationException("${name}",result);
        }
        return result;
    }
""")


def generate_jvpp(func_list, base_package, dto_package, inputfile):
    """ Generates JVpp interface and JNI implementation """
    print "Generating JVpp"

    methods = []
    methods_impl = []
    for func in func_list:

        # Skip structures that are used only as notifications
        if util.is_just_notification(func['name']) or util.is_ignored(func['name']):
            continue

        camel_case_name = util.underscore_to_camelcase(func['name'])
        camel_case_name_upper = util.underscore_to_camelcase_upper(func['name'])
        if util.is_reply(camel_case_name):
            continue

        if len(func['args']) == 0:
            methods.append(no_arg_method_template.substitute(name=camel_case_name,
                                                             base_package=base_package,
                                                             dto_package=dto_package))
            methods_impl.append(
                no_arg_method_native_template.substitute(name=camel_case_name,
                                                         base_package=base_package,
                                                         dto_package=dto_package))
            methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name,
                                                                       base_package=base_package,
                                                                       dto_package=dto_package))
        else:
            methods.append(method_template.substitute(name=camel_case_name,
                                                      request=camel_case_name_upper,
                                                      base_package=base_package,
                                                      dto_package=dto_package))
            methods_impl.append(method_native_template.substitute(name=camel_case_name,
                                                                  request=camel_case_name_upper,
                                                                  base_package=base_package,
                                                                  dto_package=dto_package))
            methods_impl.append(method_impl_template.substitute(name=camel_case_name,
                                                                request=camel_case_name_upper,
                                                                base_package=base_package,
                                                                dto_package=dto_package))

    jvpp_file = open("JVpp.java", 'w')
    jvpp_file.write(
        jvpp_ifc_template.substitute(inputfile=inputfile,
                                     methods="\n".join(methods),
                                     base_package=base_package,
                                     dto_package=dto_package))
    jvpp_file.flush()
    jvpp_file.close()

    jvpp_file = open("JVppImpl.java", 'w')
    jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile,
                                                  methods="\n".join(methods_impl),
                                                  base_package=base_package,
                                                  dto_package=dto_package))
    jvpp_file.flush()
    jvpp_file.close()