/// <reference path="api.d.ts" />

/* tslint:disable:no-unused-variable member-ordering */

{{#operations}}
namespace {{package}} {
    'use strict';

{{#description}}
    /**
    * {{&description}}
    */
{{/description}}
    export class {{classname}} {
        protected basePath = '{{basePath}}';
        public defaultHeaders : any = {};

        static $inject: string[] = ['$http', '$httpParamSerializer'];

        constructor(protected $http: ng.IHttpService, protected $httpParamSerializer?: (d: any) => any, basePath?: string) {
            if (basePath) {
                this.basePath = basePath;
            }
        }

        private extendObj<T1,T2>(objA: T1, objB: T2) {
            for(let key in objB){
                if(objB.hasOwnProperty(key)){
                    objA[key] = objB[key];
                }
            }
            return <T1&T2>objA;
        }

{{#operation}}
        /**
         * {{summary}}
         * {{notes}}
         {{#allParams}}* @param {{paramName}} {{description}}
         {{/allParams}}*/
        public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any ) : ng.IHttpPromise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
            const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
                .replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};

            let queryParameters: any = {};
            let headerParams: any = this.extendObj({}, this.defaultHeaders);
{{#hasFormParams}}
            let formParams: any = {};

{{/hasFormParams}}
{{#allParams}}
{{#required}}
            // verify required parameter '{{paramName}}' is set
            if (!{{paramName}}) {
                throw new Error('Missing required parameter {{paramName}} when calling {{nickname}}');
            }
{{/required}}
{{/allParams}}
{{#queryParams}}
            if ({{paramName}} !== undefined) {
                queryParameters['{{baseName}}'] = {{paramName}};
            }

{{/queryParams}}
{{#headerParams}}
            headerParams['{{baseName}}'] = {{paramName}};

{{/headerParams}}
{{#hasFormParams}}
            headerParams['Content-Type'] = 'application/x-www-form-urlencoded';

{{/hasFormParams}}
{{#formParams}}
            formParams['{{baseName}}'] = {{paramName}};

{{/formParams}}
            let httpRequestParams: any = {
                method: '{{httpMethod}}',
                url: localVarPath,
                json: {{#hasFormParams}}false{{/hasFormParams}}{{^hasFormParams}}true{{/hasFormParams}},
                {{#bodyParam}}data: {{paramName}},
                {{/bodyParam}}
                {{#hasFormParams}}data: this.$httpParamSerializer(formParams),
                {{/hasFormParams}}
                params: queryParameters,
                headers: headerParams
            };

            if (extraHttpRequestParams) {
                httpRequestParams = this.extendObj(httpRequestParams, extraHttpRequestParams);
            }

            return this.$http(httpRequestParams);
        }
{{/operation}}
    }
}
{{/operations}}
