/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 *
 * Code generated by Microsoft (R) AutoRest Code Generator.
 */

package com.microsoft.azure.management.website.implementation;

import retrofit2.Retrofit;
import com.google.common.reflect.TypeToken;
import com.microsoft.azure.AzureServiceResponseBuilder;
import com.microsoft.azure.CloudException;
import com.microsoft.rest.ServiceCall;
import com.microsoft.rest.ServiceCallback;
import com.microsoft.rest.ServiceResponse;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.Path;
import retrofit2.http.Query;
import retrofit2.Response;
import rx.functions.Func1;
import rx.Observable;

/**
 * An instance of this class provides access to all the operations defined
 * in Usages.
 */
public final class UsagesInner {
    /** The Retrofit service to perform REST calls. */
    private UsagesService service;
    /** The service client containing this operation class. */
    private WebSiteManagementClientImpl client;

    /**
     * Initializes an instance of UsagesInner.
     *
     * @param retrofit the Retrofit instance built from a Retrofit Builder.
     * @param client the instance of the service client containing this operation class.
     */
    public UsagesInner(Retrofit retrofit, WebSiteManagementClientImpl client) {
        this.service = retrofit.create(UsagesService.class);
        this.client = client;
    }

    /**
     * The interface defining all the services for Usages to be
     * used by Retrofit to perform actually REST calls.
     */
    interface UsagesService {
        @Headers("Content-Type: application/json; charset=utf-8")
        @GET("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web.Admin/environments/{environmentName}/usage")
        Observable<Response<ResponseBody>> getUsage(@Path("resourceGroupName") String resourceGroupName, @Path("environmentName") String environmentName, @Path("subscriptionId") String subscriptionId, @Query("lastId") String lastId, @Query("batchSize") int batchSize, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

    }

    /**
     * Returns usage records for specified subscription and resource groups.
     *
     * @param resourceGroupName Name of resource group
     * @param environmentName Environment name
     * @param lastId Last marker that was returned from the batch
     * @param batchSize size of the batch to be returned.
     * @return the Object object if successful.
     */
    public Object getUsage(String resourceGroupName, String environmentName, String lastId, int batchSize) {
        return getUsageWithServiceResponseAsync(resourceGroupName, environmentName, lastId, batchSize).toBlocking().single().getBody();
    }

    /**
     * Returns usage records for specified subscription and resource groups.
     *
     * @param resourceGroupName Name of resource group
     * @param environmentName Environment name
     * @param lastId Last marker that was returned from the batch
     * @param batchSize size of the batch to be returned.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @return the {@link ServiceCall} object
     */
    public ServiceCall<Object> getUsageAsync(String resourceGroupName, String environmentName, String lastId, int batchSize, final ServiceCallback<Object> serviceCallback) {
        return ServiceCall.create(getUsageWithServiceResponseAsync(resourceGroupName, environmentName, lastId, batchSize), serviceCallback);
    }

    /**
     * Returns usage records for specified subscription and resource groups.
     *
     * @param resourceGroupName Name of resource group
     * @param environmentName Environment name
     * @param lastId Last marker that was returned from the batch
     * @param batchSize size of the batch to be returned.
     * @return the observable to the Object object
     */
    public Observable<Object> getUsageAsync(String resourceGroupName, String environmentName, String lastId, int batchSize) {
        return getUsageWithServiceResponseAsync(resourceGroupName, environmentName, lastId, batchSize).map(new Func1<ServiceResponse<Object>, Object>() {
            @Override
            public Object call(ServiceResponse<Object> response) {
                return response.getBody();
            }
        });
    }

    /**
     * Returns usage records for specified subscription and resource groups.
     *
     * @param resourceGroupName Name of resource group
     * @param environmentName Environment name
     * @param lastId Last marker that was returned from the batch
     * @param batchSize size of the batch to be returned.
     * @return the observable to the Object object
     */
    public Observable<ServiceResponse<Object>> getUsageWithServiceResponseAsync(String resourceGroupName, String environmentName, String lastId, int batchSize) {
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (environmentName == null) {
            throw new IllegalArgumentException("Parameter environmentName is required and cannot be null.");
        }
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (lastId == null) {
            throw new IllegalArgumentException("Parameter lastId is required and cannot be null.");
        }
        if (this.client.apiVersion() == null) {
            throw new IllegalArgumentException("Parameter this.client.apiVersion() is required and cannot be null.");
        }
        return service.getUsage(resourceGroupName, environmentName, this.client.subscriptionId(), lastId, batchSize, this.client.apiVersion(), this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Object>>>() {
                @Override
                public Observable<ServiceResponse<Object>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<Object> clientResponse = getUsageDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<Object> getUsageDelegate(Response<ResponseBody> response) throws CloudException, IOException, IllegalArgumentException {
        return new AzureServiceResponseBuilder<Object, CloudException>(this.client.mapperAdapter())
                .register(200, new TypeToken<Object>() { }.getType())
                .registerError(CloudException.class)
                .build(response);
    }

}
