// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.hdinsight.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.hdinsight.fluent.VirtualMachinesClient;
import com.azure.resourcemanager.hdinsight.fluent.models.AsyncOperationResultInner;
import com.azure.resourcemanager.hdinsight.fluent.models.HostInfoInner;
import com.azure.resourcemanager.hdinsight.models.AsyncOperationResult;
import com.azure.resourcemanager.hdinsight.models.HostInfo;
import com.azure.resourcemanager.hdinsight.models.VirtualMachines;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public final class VirtualMachinesImpl implements VirtualMachines {
    private static final ClientLogger LOGGER = new ClientLogger(VirtualMachinesImpl.class);

    private final VirtualMachinesClient innerClient;

    private final com.azure.resourcemanager.hdinsight.HDInsightManager serviceManager;

    public VirtualMachinesImpl(
        VirtualMachinesClient innerClient, com.azure.resourcemanager.hdinsight.HDInsightManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<List<HostInfo>> listHostsWithResponse(
        String resourceGroupName, String clusterName, Context context) {
        Response<List<HostInfoInner>> inner =
            this.serviceClient().listHostsWithResponse(resourceGroupName, clusterName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                inner
                    .getValue()
                    .stream()
                    .map(inner1 -> new HostInfoImpl(inner1, this.manager()))
                    .collect(Collectors.toList()));
        } else {
            return null;
        }
    }

    public List<HostInfo> listHosts(String resourceGroupName, String clusterName) {
        List<HostInfoInner> inner = this.serviceClient().listHosts(resourceGroupName, clusterName);
        if (inner != null) {
            return Collections
                .unmodifiableList(
                    inner
                        .stream()
                        .map(inner1 -> new HostInfoImpl(inner1, this.manager()))
                        .collect(Collectors.toList()));
        } else {
            return Collections.emptyList();
        }
    }

    public void restartHosts(String resourceGroupName, String clusterName, List<String> hosts) {
        this.serviceClient().restartHosts(resourceGroupName, clusterName, hosts);
    }

    public void restartHosts(String resourceGroupName, String clusterName, List<String> hosts, Context context) {
        this.serviceClient().restartHosts(resourceGroupName, clusterName, hosts, context);
    }

    public Response<AsyncOperationResult> getAsyncOperationStatusWithResponse(
        String resourceGroupName, String clusterName, String operationId, Context context) {
        Response<AsyncOperationResultInner> inner =
            this
                .serviceClient()
                .getAsyncOperationStatusWithResponse(resourceGroupName, clusterName, operationId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AsyncOperationResultImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public AsyncOperationResult getAsyncOperationStatus(
        String resourceGroupName, String clusterName, String operationId) {
        AsyncOperationResultInner inner =
            this.serviceClient().getAsyncOperationStatus(resourceGroupName, clusterName, operationId);
        if (inner != null) {
            return new AsyncOperationResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    private VirtualMachinesClient serviceClient() {
        return this.innerClient;
    }

    private com.azure.resourcemanager.hdinsight.HDInsightManager manager() {
        return this.serviceManager;
    }
}
