<%--
    Copyright (C) 2024 The Qt Company Ltd.

    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 org.qtproject.qt.qtjenny.JteData
@import org.qtproject.qt.qtjenny.Constants
@import org.qtproject.qt.qtjenny.HandyHelper
@import org.qtproject.qt.qtjenny.MethodOverloadResolver.MethodRecord
@import javax.lang.model.type.TypeKind
@import javax.lang.model.element.Modifier

@param jteData: JteData

!{
val rawParams = jteData.handyHelper.getJavaMethodParam(jteData.method!!.method)
val params = rawParams.split(",").map { it.trim() }.filter { it.isNotEmpty() }

val javaCode = if (params.isEmpty()) {
    "null"
} else {
    val parsedParams = params.mapNotNull {
        val parts = it.split(" ")
        if (parts.size == 2) {
            val type = parts[0]
            val name = parts[1]
            type to name
        } else null
    }

    val objectArrayCode = parsedParams.joinToString(", ") { (type, name) ->
        when (type) {
            "int" -> "Integer.valueOf($name)"
            "long" -> "Long.valueOf($name)"
            "double" -> "Double.valueOf($name)"
            "float" -> "Float.valueOf($name)"
            "boolean" -> "Boolean.valueOf($name)"
            "char" -> "Character.valueOf($name)"
            "byte" -> "Byte.valueOf($name)"
            "short" -> "Short.valueOf($name)"
            else -> name
        }
    }
    "new Object[] { $objectArrayCode }"
}

val superInvocationCode = if (params.isEmpty()) {
    ""
} else {
    val parsedParams = params.mapNotNull {
        val parts = it.split(" ")
        if (parts.size == 2) {
            parts[1]
        } else null
    }

    val objectArrayCode = parsedParams.joinToString(", ")
    "${objectArrayCode}";
}
}

!{
fun unboxFromObject(returnType: String, objectName: String): String {
    return when (returnType) {
        "int" -> "((Integer) $objectName).intValue()"
        "boolean" -> "((Boolean) $objectName).booleanValue()"
        "double" -> "((Double) $objectName).doubleValue()"
        "long" -> "((Long) $objectName).longValue()"
        "float" -> "((Float) $objectName).floatValue()"
        "short" -> "((Short) $objectName).shortValue()"
        "byte" -> "((Byte) $objectName).byteValue()"
        "char" -> "((Character) $objectName).charValue()"
        else -> "$objectName" // No unboxing needed
    }
}
}

!{val classParam = if (jteData.rawStaticMod != "") "FULL_CLASS_NAME," else ""}
!{val thizParam = if (jteData.rawStaticMod != "") "QJniObject::" else "m_jniObject."}

@if (!jteData.method!!.method.modifiers.contains(Modifier.FINAL))

    public ${jteData.method!!.method.returnType.toString()} ${jteData.method!!.method.simpleName.toString()}(${jteData.handyHelper.getJavaMethodParam(
                            jteData.method!!.method
                        )}) {
    @if (jteData.method!!.method.returnType.toString() != "void")
    Object res = mHandler.invoke("${jteData.method!!.method.simpleName.toString()}${jteData.method!!.resolvedPostFix}", ${javaCode});
    if (res != null)
        ${jteData.returnStatement}${unboxFromObject(jteData.method!!.method.returnType.toString(), "res")};
    @else
        ${jteData.returnStatement}${unboxFromObject(jteData.method!!.method.returnType.toString(), "mHandler.invoke(\"${jteData.method!!.method.simpleName.toString()}${jteData.method!!.resolvedPostFix}\", ${javaCode})")};
    @endif
        ${jteData.returnStatement}super.${jteData.method!!.method.simpleName.toString()}(${superInvocationCode});
    }
@endif

