package {{ sdk.namespace | caseDot }}.services

import {{ sdk.namespace | caseDot }}.Client
{% if spec.definitions is not empty %}
import {{ sdk.namespace | caseDot }}.models.*
{% endif %}
{% if spec.enums is not empty %}
import {{ sdk.namespace | caseDot }}.enums.*
{% endif %}
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
import {{ sdk.namespace | caseDot }}.extensions.classOf
import okhttp3.Cookie
{% if service.features.location %}
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
{% endif %}
import java.io.File

/**
 * {{ service.description | raw | replace({"\n": "", "\r": ""}) }}
**/
class {{ service.name | caseUcfirst }}(client: Client) : Service(client) {

{% for method in service.methods %}
    /**
     * {{ method.title }}
     *
     * {{ method.description | raw | replace({"\n": "", "\r": ""}) }}
     *
    {%~ for parameter in method.parameters.all %}
     * @param {{ parameter.name | caseCamel }} {{ parameter.description | raw }}
    {%~ endfor %}
     * @return [{{ method | returnType(spec, sdk.namespace | caseDot) | raw }}]
     */
    {%~ if method.parameters.all | reduce((carry, param) => carry or not param.required) %}
    @JvmOverloads
    {%~ endif %}
    @Throws({{ spec.title | caseUcfirst }}Exception::class)
    suspend fun {% if method.responseModel | hasGenericType(spec) %}{{ '<T>' | raw }} {% endif %}{{ method.name | caseCamel }}(
        {%~ for parameter in method.parameters.all %}
        {{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required or parameter.nullable %}? = null{% endif %},
        {%~ endfor %}
        {%~ if method.responseModel | hasGenericType(spec) %}
        nestedType: Class<T>,
        {%~ endif %}
        {%~ if 'multipart/form-data' in method.consumes %}
        onProgress: ((UploadProgress) -> Unit)? = null
        {%~ endif %}
    ): {{ method | returnType(spec, sdk.namespace | caseDot) | raw }} {
        val apiPath = "{{ method.path }}"
        {%~ for parameter in method.parameters.path %}
            .replace("{{ '{' ~ parameter.name | caseCamel ~ '}' }}", {{ parameter.name | caseCamel }}{% if parameter.enumValues is not empty %}.value{% endif %})
        {%~ endfor %}

        val apiParams = mutableMapOf<String, Any?>(
        {%~ for parameter in method.parameters.query | merge(method.parameters.body) %}
            "{{ parameter.name }}" to {{ parameter.name | caseCamel }},
        {%~ endfor %}
        )
        val apiHeaders = mutableMapOf(
        {%~ for key, header in method.headers %}
            "{{ key }}" to "{{ header }}",
        {%~ endfor %}
        )
        {%~ if method.type == 'location' %}
            {{~ include('kotlin/base/requests/location.twig') }}
        {%~ elseif method.type == 'webAuth' %}
            {{~ include('kotlin/base/requests/oauth.twig') }}
        {%~ else %}
        {%~ if method.responseModel %}
        val converter: (Any) -> {{ method | returnType(spec, sdk.namespace | caseDot) | raw }} = {
        {%~ if method.responseModel == 'any' %}
            it
        {%~ else %}
            {{ sdk.namespace | caseDot }}.models.{{ method.responseModel | caseUcfirst }}.from(map = it as Map<String, Any>{% if method.responseModel | hasGenericType(spec) %}, nestedType{% endif %})
        {%~ endif %}
        }
        {%~ endif %}
        {%~ if 'multipart/form-data' in method.consumes %}
            {{~ include('kotlin/base/requests/file.twig') }}
        {%~ else %}
            {{~ include('kotlin/base/requests/api.twig') }}
        {%~ endif %}
        {%~ endif %}
    }
    {%~ if method.responseModel | hasGenericType(spec) %}

    /**
     * {{ method.title }}
     *
     * {{ method.description | raw | replace({"\n": "", "\r": ""}) }}
     *
    {%~ for parameter in method.parameters.all %}
     * @param {{ parameter.name | caseCamel }} {{ parameter.description | raw }}
    {%~ endfor %}
     * @return [{{ method | returnType(spec, sdk.namespace | caseDot) | raw }}]
     */
    {%~ if method.parameters.all | reduce((carry, param) => carry or not param.required) %}
    @JvmOverloads
    {%~ endif %}
    @Throws({{ spec.title | caseUcfirst }}Exception::class)
    suspend fun {{ method.name | caseCamel }}(
        {%~ for parameter in method.parameters.all %}
        {{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required or parameter.nullable %}? = null{% endif %},
        {%~ endfor %}
        {%~ if 'multipart/form-data' in method.consumes %}
        onProgress: ((UploadProgress) -> Unit)? = null
        {%~ endif %}
    ): {{ method | returnType(spec, sdk.namespace | caseDot, 'Map<String, Any>') | raw }} = {{ method.name | caseCamel }}(
        {%~ if method.type == "webAuth" %}
        activity,
        {%~ endif %}
        {%~ for parameter in method.parameters.all %}
        {{ parameter.name | caseCamel }},
        {%~ endfor %}
        {%~ if method.responseModel | hasGenericType(spec) %}
        nestedType = classOf(),
        {%~ endif %}
        {%~ if 'multipart/form-data' in method.consumes %}
        onProgress = onProgress
        {%~ endif %}
    )
    {%~ endif %}

    {%~ endfor %}
}