{{#operations}}
#import "{{classname}}.h"
#import "{{classPrefix}}QueryParamCollection.h"
{{#imports}}#import "{{import}}.h"
{{/imports}}
{{newline}}

@interface {{classname}} ()
    @property (readwrite, nonatomic, strong) NSMutableDictionary *defaultHeaders;
@end

@implementation {{classname}}

static {{classname}}* singletonAPI = nil;

#pragma mark - Initialize methods

- (instancetype) init {
    self = [super init];
    if (self) {
        {{classPrefix}}Configuration *config = [{{classPrefix}}Configuration sharedConfig];
        if (config.apiClient == nil) {
            config.apiClient = [[{{classPrefix}}ApiClient alloc] init];
        }
        self.apiClient = config.apiClient;
        self.defaultHeaders = [NSMutableDictionary dictionary];
    }
    return self;
}

- (instancetype) initWithApiClient:({{classPrefix}}ApiClient *)apiClient {
    self = [super init];
    if (self) {
        self.apiClient = apiClient;
        self.defaultHeaders = [NSMutableDictionary dictionary];
    }
    return self;
}

#pragma mark -

+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
    if (singletonAPI == nil) {
        singletonAPI = [[{{classname}} alloc] init];
        [singletonAPI addHeader:headerValue forKey:key];
    }
    return singletonAPI;
}

+({{classname}}*) sharedAPI {
    if (singletonAPI == nil) {
        singletonAPI = [[{{classname}} alloc] init];
    }
    return singletonAPI;
}

-(void) addHeader:(NSString*)value forKey:(NSString*)key {
    [self.defaultHeaders setValue:value forKey:key];
}

-(void) setHeaderValue:(NSString*) value
           forKey:(NSString*)key {
    [self.defaultHeaders setValue:value forKey:key];
}

-(unsigned long) requestQueueSize {
    return [{{classPrefix}}ApiClient requestQueueSize];
}

#pragma mark - Api Methods

{{#operation}}
///
/// {{{summary}}}
/// {{{notes}}}
/// {{#allParams}} @param {{paramName}} {{{description}}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
///
/// {{/allParams}} @returns {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
///
-(NSNumber*) {{#vendorExtensions.x-objc-operationId}}{{vendorExtensions.x-objc-operationId}}{{/vendorExtensions.x-objc-operationId}}{{^vendorExtensions.x-objc-operationId}}{{nickname}}{{#hasParams}}With{{vendorExtensions.firstParamAltName}}{{/hasParams}}{{^hasParams}}WithCompletionHandler: {{/hasParams}}{{/vendorExtensions.x-objc-operationId}}{{#allParams}}{{#secondaryParam}}
    {{paramName}}{{/secondaryParam}}: ({{{dataType}}}) {{paramName}}{{/allParams}}
    {{#hasParams}}completionHandler: {{/hasParams}}(void (^)({{#returnBaseType}}{{{returnType}}} output, {{/returnBaseType}}NSError* error)) handler {
    {{#allParams}}
    {{#required}}
    // verify the required parameter '{{paramName}}' is set
    if ({{paramName}} == nil) {
        [NSException raise:@"Invalid parameter" format:@"Missing the required parameter `{{paramName}}` when calling `{{nickname}}`"];
    }

    {{/required}}
    {{/allParams}}
    NSMutableString* resourcePath = [NSMutableString stringWithFormat:@"{{path}}"];

    // remove format in URL if needed
    [resourcePath replaceOccurrencesOfString:@".{format}" withString:@".json" options:0 range:NSMakeRange(0,resourcePath.length)];

    NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init];
    {{#pathParams}}
    if ({{paramName}} != nil) {
        pathParams[@"{{baseName}}"] = {{paramName}};
    }
    {{/pathParams}}

    NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
    {{#queryParams}}
    if ({{paramName}} != nil) {
        {{#collectionFormat}}
        queryParams[@"{{baseName}}"] = [[{{classPrefix}}QueryParamCollection alloc] initWithValuesAndFormat: {{baseName}} format: @"{{collectionFormat}}"];
        {{/collectionFormat}}
        {{^collectionFormat}}queryParams[@"{{baseName}}"] = {{paramName}};{{/collectionFormat}}
    }
    {{/queryParams}}
    NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
    {{#headerParams}}
    
    if ({{paramName}} != nil) {
        headerParams[@"{{baseName}}"] = {{paramName}};
    }

    {{/headerParams}}
    // HTTP header `Accept`
    NSString *acceptHeader = [self.apiClient.sanitizer selectHeaderAccept:@[{{#produces}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}]];
    if(acceptHeader.length > 0) {
        headerParams[@"Accept"] = acceptHeader;
    }

    // response content type
    NSString *responseContentType = [[acceptHeader componentsSeparatedByString:@", "] firstObject] ?: @"";

    // request content type
    NSString *requestContentType = [self.apiClient.sanitizer selectHeaderContentType:@[{{#consumes}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}]];

    // Authentication setting
    NSArray *authSettings = @[{{#authMethods}}@"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}}];

    id bodyParam = nil;
    NSMutableDictionary *formParams = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *localVarFiles = [[NSMutableDictionary alloc] init];
    {{#bodyParam}}
    bodyParam = {{paramName}};
    {{/bodyParam}}
    {{^bodyParam}}
    {{#formParams}}
    {{#notFile}}
    if ({{paramName}}) {
        formParams[@"{{baseName}}"] = {{paramName}};
    }
    {{/notFile}}
    {{#isFile}}
    localVarFiles[@"{{paramName}}"] = {{paramName}};
    {{/isFile}}
    {{/formParams}}
    {{/bodyParam}}

    return [self.apiClient requestWithPath: resourcePath
                                    method: @"{{httpMethod}}"
                                pathParams: pathParams
                               queryParams: queryParams
                                formParams: formParams
                                     files: localVarFiles
                                      body: bodyParam
                              headerParams: headerParams
                              authSettings: authSettings
                        requestContentType: requestContentType
                       responseContentType: responseContentType
                              responseType: {{^returnType}}nil{{/returnType}}{{#returnType}}@"{{{ returnType }}}"{{/returnType}}
                           completionBlock: ^(id data, NSError *error) {
                               handler({{#returnType}}({{{ returnType }}})data, {{/returnType}}error);
                           }
          ];
}

{{/operation}}

{{newline}}
{{/operations}}
@end
