/*
 * AvaTax Brazil
 * The Avatax-Brazil API exposes the most commonly services available for interacting with the AvaTax-Brazil services, allowing calculation of taxes, issuing electronic invoice documents and modifying existing transactions when allowed by tax authorities.  This API is exclusively for use by business with a physical presence in Brazil.
 *
 * OpenAPI spec version: 1.0
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */


package io.swagger.client.api;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;

import io.swagger.client.ApiCallback;
import io.swagger.client.ApiClient;
import io.swagger.client.ApiException;
import io.swagger.client.ApiResponse;
import io.swagger.client.Configuration;
import io.swagger.client.Pair;
import io.swagger.client.ProgressRequestBody;
import io.swagger.client.ProgressResponseBody;
import io.swagger.client.model.AbrasfItDeleteIn;
import io.swagger.client.model.AbrasfItGetOut;
import io.swagger.client.model.AbrasfPostOut;
import io.swagger.client.model.TransactionForAbrasfList;

public class InvoiceServiceApi {
    private ApiClient apiClient;

    public InvoiceServiceApi() {
        this(Configuration.getDefaultApiClient());
    }

    public InvoiceServiceApi(ApiClient apiClient) {
        this.apiClient = apiClient;
    }

    public ApiClient getApiClient() {
        return apiClient;
    }

    public void setApiClient(ApiClient apiClient) {
        this.apiClient = apiClient;
    }

    /* Build call for cancelInvoiceService */
    private com.squareup.okhttp.Call cancelInvoiceServiceCall(String authorization, String key, AbrasfItDeleteIn body, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        Object localVarPostBody = body;
        
        // create path and map variables
        String localVarPath = "/invoices/service/{key}".replaceAll("\\{format\\}","json")
        .replaceAll("\\{" + "key" + "\\}", apiClient.escapeString(key.toString()));

        List<Pair> localVarQueryParams = new ArrayList<Pair>();

        Map<String, String> localVarHeaderParams = new HashMap<String, String>();
        if (authorization != null)
        localVarHeaderParams.put("Authorization", apiClient.parameterToString(authorization));

        Map<String, Object> localVarFormParams = new HashMap<String, Object>();

        final String[] localVarAccepts = {
            "application/json"
        };
        final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept);

        final String[] localVarContentTypes = {
            "application/json"
        };
        final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        if(progressListener != null) {
            apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
                @Override
                public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain) throws IOException {
                    com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
                    return originalResponse.newBuilder()
                    .body(new ProgressResponseBody(originalResponse.body(), progressListener))
                    .build();
                }
            });
        }

        String[] localVarAuthNames = new String[] {  };
        return apiClient.buildCall(localVarPath, "DELETE", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
    }
    
    @SuppressWarnings("rawtypes")
    private com.squareup.okhttp.Call cancelInvoiceServiceValidateBeforeCall(String authorization, String key, AbrasfItDeleteIn body, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        
        // verify the required parameter 'authorization' is set
        if (authorization == null) {
            throw new ApiException("Missing the required parameter 'authorization' when calling cancelInvoiceService(Async)");
        }
        
        // verify the required parameter 'key' is set
        if (key == null) {
            throw new ApiException("Missing the required parameter 'key' when calling cancelInvoiceService(Async)");
        }
        
        // verify the required parameter 'body' is set
        if (body == null) {
            throw new ApiException("Missing the required parameter 'body' when calling cancelInvoiceService(Async)");
        }
        
        
        com.squareup.okhttp.Call call = cancelInvoiceServiceCall(authorization, key, body, progressListener, progressRequestListener);
        return call;

        
        
        
        
    }

    /**
     * Cancel e-Invoice Service
     * Cancel the invoice
     * @param authorization Bearer {auth} (required)
     * @param key Invoice Key (required)
     * @param body Cancel Message (required)
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public void cancelInvoiceService(String authorization, String key, AbrasfItDeleteIn body) throws ApiException {
        cancelInvoiceServiceWithHttpInfo(authorization, key, body);
    }

    /**
     * Cancel e-Invoice Service
     * Cancel the invoice
     * @param authorization Bearer {auth} (required)
     * @param key Invoice Key (required)
     * @param body Cancel Message (required)
     * @return ApiResponse&lt;Void&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public ApiResponse<Void> cancelInvoiceServiceWithHttpInfo(String authorization, String key, AbrasfItDeleteIn body) throws ApiException {
        com.squareup.okhttp.Call call = cancelInvoiceServiceValidateBeforeCall(authorization, key, body, null, null);
        return apiClient.execute(call);
    }

    /**
     * Cancel e-Invoice Service (asynchronously)
     * Cancel the invoice
     * @param authorization Bearer {auth} (required)
     * @param key Invoice Key (required)
     * @param body Cancel Message (required)
     * @param callback The callback to be executed when the API call finishes
     * @return The request call
     * @throws ApiException If fail to process the API call, e.g. serializing the request body object
     */
    public com.squareup.okhttp.Call cancelInvoiceServiceAsync(String authorization, String key, AbrasfItDeleteIn body, final ApiCallback<Void> callback) throws ApiException {

        ProgressResponseBody.ProgressListener progressListener = null;
        ProgressRequestBody.ProgressRequestListener progressRequestListener = null;

        if (callback != null) {
            progressListener = new ProgressResponseBody.ProgressListener() {
                @Override
                public void update(long bytesRead, long contentLength, boolean done) {
                    callback.onDownloadProgress(bytesRead, contentLength, done);
                }
            };

            progressRequestListener = new ProgressRequestBody.ProgressRequestListener() {
                @Override
                public void onRequestProgress(long bytesWritten, long contentLength, boolean done) {
                    callback.onUploadProgress(bytesWritten, contentLength, done);
                }
            };
        }

        com.squareup.okhttp.Call call = cancelInvoiceServiceValidateBeforeCall(authorization, key, body, progressListener, progressRequestListener);
        apiClient.executeAsync(call, callback);
        return call;
    }
    /* Build call for retrieveInvoiceService */
    private com.squareup.okhttp.Call retrieveInvoiceServiceCall(String authorization, String key, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        Object localVarPostBody = null;
        
        // create path and map variables
        String localVarPath = "/invoices/service/{key}".replaceAll("\\{format\\}","json")
        .replaceAll("\\{" + "key" + "\\}", apiClient.escapeString(key.toString()));

        List<Pair> localVarQueryParams = new ArrayList<Pair>();

        Map<String, String> localVarHeaderParams = new HashMap<String, String>();
        if (authorization != null)
        localVarHeaderParams.put("Authorization", apiClient.parameterToString(authorization));

        Map<String, Object> localVarFormParams = new HashMap<String, Object>();

        final String[] localVarAccepts = {
            "application/json"
        };
        final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept);

        final String[] localVarContentTypes = {
            "application/json"
        };
        final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        if(progressListener != null) {
            apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
                @Override
                public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain) throws IOException {
                    com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
                    return originalResponse.newBuilder()
                    .body(new ProgressResponseBody(originalResponse.body(), progressListener))
                    .build();
                }
            });
        }

        String[] localVarAuthNames = new String[] {  };
        return apiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
    }
    
    @SuppressWarnings("rawtypes")
    private com.squareup.okhttp.Call retrieveInvoiceServiceValidateBeforeCall(String authorization, String key, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        
        // verify the required parameter 'authorization' is set
        if (authorization == null) {
            throw new ApiException("Missing the required parameter 'authorization' when calling retrieveInvoiceService(Async)");
        }
        
        // verify the required parameter 'key' is set
        if (key == null) {
            throw new ApiException("Missing the required parameter 'key' when calling retrieveInvoiceService(Async)");
        }
        
        
        com.squareup.okhttp.Call call = retrieveInvoiceServiceCall(authorization, key, progressListener, progressRequestListener);
        return call;

        
        
        
        
    }

    /**
     * Retrieve e-Invoice Service
     * Retrieve the invoice
     * @param authorization Bearer {auth} (required)
     * @param key Invoice Key (required)
     * @return AbrasfItGetOut
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public AbrasfItGetOut retrieveInvoiceService(String authorization, String key) throws ApiException {
        ApiResponse<AbrasfItGetOut> resp = retrieveInvoiceServiceWithHttpInfo(authorization, key);
        return resp.getData();
    }

    /**
     * Retrieve e-Invoice Service
     * Retrieve the invoice
     * @param authorization Bearer {auth} (required)
     * @param key Invoice Key (required)
     * @return ApiResponse&lt;AbrasfItGetOut&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public ApiResponse<AbrasfItGetOut> retrieveInvoiceServiceWithHttpInfo(String authorization, String key) throws ApiException {
        com.squareup.okhttp.Call call = retrieveInvoiceServiceValidateBeforeCall(authorization, key, null, null);
        Type localVarReturnType = new TypeToken<AbrasfItGetOut>(){}.getType();
        return apiClient.execute(call, localVarReturnType);
    }

    /**
     * Retrieve e-Invoice Service (asynchronously)
     * Retrieve the invoice
     * @param authorization Bearer {auth} (required)
     * @param key Invoice Key (required)
     * @param callback The callback to be executed when the API call finishes
     * @return The request call
     * @throws ApiException If fail to process the API call, e.g. serializing the request body object
     */
    public com.squareup.okhttp.Call retrieveInvoiceServiceAsync(String authorization, String key, final ApiCallback<AbrasfItGetOut> callback) throws ApiException {

        ProgressResponseBody.ProgressListener progressListener = null;
        ProgressRequestBody.ProgressRequestListener progressRequestListener = null;

        if (callback != null) {
            progressListener = new ProgressResponseBody.ProgressListener() {
                @Override
                public void update(long bytesRead, long contentLength, boolean done) {
                    callback.onDownloadProgress(bytesRead, contentLength, done);
                }
            };

            progressRequestListener = new ProgressRequestBody.ProgressRequestListener() {
                @Override
                public void onRequestProgress(long bytesWritten, long contentLength, boolean done) {
                    callback.onUploadProgress(bytesWritten, contentLength, done);
                }
            };
        }

        com.squareup.okhttp.Call call = retrieveInvoiceServiceValidateBeforeCall(authorization, key, progressListener, progressRequestListener);
        Type localVarReturnType = new TypeToken<AbrasfItGetOut>(){}.getType();
        apiClient.executeAsync(call, localVarReturnType, callback);
        return call;
    }
    /* Build call for sendInvoiceService */
    private com.squareup.okhttp.Call sendInvoiceServiceCall(String authorization, TransactionForAbrasfList body, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        Object localVarPostBody = body;
        
        // create path and map variables
        String localVarPath = "/invoices/service".replaceAll("\\{format\\}","json");

        List<Pair> localVarQueryParams = new ArrayList<Pair>();

        Map<String, String> localVarHeaderParams = new HashMap<String, String>();
        if (authorization != null)
        localVarHeaderParams.put("Authorization", apiClient.parameterToString(authorization));

        Map<String, Object> localVarFormParams = new HashMap<String, Object>();

        final String[] localVarAccepts = {
            "application/json"
        };
        final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept);

        final String[] localVarContentTypes = {
            "application/json"
        };
        final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        if(progressListener != null) {
            apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
                @Override
                public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain) throws IOException {
                    com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
                    return originalResponse.newBuilder()
                    .body(new ProgressResponseBody(originalResponse.body(), progressListener))
                    .build();
                }
            });
        }

        String[] localVarAuthNames = new String[] {  };
        return apiClient.buildCall(localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
    }
    
    @SuppressWarnings("rawtypes")
    private com.squareup.okhttp.Call sendInvoiceServiceValidateBeforeCall(String authorization, TransactionForAbrasfList body, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        
        // verify the required parameter 'authorization' is set
        if (authorization == null) {
            throw new ApiException("Missing the required parameter 'authorization' when calling sendInvoiceService(Async)");
        }
        
        // verify the required parameter 'body' is set
        if (body == null) {
            throw new ApiException("Missing the required parameter 'body' when calling sendInvoiceService(Async)");
        }
        
        
        com.squareup.okhttp.Call call = sendInvoiceServiceCall(authorization, body, progressListener, progressRequestListener);
        return call;

        
        
        
        
    }

    /**
     * Send an e-Invoice Service
     * Send a invoice
     * @param authorization Bearer {auth} (required)
     * @param body Invoices (required)
     * @return AbrasfPostOut
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public AbrasfPostOut sendInvoiceService(String authorization, TransactionForAbrasfList body) throws ApiException {
        ApiResponse<AbrasfPostOut> resp = sendInvoiceServiceWithHttpInfo(authorization, body);
        return resp.getData();
    }

    /**
     * Send an e-Invoice Service
     * Send a invoice
     * @param authorization Bearer {auth} (required)
     * @param body Invoices (required)
     * @return ApiResponse&lt;AbrasfPostOut&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public ApiResponse<AbrasfPostOut> sendInvoiceServiceWithHttpInfo(String authorization, TransactionForAbrasfList body) throws ApiException {
        com.squareup.okhttp.Call call = sendInvoiceServiceValidateBeforeCall(authorization, body, null, null);
        Type localVarReturnType = new TypeToken<AbrasfPostOut>(){}.getType();
        return apiClient.execute(call, localVarReturnType);
    }

    /**
     * Send an e-Invoice Service (asynchronously)
     * Send a invoice
     * @param authorization Bearer {auth} (required)
     * @param body Invoices (required)
     * @param callback The callback to be executed when the API call finishes
     * @return The request call
     * @throws ApiException If fail to process the API call, e.g. serializing the request body object
     */
    public com.squareup.okhttp.Call sendInvoiceServiceAsync(String authorization, TransactionForAbrasfList body, final ApiCallback<AbrasfPostOut> callback) throws ApiException {

        ProgressResponseBody.ProgressListener progressListener = null;
        ProgressRequestBody.ProgressRequestListener progressRequestListener = null;

        if (callback != null) {
            progressListener = new ProgressResponseBody.ProgressListener() {
                @Override
                public void update(long bytesRead, long contentLength, boolean done) {
                    callback.onDownloadProgress(bytesRead, contentLength, done);
                }
            };

            progressRequestListener = new ProgressRequestBody.ProgressRequestListener() {
                @Override
                public void onRequestProgress(long bytesWritten, long contentLength, boolean done) {
                    callback.onUploadProgress(bytesWritten, contentLength, done);
                }
            };
        }

        com.squareup.okhttp.Call call = sendInvoiceServiceValidateBeforeCall(authorization, body, progressListener, progressRequestListener);
        Type localVarReturnType = new TypeToken<AbrasfPostOut>(){}.getType();
        apiClient.executeAsync(call, localVarReturnType, callback);
        return call;
    }
}
