{{>licenseInfo}}
package {{package}};

import {{invokerPackage}}.ApiInvoker;
import {{invokerPackage}}.ApiException;
import {{invokerPackage}}.Pair;
import {{modelPackage}}.*;
import com.fht360.swagger.models.*;

import com.fht360.fht.BuildConfig;
import com.fht360.fht.volley.RestfulRequest.FHTCallback;

import java.util.*;

{{#imports}}import {{import}};
{{/imports}}

import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import com.android.volley.Response;
import com.android.volley.VolleyError;

{{#operations}}
public class {{classname}} {
  String basePath = BuildConfig.API_HOST;
  ApiInvoker apiInvoker = ApiInvoker.getInstance();
  private Boolean cache;

  public {{classname}}()
  {
     this(true);
  }
  
  public {{classname}}(Boolean cache) {
      this.cache = cache;
  }

  public void addHeader(String key, String value) {
    getInvoker().addDefaultHeader(key, value);
  }

  public ApiInvoker getInvoker() {
    return apiInvoker;
  }

  public void setBasePath(String basePath) {
    this.basePath = basePath;
  }

  public String getBasePath() {
    return basePath;
  }

  {{#operation}}
      /**
   * {{summary}}
   * {{notes}}
{{#allParams}}   * @param {{paramName}} {{description}}{{/allParams}}
   * MARK 1: returnContainer: {{returnContainer}}, returnBaseType: {{returnBaseType}}
  */
  public void {{nickname}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}, {{/hasMore}}{{/allParams}}final FHTCallback<{{#returnType}}{{#returnContainer}}{{{returnBaseType}}}[]{{/returnContainer}}{{^returnContainer}}{{{returnBaseType}}}{{/returnContainer}}{{/returnType}}{{^returnType}}String{{/returnType}}> callback) {
    Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};

  {{#allParams}}{{#required}}
    // verify the required parameter '{{paramName}}' is set
    if ({{paramName}} == null) {
       VolleyError error = new VolleyError("Missing the required parameter '{{paramName}}' when calling {{nickname}}",
         new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{nickname}}"));
    } 
    {{/required}}{{/allParams}}

    // create path and map variables
    String path = "{{path}}".replaceAll("\\{format\\}","json"){{#pathParams}}.replaceAll("\\{" + "{{baseName}}" + "\\}", apiInvoker.escapeString({{{paramName}}}.toString())){{/pathParams}};

    // query params
    List<Pair> queryParams = new ArrayList<Pair>();
    // header params
    Map<String, String> headerParams = new HashMap<String, String>();
    // form params
    Map<String, String> formParams = new HashMap<String, String>();

    {{#queryParams}}
    queryParams.addAll(ApiInvoker.parameterToPairs("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
    {{/queryParams}}

    {{#headerParams}}
    headerParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));
    {{/headerParams}}

    //String[] contentTypes = {
    //  {{#consumes}}"{{{mediaType}}}"{{#hasMore}},{{/hasMore}}{{/consumes}}
    //};
    //String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
    String contentType ="application/json";

    if (contentType.startsWith("multipart/form-data")) {
      // file uploading
      MultipartEntityBuilder localVarBuilder = MultipartEntityBuilder.create();
      {{#formParams}}{{#notFile}}
      if ({{paramName}} != null) {
        localVarBuilder.addTextBody("{{baseName}}", ApiInvoker.parameterToString({{paramName}}), ApiInvoker.TEXT_PLAIN_UTF8);
      }
      {{/notFile}}{{#isFile}}
      if ({{paramName}} != null) {
        localVarBuilder.addBinaryBody("{{baseName}}", {{paramName}});
      }
      {{/isFile}}{{/formParams}}

      HttpEntity httpEntity = localVarBuilder.build();
      postBody = httpEntity;
    } else {
      // normal form params
      {{#formParams}}{{#notFile}}formParams.put("{{baseName}}", ApiInvoker.parameterToString({{paramName}}));{{/notFile}}
      {{/formParams}}
    }

      String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };

    try {
      {{#returnType}}
      {{#returnContainer}}
      callback.returnType = {{returnBaseType}}[].class;
      {{/returnContainer}}
      {{^returnContainer}}
      callback.returnType = {{returnBaseType}}.class;
      {{/returnContainer}}
      {{/returnType}}
      {{^returnType}}
      callback.returnType = null;
      {{/returnType}}
      apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", this.cache, queryParams, postBody, headerParams, formParams, contentType, authNames,
        callback);
    } catch (ApiException ex) {
      callback.getErrorListener().onErrorResponse(new VolleyError(ex));
    }
  }
  {{/operation}}
}
{{/operations}}
