{{>partial_header}}

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Mime;
using {{packageName}}.Client;
{{#hasImport}}using {{packageName}}.{{modelPackage}};
{{/hasImport}}

namespace {{packageName}}.{{apiPackage}}
{
    {{#operations}}

    /// <summary>
    /// Represents a collection of functions to interact with the API endpoints
    /// </summary>
    {{>visibility}} interface {{interfacePrefix}}{{classname}}Sync : IApiAccessor
    {
        #region Synchronous Operations
        {{#operation}}
        /// <summary>
        /// {{summary}}
        /// </summary>
        /// <remarks>
        /// {{notes}}
        /// </remarks>
        /// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
        {{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
        {{/allParams}}/// <returns>{{#returnType}}{{returnType}}{{/returnType}}</returns>
        {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}} ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

        /// <summary>
        /// {{summary}}
        /// </summary>
        /// <remarks>
        /// {{notes}}
        /// </remarks>
        /// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
        {{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
        {{/allParams}}/// <returns>ApiResponse of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Object(void){{/returnType}}</returns>
        ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}> {{operationId}}WithHttpInfo ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
        {{/operation}}
        #endregion Synchronous Operations
    }

    {{#supportsAsync}}
    /// <summary>
    /// Represents a collection of functions to interact with the API endpoints
    /// </summary>
    {{>visibility}} interface {{interfacePrefix}}{{classname}}Async : IApiAccessor
    {
        #region Asynchronous Operations
        {{#operation}}
        /// <summary>
        /// {{summary}}
        /// </summary>
        /// <remarks>
        /// {{notes}}
        /// </remarks>
        /// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
        {{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
        {{/allParams}}/// <returns>Task of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}</returns>
        {{#returnType}}System.Threading.Tasks.Task<{{{returnType}}}>{{/returnType}}{{^returnType}}System.Threading.Tasks.Task{{/returnType}} {{operationId}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

        /// <summary>
        /// {{summary}}
        /// </summary>
        /// <remarks>
        /// {{notes}}
        /// </remarks>
        /// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
        {{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
        {{/allParams}}/// <returns>Task of ApiResponse{{#returnType}} ({{returnType}}){{/returnType}}</returns>
        System.Threading.Tasks.Task<ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}>> {{operationId}}AsyncWithHttpInfo ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
        {{/operation}}
        #endregion Asynchronous Operations
    }
    {{/supportsAsync}}

    /// <summary>
    /// Represents a collection of functions to interact with the API endpoints
    /// </summary>
    {{>visibility}} interface {{interfacePrefix}}{{classname}} : {{interfacePrefix}}{{classname}}Sync{{#supportsAsync}}, {{interfacePrefix}}{{classname}}Async{{/supportsAsync}}
    {

    }

    /// <summary>
    /// Represents a collection of functions to interact with the API endpoints
    /// </summary>
    {{>visibility}} partial class {{classname}} : {{interfacePrefix}}{{classname}}
    {
        private {{packageName}}.Client.ExceptionFactory _exceptionFactory = (name, response) => null;

        /// <summary>
        /// Initializes a new instance of the <see cref="{{classname}}"/> class.
        /// </summary>
        /// <returns></returns>
        public {{classname}}() : this((string) null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="{{classname}}"/> class.
        /// </summary>
        /// <returns></returns>
        public {{classname}}(String basePath)
        {
            this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
                {{packageName}}.Client.GlobalConfiguration.Instance,
                new {{packageName}}.Client.Configuration { BasePath = basePath }
            );
            this.Client = new {{packageName}}.Client.ApiClient(this.Configuration.BasePath);
            this.AsynchronousClient = new {{packageName}}.Client.ApiClient(this.Configuration.BasePath);
            this.ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="{{classname}}"/> class
        /// using Configuration object
        /// </summary>
        /// <param name="configuration">An instance of Configuration</param>
        /// <returns></returns>
        public {{classname}}({{packageName}}.Client.Configuration configuration)
        {
            if (configuration == null) throw new ArgumentNullException("configuration");

            this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
                {{packageName}}.Client.GlobalConfiguration.Instance,
                configuration
            );
            this.Client = new {{packageName}}.Client.ApiClient(this.Configuration.BasePath);
            this.AsynchronousClient = new {{packageName}}.Client.ApiClient(this.Configuration.BasePath);
            ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PetApi"/> class
        /// using a Configuration object and client instance.
        /// </summary>
        /// <param name="client">The client interface for synchronous API access.</param>{{#supportsAsync}}
        /// <param name="asyncClient">The client interface for asynchronous API access.</param>{{/supportsAsync}}
        /// <param name="configuration">The configuration object.</param>
        public {{classname}}({{packageName}}.Client.ISynchronousClient client,{{#supportsAsync}}{{packageName}}.Client.IAsynchronousClient asyncClient,{{/supportsAsync}} {{packageName}}.Client.IReadableConfiguration configuration)
        {
            if(client == null) throw new ArgumentNullException("client");
            {{#supportsAsync}}
            if(asyncClient == null) throw new ArgumentNullException("asyncClient");
            {{/supportsAsync}}
            if(configuration == null) throw new ArgumentNullException("configuration");

            this.Client = client;
            {{#supportsAsync}}
            this.AsynchronousClient = asyncClient;
            {{/supportsAsync}}
            this.Configuration = configuration;
            this.ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory;
        }

        {{#supportsAsync}}
        /// <summary>
        /// The client for accessing this underlying API asynchronously.
        /// </summary>
        public {{packageName}}.Client.IAsynchronousClient AsynchronousClient { get; set; }
        {{/supportsAsync}}

        /// <summary>
        /// The client for accessing this underlying API synchronously.
        /// </summary>
        public {{packageName}}.Client.ISynchronousClient Client { get; set; }

        /// <summary>
        /// Gets the base path of the API client.
        /// </summary>
        /// <value>The base path</value>
        public String GetBasePath()
        {
            return this.Configuration.BasePath;
        }

        /// <summary>
        /// Gets or sets the configuration object
        /// </summary>
        /// <value>An instance of the Configuration</value>
        public {{packageName}}.Client.IReadableConfiguration Configuration {get; set;}

        /// <summary>
        /// Provides a factory method hook for the creation of exceptions.
        /// </summary>
        public {{packageName}}.Client.ExceptionFactory ExceptionFactory
        {
            get
            {
                if (_exceptionFactory != null && _exceptionFactory.GetInvocationList().Length > 1)
                {
                    throw new InvalidOperationException("Multicast delegate for ExceptionFactory is unsupported.");
                }
                return _exceptionFactory;
            }
            set { _exceptionFactory = value; }
        }

        {{#operation}}
        /// <summary>
        /// {{summary}} {{notes}}
        /// </summary>
        /// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
        {{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
        {{/allParams}}/// <returns>{{#returnType}}{{returnType}}{{/returnType}}</returns>
        public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}} ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
        {
             {{#returnType}}{{packageName}}.Client.ApiResponse<{{{returnType}}}> localVarResponse = {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
             return localVarResponse.Data;{{/returnType}}{{^returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{/returnType}}
        }

        /// <summary>
        /// {{summary}} {{notes}}
        /// </summary>
        /// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
        {{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
        {{/allParams}}/// <returns>ApiResponse of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Object(void){{/returnType}}</returns>
        public {{packageName}}.Client.ApiResponse<{{#returnType}} {{{returnType}}} {{/returnType}}{{^returnType}}Object{{/returnType}}> {{operationId}}WithHttpInfo ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
        {
            {{#allParams}}
            {{^isNullable}}
            {{#required}}
            // verify the required parameter '{{paramName}}' is set
            if ({{paramName}} == null)
                throw new {{packageName}}.Client.ApiException(400, "Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}");

            {{/required}}
            {{/isNullable}}
            {{/allParams}}
            {{packageName}}.Client.RequestOptions requestOptions = new {{packageName}}.Client.RequestOptions();

            String[] @contentTypes = new String[] {
                {{#consumes}}
                "{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}
                {{/consumes}}
            };

            // to determine the Accept header
            String[] @accepts = new String[] {
                {{#produces}}
                "{{{mediaType}}}"{{#hasMore}},{{/hasMore}}
                {{/produces}}
            };

            var localVarConentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(@contentTypes);
            if (localVarConentType != null) requestOptions.HeaderParameters.Add("Content-Type", localVarConentType);

            var localVarAccept = Org.OpenAPITools.Client.ClientUtils.SelectHeaderAccept(@accepts);
            if (localVarAccept != null) requestOptions.HeaderParameters.Add("Accept", localVarAccept);

            {{#pathParams}}
            if ({{paramName}} != null)
                requestOptions.PathParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // path parameter
            {{/pathParams}}
            {{#queryParams}}
            if ({{paramName}} != null)
            {
                foreach (var kvp in {{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}))
                {
                    foreach (var value in kvp.Value)
                    {
                        requestOptions.QueryParameters.Add(kvp.Key, value);
                    }
                }
            }
            {{/queryParams}}
            {{#headerParams}}
            if ({{paramName}} != null)
                requestOptions.HeaderParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // header parameter
            {{/headerParams}}
            {{#formParams}}
            if ({{paramName}} != null)
            {
                {{#isFile}}
                requestOptions.FileParameters.Add("{{baseName}}", {{paramName}});
                {{/isFile}}
                {{^isFile}}
                requestOptions.FormParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // form parameter
                {{/isFile}}
            }
            {{/formParams}}
            {{#bodyParam}}
            requestOptions.Data = {{paramName}};
            {{/bodyParam}}

            {{#authMethods}}
            // authentication ({{name}}) required
            {{#isApiKey}}
            {{#isKeyInHeader}}
            if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
            {
                requestOptions.HeaderParameters.Add("{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"));
            }
            {{/isKeyInHeader}}
            {{#isKeyInQuery}}
            if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
            {
                foreach (var kvp in {{packageName}}.Client.ClientUtils.ParameterToMultiMap("", "{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
                {
                    foreach (var value in kvp.Value)
                    {
                        requestOptions.QueryParameters.Add(kvp.Key, value);
                    }
                }
            }
            {{/isKeyInQuery}}
            {{/isApiKey}}
            {{#isBasic}}
            // http basic authentication required
            if (!String.IsNullOrEmpty(this.Configuration.Username) || !String.IsNullOrEmpty(this.Configuration.Password))
            {
                requestOptions.HeaderParameters.Add("Authorization", "Basic " + {{packageName}}.Client.ClientUtils.Base64Encode(this.Configuration.Username + ":" + this.Configuration.Password));
            }
            {{/isBasic}}
            {{#isOAuth}}
            // oauth required
            if (!String.IsNullOrEmpty(this.Configuration.AccessToken))
            {
                requestOptions.HeaderParameters.Add("Authorization", "Bearer " + this.Configuration.AccessToken);
            }
            {{/isOAuth}}
            {{/authMethods}}

            // make the HTTP request

            var response = this.Client.{{#lambda.titlecase}}{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}{{/lambda.titlecase}}<{{#returnType}} {{{returnType}}} {{/returnType}}{{^returnType}}Object{{/returnType}}>("{{{path}}}", requestOptions, this.Configuration);

            if (this.ExceptionFactory != null)
            {
                Exception exception = this.ExceptionFactory("{{operationId}}", response);
                if (exception != null) throw exception;
            }

            return response;
        }

        {{#supportsAsync}}
        /// <summary>
        /// {{summary}} {{notes}}
        /// </summary>
        /// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
        {{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
        {{/allParams}}/// <returns>Task of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}</returns>
        {{#returnType}}public async System.Threading.Tasks.Task<{{{returnType}}}>{{/returnType}}{{^returnType}}public async System.Threading.Tasks.Task{{/returnType}} {{operationId}}Async ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
        {
             {{#returnType}}{{packageName}}.Client.ApiResponse<{{{returnType}}}> localVarResponse = await {{operationId}}AsyncWithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
             return localVarResponse.Data;{{/returnType}}{{^returnType}}await {{operationId}}AsyncWithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{/returnType}}

        }

        /// <summary>
        /// {{summary}} {{notes}}
        /// </summary>
        /// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
        {{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
        {{/allParams}}/// <returns>Task of ApiResponse{{#returnType}} ({{returnType}}){{/returnType}}</returns>
        public async System.Threading.Tasks.Task<{{packageName}}.Client.ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}>> {{operationId}}AsyncWithHttpInfo ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
        {
            {{#allParams}}
            {{^isNullable}}
            {{#required}}
            // verify the required parameter '{{paramName}}' is set
            if ({{paramName}} == null)
                throw new {{packageName}}.Client.ApiException(400, "Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}");

            {{/required}}
            {{/isNullable}}
            {{/allParams}}

            {{packageName}}.Client.RequestOptions requestOptions = new {{packageName}}.Client.RequestOptions();

            String[] @contentTypes = new String[] {
                {{#consumes}}
                "{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}
                {{/consumes}}
            };

            // to determine the Accept header
            String[] @accepts = new String[] {
                {{#produces}}
                "{{{mediaType}}}"{{#hasMore}},{{/hasMore}}
                {{/produces}}
            };
            
            foreach (var contentType in @contentTypes)
                requestOptions.HeaderParameters.Add("Content-Type", contentType);
            
            foreach (var accept in @accepts)
                requestOptions.HeaderParameters.Add("Accept", accept);
            
            {{#pathParams}}
            if ({{paramName}} != null)
                requestOptions.PathParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // path parameter
            {{/pathParams}}
            {{#queryParams}}
            if ({{paramName}} != null)
            {
                foreach (var kvp in {{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}))
                {
                    foreach (var value in kvp.Value)
                    {
                        requestOptions.QueryParameters.Add(kvp.Key, value);
                    }
                }
            }
            {{/queryParams}}
            {{#headerParams}}
            if ({{paramName}} != null)
                requestOptions.HeaderParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // header parameter
            {{/headerParams}}
            {{#formParams}}
            if ({{paramName}} != null)
            {
                {{#isFile}}
                requestOptions.FileParameters.Add("{{baseName}}", {{paramName}});
                {{/isFile}}
                {{^isFile}}
                requestOptions.FormParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // form parameter
                {{/isFile}}
            }
            {{/formParams}}
            {{#bodyParam}}
            requestOptions.Data = {{paramName}};
            {{/bodyParam}}

            {{#authMethods}}
            // authentication ({{name}}) required
            {{#isApiKey}}
            {{#isKeyInHeader}}
            if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
            {
                requestOptions.HeaderParameters.Add("{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"));
            }
            {{/isKeyInHeader}}
            {{#isKeyInQuery}}
            if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
            {
                foreach (var kvp in {{packageName}}.Client.ClientUtils.ParameterToMultiMap("", "{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
                {
                    foreach (var value in kvp.Value)
                    {
                        requestOptions.QueryParameters.Add(kvp.Key, value);
                    }
                }
            }
            {{/isKeyInQuery}}
            {{/isApiKey}}
            {{#isBasic}}
            // http basic authentication required
            if (!String.IsNullOrEmpty(this.Configuration.Username) || !String.IsNullOrEmpty(this.Configuration.Password))
            {
                requestOptions.HeaderParameters.Add("Authorization", "Basic " + {{packageName}}.Client.ClientUtils.Base64Encode(this.Configuration.Username + ":" + this.Configuration.Password));
            }
            {{/isBasic}}
            {{#isOAuth}}
            // oauth required
            if (!String.IsNullOrEmpty(this.Configuration.AccessToken))
            {
                requestOptions.HeaderParameters.Add("Authorization", "Bearer " + this.Configuration.AccessToken);
            }
            {{/isOAuth}}
            {{/authMethods}}

            // make the HTTP request

            var response = await this.AsynchronousClient.{{#lambda.titlecase}}{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}{{/lambda.titlecase}}Async<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}>("{{{path}}}", requestOptions, this.Configuration);

            if (this.ExceptionFactory != null)
            {
                Exception exception = this.ExceptionFactory("{{operationId}}", response);
                if (exception != null) throw exception;
            }

            return response;
        }

        {{/supportsAsync}}
        {{/operation}}
    }
    {{/operations}}
}
