/*
 * Bybit API
 * ## REST API for the Bybit Exchange. Base URI: [https://api.bybit.com]  
 *
 * OpenAPI spec version: 0.2.10
 * Contact: support@bybit.com
 *
 * 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 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 com.google.gson.reflect.TypeToken;

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;

public class ExecutionApi {
    private ApiClient apiClient;

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

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

    public ApiClient getApiClient() {
        return apiClient;
    }

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

    /**
     * Build call for executionGetTrades
     * @param orderId OrderID. If not provided, will return user’s trading records. (optional)
     * @param symbol Contract type. If order_id not provided, symbol is required. (optional)
     * @param startTime Start timestamp point for result. (optional)
     * @param page Page. Default getting first page data. (optional)
     * @param limit Limit for data size per page, max size is 50. Default as showing 20 pieces of data per page. (optional)
     * @param progressListener Progress listener
     * @param progressRequestListener Progress request listener
     * @return Call to execute
     * @throws ApiException If fail to serialize the request body object
     */
    public com.squareup.okhttp.Call executionGetTradesCall(String orderId, String symbol, String startTime, String page, String limit, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        Object localVarPostBody = null;

        // create path and map variables
        String localVarPath = "/v2/private/execution/list";

        List<Pair> localVarQueryParams = new ArrayList<Pair>();
        List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
        if (orderId != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("order_id", orderId));
        if (symbol != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("symbol", symbol));
        if (startTime != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("start_time", startTime));
        if (page != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("page", page));
        if (limit != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("limit", limit));

        Map<String, String> localVarHeaderParams = new HashMap<String, String>();

        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", "application/x-www-form-urlencoded"
        };
        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[] { "apiKey", "apiSignature", "timestamp" };
        return apiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
    }

    @SuppressWarnings("rawtypes")
    private com.squareup.okhttp.Call executionGetTradesValidateBeforeCall(String orderId, String symbol, String startTime, String page, String limit, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        

        com.squareup.okhttp.Call call = executionGetTradesCall(orderId, symbol, startTime, page, limit, progressListener, progressRequestListener);
        return call;

    }

    /**
     * Get user’s trade records.
     * 
     * @param orderId OrderID. If not provided, will return user’s trading records. (optional)
     * @param symbol Contract type. If order_id not provided, symbol is required. (optional)
     * @param startTime Start timestamp point for result. (optional)
     * @param page Page. Default getting first page data. (optional)
     * @param limit Limit for data size per page, max size is 50. Default as showing 20 pieces of data per page. (optional)
     * @return Object
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public Object executionGetTrades(String orderId, String symbol, String startTime, String page, String limit) throws ApiException {
        ApiResponse<Object> resp = executionGetTradesWithHttpInfo(orderId, symbol, startTime, page, limit);
        return resp.getData();
    }

    /**
     * Get user’s trade records.
     * 
     * @param orderId OrderID. If not provided, will return user’s trading records. (optional)
     * @param symbol Contract type. If order_id not provided, symbol is required. (optional)
     * @param startTime Start timestamp point for result. (optional)
     * @param page Page. Default getting first page data. (optional)
     * @param limit Limit for data size per page, max size is 50. Default as showing 20 pieces of data per page. (optional)
     * @return ApiResponse&lt;Object&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public ApiResponse<Object> executionGetTradesWithHttpInfo(String orderId, String symbol, String startTime, String page, String limit) throws ApiException {
        com.squareup.okhttp.Call call = executionGetTradesValidateBeforeCall(orderId, symbol, startTime, page, limit, null, null);
        Type localVarReturnType = new TypeToken<Object>(){}.getType();
        return apiClient.execute(call, localVarReturnType);
    }

    /**
     * Get user’s trade records. (asynchronously)
     * 
     * @param orderId OrderID. If not provided, will return user’s trading records. (optional)
     * @param symbol Contract type. If order_id not provided, symbol is required. (optional)
     * @param startTime Start timestamp point for result. (optional)
     * @param page Page. Default getting first page data. (optional)
     * @param limit Limit for data size per page, max size is 50. Default as showing 20 pieces of data per page. (optional)
     * @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 executionGetTradesAsync(String orderId, String symbol, String startTime, String page, String limit, final ApiCallback<Object> 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 = executionGetTradesValidateBeforeCall(orderId, symbol, startTime, page, limit, progressListener, progressRequestListener);
        Type localVarReturnType = new TypeToken<Object>(){}.getType();
        apiClient.executeAsync(call, localVarReturnType, callback);
        return call;
    }
    /**
     * Build call for positionsClosePnlRecords
     * @param symbol Contract type (required)
     * @param startTime Start timestamp point for result, in second (optional)
     * @param endTime End timestamp point for result, in second (optional)
     * @param execType Execution type (optional)
     * @param page Page. By default, gets first page of data. Maximum of 50 pages (optional)
     * @param limit Limit for data size per page, max size is 50. Default as showing 20 pieces of data per page. (optional)
     * @param progressListener Progress listener
     * @param progressRequestListener Progress request listener
     * @return Call to execute
     * @throws ApiException If fail to serialize the request body object
     */
    public com.squareup.okhttp.Call positionsClosePnlRecordsCall(String symbol, Integer startTime, Integer endTime, String execType, Integer page, Integer limit, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        Object localVarPostBody = null;

        // create path and map variables
        String localVarPath = "/v2/private/trade/closed-pnl/list";

        List<Pair> localVarQueryParams = new ArrayList<Pair>();
        List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
        if (symbol != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("symbol", symbol));
        if (startTime != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("start_time", startTime));
        if (endTime != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("end_time", endTime));
        if (execType != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("exec_type", execType));
        if (page != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("page", page));
        if (limit != null)
        localVarQueryParams.addAll(apiClient.parameterToPair("limit", limit));

        Map<String, String> localVarHeaderParams = new HashMap<String, String>();

        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", "application/x-www-form-urlencoded"
        };
        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[] { "apiKey", "apiSignature", "timestamp" };
        return apiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
    }

    @SuppressWarnings("rawtypes")
    private com.squareup.okhttp.Call positionsClosePnlRecordsValidateBeforeCall(String symbol, Integer startTime, Integer endTime, String execType, Integer page, Integer limit, final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        
        // verify the required parameter 'symbol' is set
        if (symbol == null) {
            throw new ApiException("Missing the required parameter 'symbol' when calling positionsClosePnlRecords(Async)");
        }
        

        com.squareup.okhttp.Call call = positionsClosePnlRecordsCall(symbol, startTime, endTime, execType, page, limit, progressListener, progressRequestListener);
        return call;

    }

    /**
     * Get user&#39;s closed profit and loss records
     * 
     * @param symbol Contract type (required)
     * @param startTime Start timestamp point for result, in second (optional)
     * @param endTime End timestamp point for result, in second (optional)
     * @param execType Execution type (optional)
     * @param page Page. By default, gets first page of data. Maximum of 50 pages (optional)
     * @param limit Limit for data size per page, max size is 50. Default as showing 20 pieces of data per page. (optional)
     * @return Object
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public Object positionsClosePnlRecords(String symbol, Integer startTime, Integer endTime, String execType, Integer page, Integer limit) throws ApiException {
        ApiResponse<Object> resp = positionsClosePnlRecordsWithHttpInfo(symbol, startTime, endTime, execType, page, limit);
        return resp.getData();
    }

    /**
     * Get user&#39;s closed profit and loss records
     * 
     * @param symbol Contract type (required)
     * @param startTime Start timestamp point for result, in second (optional)
     * @param endTime End timestamp point for result, in second (optional)
     * @param execType Execution type (optional)
     * @param page Page. By default, gets first page of data. Maximum of 50 pages (optional)
     * @param limit Limit for data size per page, max size is 50. Default as showing 20 pieces of data per page. (optional)
     * @return ApiResponse&lt;Object&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public ApiResponse<Object> positionsClosePnlRecordsWithHttpInfo(String symbol, Integer startTime, Integer endTime, String execType, Integer page, Integer limit) throws ApiException {
        com.squareup.okhttp.Call call = positionsClosePnlRecordsValidateBeforeCall(symbol, startTime, endTime, execType, page, limit, null, null);
        Type localVarReturnType = new TypeToken<Object>(){}.getType();
        return apiClient.execute(call, localVarReturnType);
    }

    /**
     * Get user&#39;s closed profit and loss records (asynchronously)
     * 
     * @param symbol Contract type (required)
     * @param startTime Start timestamp point for result, in second (optional)
     * @param endTime End timestamp point for result, in second (optional)
     * @param execType Execution type (optional)
     * @param page Page. By default, gets first page of data. Maximum of 50 pages (optional)
     * @param limit Limit for data size per page, max size is 50. Default as showing 20 pieces of data per page. (optional)
     * @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 positionsClosePnlRecordsAsync(String symbol, Integer startTime, Integer endTime, String execType, Integer page, Integer limit, final ApiCallback<Object> 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 = positionsClosePnlRecordsValidateBeforeCall(symbol, startTime, endTime, execType, page, limit, progressListener, progressRequestListener);
        Type localVarReturnType = new TypeToken<Object>(){}.getType();
        apiClient.executeAsync(call, localVarReturnType, callback);
        return call;
    }
}
