/**
 * 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.recoveryservices.v2016_06_01.implementation;

import retrofit2.Retrofit;
import com.google.common.reflect.TypeToken;
import com.microsoft.azure.CloudException;
import com.microsoft.rest.ServiceCallback;
import com.microsoft.rest.ServiceFuture;
import com.microsoft.rest.ServiceResponse;
import com.microsoft.rest.Validator;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.PATCH;
import retrofit2.http.Path;
import retrofit2.http.PUT;
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 VaultExtendedInfos.
 */
public class VaultExtendedInfosInner {
    /** The Retrofit service to perform REST calls. */
    private VaultExtendedInfosService service;
    /** The service client containing this operation class. */
    private RecoveryServicesClientImpl client;

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

    /**
     * The interface defining all the services for VaultExtendedInfos to be
     * used by Retrofit to perform actually REST calls.
     */
    interface VaultExtendedInfosService {
        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.recoveryservices.v2016_06_01.VaultExtendedInfos get" })
        @GET("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/extendedInformation/vaultExtendedInfo")
        Observable<Response<ResponseBody>> get(@Path("subscriptionId") String subscriptionId, @Path("resourceGroupName") String resourceGroupName, @Path("vaultName") String vaultName, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.recoveryservices.v2016_06_01.VaultExtendedInfos createOrUpdate" })
        @PUT("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/extendedInformation/vaultExtendedInfo")
        Observable<Response<ResponseBody>> createOrUpdate(@Path("subscriptionId") String subscriptionId, @Path("resourceGroupName") String resourceGroupName, @Path("vaultName") String vaultName, @Query("api-version") String apiVersion, @Body VaultExtendedInfoResourceInner resourceResourceExtendedInfoDetails, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.recoveryservices.v2016_06_01.VaultExtendedInfos update" })
        @PATCH("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/extendedInformation/vaultExtendedInfo")
        Observable<Response<ResponseBody>> update(@Path("subscriptionId") String subscriptionId, @Path("resourceGroupName") String resourceGroupName, @Path("vaultName") String vaultName, @Query("api-version") String apiVersion, @Body VaultExtendedInfoResourceInner resourceResourceExtendedInfoDetails, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

    }

    /**
     * Get the vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws CloudException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the VaultExtendedInfoResourceInner object if successful.
     */
    public VaultExtendedInfoResourceInner get(String resourceGroupName, String vaultName) {
        return getWithServiceResponseAsync(resourceGroupName, vaultName).toBlocking().single().body();
    }

    /**
     * Get the vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<VaultExtendedInfoResourceInner> getAsync(String resourceGroupName, String vaultName, final ServiceCallback<VaultExtendedInfoResourceInner> serviceCallback) {
        return ServiceFuture.fromResponse(getWithServiceResponseAsync(resourceGroupName, vaultName), serviceCallback);
    }

    /**
     * Get the vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the VaultExtendedInfoResourceInner object
     */
    public Observable<VaultExtendedInfoResourceInner> getAsync(String resourceGroupName, String vaultName) {
        return getWithServiceResponseAsync(resourceGroupName, vaultName).map(new Func1<ServiceResponse<VaultExtendedInfoResourceInner>, VaultExtendedInfoResourceInner>() {
            @Override
            public VaultExtendedInfoResourceInner call(ServiceResponse<VaultExtendedInfoResourceInner> response) {
                return response.body();
            }
        });
    }

    /**
     * Get the vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the VaultExtendedInfoResourceInner object
     */
    public Observable<ServiceResponse<VaultExtendedInfoResourceInner>> getWithServiceResponseAsync(String resourceGroupName, String vaultName) {
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (vaultName == null) {
            throw new IllegalArgumentException("Parameter vaultName 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.get(this.client.subscriptionId(), resourceGroupName, vaultName, this.client.apiVersion(), this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<VaultExtendedInfoResourceInner>>>() {
                @Override
                public Observable<ServiceResponse<VaultExtendedInfoResourceInner>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<VaultExtendedInfoResourceInner> clientResponse = getDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<VaultExtendedInfoResourceInner> getDelegate(Response<ResponseBody> response) throws CloudException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<VaultExtendedInfoResourceInner, CloudException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<VaultExtendedInfoResourceInner>() { }.getType())
                .registerError(CloudException.class)
                .build(response);
    }

    /**
     * Create vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @param resourceResourceExtendedInfoDetails Details of ResourceExtendedInfo
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws CloudException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the VaultExtendedInfoResourceInner object if successful.
     */
    public VaultExtendedInfoResourceInner createOrUpdate(String resourceGroupName, String vaultName, VaultExtendedInfoResourceInner resourceResourceExtendedInfoDetails) {
        return createOrUpdateWithServiceResponseAsync(resourceGroupName, vaultName, resourceResourceExtendedInfoDetails).toBlocking().single().body();
    }

    /**
     * Create vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @param resourceResourceExtendedInfoDetails Details of ResourceExtendedInfo
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<VaultExtendedInfoResourceInner> createOrUpdateAsync(String resourceGroupName, String vaultName, VaultExtendedInfoResourceInner resourceResourceExtendedInfoDetails, final ServiceCallback<VaultExtendedInfoResourceInner> serviceCallback) {
        return ServiceFuture.fromResponse(createOrUpdateWithServiceResponseAsync(resourceGroupName, vaultName, resourceResourceExtendedInfoDetails), serviceCallback);
    }

    /**
     * Create vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @param resourceResourceExtendedInfoDetails Details of ResourceExtendedInfo
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the VaultExtendedInfoResourceInner object
     */
    public Observable<VaultExtendedInfoResourceInner> createOrUpdateAsync(String resourceGroupName, String vaultName, VaultExtendedInfoResourceInner resourceResourceExtendedInfoDetails) {
        return createOrUpdateWithServiceResponseAsync(resourceGroupName, vaultName, resourceResourceExtendedInfoDetails).map(new Func1<ServiceResponse<VaultExtendedInfoResourceInner>, VaultExtendedInfoResourceInner>() {
            @Override
            public VaultExtendedInfoResourceInner call(ServiceResponse<VaultExtendedInfoResourceInner> response) {
                return response.body();
            }
        });
    }

    /**
     * Create vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @param resourceResourceExtendedInfoDetails Details of ResourceExtendedInfo
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the VaultExtendedInfoResourceInner object
     */
    public Observable<ServiceResponse<VaultExtendedInfoResourceInner>> createOrUpdateWithServiceResponseAsync(String resourceGroupName, String vaultName, VaultExtendedInfoResourceInner resourceResourceExtendedInfoDetails) {
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (vaultName == null) {
            throw new IllegalArgumentException("Parameter vaultName is required and cannot be null.");
        }
        if (this.client.apiVersion() == null) {
            throw new IllegalArgumentException("Parameter this.client.apiVersion() is required and cannot be null.");
        }
        if (resourceResourceExtendedInfoDetails == null) {
            throw new IllegalArgumentException("Parameter resourceResourceExtendedInfoDetails is required and cannot be null.");
        }
        Validator.validate(resourceResourceExtendedInfoDetails);
        return service.createOrUpdate(this.client.subscriptionId(), resourceGroupName, vaultName, this.client.apiVersion(), resourceResourceExtendedInfoDetails, this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<VaultExtendedInfoResourceInner>>>() {
                @Override
                public Observable<ServiceResponse<VaultExtendedInfoResourceInner>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<VaultExtendedInfoResourceInner> clientResponse = createOrUpdateDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<VaultExtendedInfoResourceInner> createOrUpdateDelegate(Response<ResponseBody> response) throws CloudException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<VaultExtendedInfoResourceInner, CloudException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<VaultExtendedInfoResourceInner>() { }.getType())
                .registerError(CloudException.class)
                .build(response);
    }

    /**
     * Update vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @param resourceResourceExtendedInfoDetails Details of ResourceExtendedInfo
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws CloudException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the VaultExtendedInfoResourceInner object if successful.
     */
    public VaultExtendedInfoResourceInner update(String resourceGroupName, String vaultName, VaultExtendedInfoResourceInner resourceResourceExtendedInfoDetails) {
        return updateWithServiceResponseAsync(resourceGroupName, vaultName, resourceResourceExtendedInfoDetails).toBlocking().single().body();
    }

    /**
     * Update vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @param resourceResourceExtendedInfoDetails Details of ResourceExtendedInfo
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<VaultExtendedInfoResourceInner> updateAsync(String resourceGroupName, String vaultName, VaultExtendedInfoResourceInner resourceResourceExtendedInfoDetails, final ServiceCallback<VaultExtendedInfoResourceInner> serviceCallback) {
        return ServiceFuture.fromResponse(updateWithServiceResponseAsync(resourceGroupName, vaultName, resourceResourceExtendedInfoDetails), serviceCallback);
    }

    /**
     * Update vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @param resourceResourceExtendedInfoDetails Details of ResourceExtendedInfo
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the VaultExtendedInfoResourceInner object
     */
    public Observable<VaultExtendedInfoResourceInner> updateAsync(String resourceGroupName, String vaultName, VaultExtendedInfoResourceInner resourceResourceExtendedInfoDetails) {
        return updateWithServiceResponseAsync(resourceGroupName, vaultName, resourceResourceExtendedInfoDetails).map(new Func1<ServiceResponse<VaultExtendedInfoResourceInner>, VaultExtendedInfoResourceInner>() {
            @Override
            public VaultExtendedInfoResourceInner call(ServiceResponse<VaultExtendedInfoResourceInner> response) {
                return response.body();
            }
        });
    }

    /**
     * Update vault extended info.
     *
     * @param resourceGroupName The name of the resource group where the recovery services vault is present.
     * @param vaultName The name of the recovery services vault.
     * @param resourceResourceExtendedInfoDetails Details of ResourceExtendedInfo
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the VaultExtendedInfoResourceInner object
     */
    public Observable<ServiceResponse<VaultExtendedInfoResourceInner>> updateWithServiceResponseAsync(String resourceGroupName, String vaultName, VaultExtendedInfoResourceInner resourceResourceExtendedInfoDetails) {
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (vaultName == null) {
            throw new IllegalArgumentException("Parameter vaultName is required and cannot be null.");
        }
        if (this.client.apiVersion() == null) {
            throw new IllegalArgumentException("Parameter this.client.apiVersion() is required and cannot be null.");
        }
        if (resourceResourceExtendedInfoDetails == null) {
            throw new IllegalArgumentException("Parameter resourceResourceExtendedInfoDetails is required and cannot be null.");
        }
        Validator.validate(resourceResourceExtendedInfoDetails);
        return service.update(this.client.subscriptionId(), resourceGroupName, vaultName, this.client.apiVersion(), resourceResourceExtendedInfoDetails, this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<VaultExtendedInfoResourceInner>>>() {
                @Override
                public Observable<ServiceResponse<VaultExtendedInfoResourceInner>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<VaultExtendedInfoResourceInner> clientResponse = updateDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<VaultExtendedInfoResourceInner> updateDelegate(Response<ResponseBody> response) throws CloudException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<VaultExtendedInfoResourceInner, CloudException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<VaultExtendedInfoResourceInner>() { }.getType())
                .registerError(CloudException.class)
                .build(response);
    }

}
