package {{package}}

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

import java.io.File

import org.scalatra.{ TypedParamSupport, ScalatraServlet }
import org.scalatra.swagger._
import org.json4s._
import org.json4s.JsonDSL._
import org.scalatra.json.{ JValueResult, JacksonJsonSupport }
import org.scalatra.servlet.{FileUploadSupport, MultipartConfig, SizeConstraintExceededException}

import scala.collection.JavaConverters._

class {{classname}} (implicit val swagger: Swagger) extends ScalatraServlet 
    with FileUploadSupport
    with JacksonJsonSupport
    with SwaggerSupport {
  protected implicit val jsonFormats: Formats = DefaultFormats

  protected val applicationDescription: String = "{{classname}}"
  override protected val applicationName: Option[String] = Some("{{baseName}}")

  before() {
    contentType = formats("json")
    response.headers += ("Access-Control-Allow-Origin" -> "*")
  }
{{#operations}}
{{#operation}}
  {{newline}}

  val {{nickname}}Operation = (apiOperation[{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}]("{{nickname}}")
      summary "{{{summary}}}"
      parameters(
        {{#allParams}}{{#isQueryParam}}queryParam[{{dataType}}]("{{paramName}}").description(""){{^required}}.optional{{/required}}{{#defaultValue}}.defaultValue({{{defaultValue}}}){{/defaultValue}}
        {{/isQueryParam}}
        {{#isPathParam}}pathParam[{{dataType}}]("{{paramName}}").description(""){{#defaultValue}}.defaultValue({{{defaultValue}}}){{/defaultValue}}
        {{/isPathParam}}
        {{#isHeaderParam}}headerParam[{{dataType}}]("{{paramName}}").description(""){{^required}}.optional{{/required}}{{#defaultValue}}.defaultValue({{{defaultValue}}}){{/defaultValue}}
        {{/isHeaderParam}}
        {{#isBodyParam}}bodyParam[{{dataType}}]("{{paramName}}").description(""){{^required}}.optional{{/required}}{{#defaultValue}}.defaultValue({{{defaultValue}}}){{/defaultValue}}
        {{/isBodyParam}}
        {{#isFormParam}}formParam[{{dataType}}]("{{paramName}}").description(""){{^required}}.optional{{/required}}{{#defaultValue}}.defaultValue({{{defaultValue}}}){{/defaultValue}}
        {{/isFormParam}}
        {{#hasMore}},{{/hasMore}}
        {{/allParams}})
  )

  {{httpMethod}}("{{path}}",operation({{nickname}}Operation)) {
    {{#allParams}}
    {{#isFile}}
      val {{paramName}} = fileParams("{{paramName}}")
    {{/isFile}}
    {{^isFile}}
    {{#isPathParam}}
      val {{paramName}} = params.getOrElse("{{paramName}}", halt(400))
    {{/isPathParam}}

    {{#isQueryParam}}
      {{#collectionFormat}}val {{paramName}}String = params.getAs[String]("{{paramName}}")
      val {{paramName}} = if("{{collectionFormat}}".equals("default")) {
        {{paramName}}String match {
          case Some(str) => str.split(",")
          case None => List()
        }
      }
      else
        List()
      {{/collectionFormat}}
      {{^collectionFormat}}val {{paramName}} = params.getAs[{{dataType}}]("{{paramName}}"){{/collectionFormat}}
      
    {{/isQueryParam}}

    {{#isHeaderParam}}
      val {{paramName}} = request.getHeader("{{paramName}}")
    {{/isHeaderParam}}

    {{#isFormParam}}
      val {{paramName}} = params.getAs[{{dataType}}]("{{paramName}}")
    {{/isFormParam}}

    {{#isBodyParam}}
      val {{paramName}} = parsedBody.extract[{{dataType}}]
    {{/isBodyParam}}
    {{/isFile}}
    println("{{paramName}}: " + {{paramName}})
  {{/allParams}}
  }

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