using System;
using Nancy;
using Nancy.ModelBinding;
using System.Collections.Generic;
using Sharpility.Base;
using {{packageName}}.{{packageContext}}.Models;
using {{packageName}}.{{packageContext}}.Utils;
using NodaTime;{{#asyncServer}}
using System.Threading.Tasks;{{/asyncServer}}
{{#imports}}using {{import}};
{{/imports}}

namespace {{packageName}}.{{packageContext}}.Modules
{ {{#operations}}{{#operation}}{{#allParams}}{{#isEnum}}
    {{>innerApiEnum}}{{/isEnum}}{{/allParams}}{{/operation}}

    /// <summary>
    /// Module processing requests of {{classname}} domain.
    /// </summary>
    public sealed class {{classname}}Module : NancyModule
    {
        /// <summary>
        /// Sets up HTTP methods mappings.
        /// </summary>
        /// <param name="service">Service handling requests</param>
        public {{classname}}Module({{interfacePrefix}}{{classname}}Service service) : base("{{{baseContext}}}")
        { {{#operation}}
            {{httpMethod}}["{{{path}}}"{{#asyncServer}}, true{{/asyncServer}}] = {{#asyncServer}}async (parameters, ct){{/asyncServer}}{{^asyncServer}}parameters{{/asyncServer}} =>
            {
                {{#allParams}}{{#isBodyParam}}var {{paramName}} = this.Bind<{{&dataType}}>();{{/isBodyParam}}{{^isBodyParam}}{{#isEnum}}var {{paramName}} = Parameters.ValueOf<{{>innerApiEnumName}}?>({{>innerParameterValueOfArgs}});{{/isEnum}}{{^isEnum}}var {{paramName}} = Parameters.ValueOf<{{&dataType}}>({{>innerParameterValueOfArgs}});{{/isEnum}}{{#hasMore}}
                {{/hasMore}}{{/isBodyParam}}{{/allParams}}{{#allParams}}{{#required}}
                Preconditions.IsNotNull({{paramName}}, "Required parameter: '{{paramName}}' is missing at '{{operationId}}'");
                {{/required}}{{/allParams}}
                {{#returnType}}return {{/returnType}}{{#asyncServer}}await {{/asyncServer}}service.{{operationId}}(Context{{#allParams.0}}, {{/allParams.0}}{{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}{{#isListContainer}}.ToArray(){{/isListContainer}}{{/returnType}};{{^returnType}}
                return new Response { ContentType = "{{produces.0.mediaType}}"};{{/returnType}}
            };
{{/operation}}
        }
    }

    /// <summary>
    /// Service handling {{classname}} requests.
    /// </summary>
    public interface {{interfacePrefix}}{{classname}}Service
    {
        {{#operation}}/// <summary>
        /// {{notes}}
        /// </summary>
        /// <param name="context">Context of request</param>
        {{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
        {{/allParams}}/// <returns>{{#returnType}}{{returnType}}{{/returnType}}</returns>
        {{#asyncServer}}{{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}}{{/asyncServer}}{{^asyncServer}}{{#returnType}}{{&returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}{{/asyncServer}} {{operationId}}(NancyContext context{{#allParams.0}}, {{/allParams.0}}{{>paramsList}});{{#hasMore}}

        {{/hasMore}}{{/operation}}
    }

    /// <summary>
    /// Abstraction of {{classname}}Service.
    /// </summary>
    public abstract class Abstract{{classname}}Service: {{interfacePrefix}}{{classname}}Service
    {
        {{#operation}}public virtual {{#asyncServer}}{{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}}{{/asyncServer}}{{^asyncServer}}{{#returnType}}{{&returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}{{/asyncServer}} {{operationId}}(NancyContext context{{#allParams.0}}, {{/allParams.0}}{{>paramsList}})
        {
            {{^asyncServer}}{{#returnType}}return {{/returnType}}{{/asyncServer}}{{#asyncServer}}return {{/asyncServer}}{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
        }{{#hasMore}}

        {{/hasMore}}{{/operation}}

        {{#operation}}protected abstract {{#asyncServer}}{{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}}{{/asyncServer}}{{^asyncServer}}{{#returnType}}{{&returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}{{/asyncServer}} {{operationId}}({{>paramsList}});{{#hasMore}}

        {{/hasMore}}{{/operation}}
    }

{{/operations}}
}
