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

package com.azure.resourcemanager.networkcloud.implementation;

import com.azure.core.http.rest.PagedIterable;
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.networkcloud.fluent.VirtualMachinesClient;
import com.azure.resourcemanager.networkcloud.fluent.models.VirtualMachineInner;
import com.azure.resourcemanager.networkcloud.models.VirtualMachine;
import com.azure.resourcemanager.networkcloud.models.VirtualMachinePowerOffParameters;
import com.azure.resourcemanager.networkcloud.models.VirtualMachineVolumeParameters;
import com.azure.resourcemanager.networkcloud.models.VirtualMachines;

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

    private final VirtualMachinesClient innerClient;

    private final com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager;

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

    public PagedIterable<VirtualMachine> list() {
        PagedIterable<VirtualMachineInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new VirtualMachineImpl(inner1, this.manager()));
    }

    public PagedIterable<VirtualMachine> list(Context context) {
        PagedIterable<VirtualMachineInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new VirtualMachineImpl(inner1, this.manager()));
    }

    public PagedIterable<VirtualMachine> listByResourceGroup(String resourceGroupName) {
        PagedIterable<VirtualMachineInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new VirtualMachineImpl(inner1, this.manager()));
    }

    public PagedIterable<VirtualMachine> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<VirtualMachineInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new VirtualMachineImpl(inner1, this.manager()));
    }

    public Response<VirtualMachine> getByResourceGroupWithResponse(
        String resourceGroupName, String virtualMachineName, Context context) {
        Response<VirtualMachineInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, virtualMachineName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new VirtualMachineImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public VirtualMachine getByResourceGroup(String resourceGroupName, String virtualMachineName) {
        VirtualMachineInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, virtualMachineName);
        if (inner != null) {
            return new VirtualMachineImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void deleteByResourceGroup(String resourceGroupName, String virtualMachineName) {
        this.serviceClient().delete(resourceGroupName, virtualMachineName);
    }

    public void delete(String resourceGroupName, String virtualMachineName, Context context) {
        this.serviceClient().delete(resourceGroupName, virtualMachineName, context);
    }

    public void attachVolume(
        String resourceGroupName,
        String virtualMachineName,
        VirtualMachineVolumeParameters virtualMachineAttachVolumeParameters) {
        this.serviceClient().attachVolume(resourceGroupName, virtualMachineName, virtualMachineAttachVolumeParameters);
    }

    public void attachVolume(
        String resourceGroupName,
        String virtualMachineName,
        VirtualMachineVolumeParameters virtualMachineAttachVolumeParameters,
        Context context) {
        this
            .serviceClient()
            .attachVolume(resourceGroupName, virtualMachineName, virtualMachineAttachVolumeParameters, context);
    }

    public void detachVolume(
        String resourceGroupName,
        String virtualMachineName,
        VirtualMachineVolumeParameters virtualMachineDetachVolumeParameters) {
        this.serviceClient().detachVolume(resourceGroupName, virtualMachineName, virtualMachineDetachVolumeParameters);
    }

    public void detachVolume(
        String resourceGroupName,
        String virtualMachineName,
        VirtualMachineVolumeParameters virtualMachineDetachVolumeParameters,
        Context context) {
        this
            .serviceClient()
            .detachVolume(resourceGroupName, virtualMachineName, virtualMachineDetachVolumeParameters, context);
    }

    public void powerOff(String resourceGroupName, String virtualMachineName) {
        this.serviceClient().powerOff(resourceGroupName, virtualMachineName);
    }

    public void powerOff(
        String resourceGroupName,
        String virtualMachineName,
        VirtualMachinePowerOffParameters virtualMachinePowerOffParameters,
        Context context) {
        this.serviceClient().powerOff(resourceGroupName, virtualMachineName, virtualMachinePowerOffParameters, context);
    }

    public void reimage(String resourceGroupName, String virtualMachineName) {
        this.serviceClient().reimage(resourceGroupName, virtualMachineName);
    }

    public void reimage(String resourceGroupName, String virtualMachineName, Context context) {
        this.serviceClient().reimage(resourceGroupName, virtualMachineName, context);
    }

    public void restart(String resourceGroupName, String virtualMachineName) {
        this.serviceClient().restart(resourceGroupName, virtualMachineName);
    }

    public void restart(String resourceGroupName, String virtualMachineName, Context context) {
        this.serviceClient().restart(resourceGroupName, virtualMachineName, context);
    }

    public void start(String resourceGroupName, String virtualMachineName) {
        this.serviceClient().start(resourceGroupName, virtualMachineName);
    }

    public void start(String resourceGroupName, String virtualMachineName, Context context) {
        this.serviceClient().start(resourceGroupName, virtualMachineName, context);
    }

    public VirtualMachine getById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String virtualMachineName = Utils.getValueFromIdByName(id, "virtualMachines");
        if (virtualMachineName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'virtualMachines'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, virtualMachineName, Context.NONE).getValue();
    }

    public Response<VirtualMachine> getByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String virtualMachineName = Utils.getValueFromIdByName(id, "virtualMachines");
        if (virtualMachineName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'virtualMachines'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, virtualMachineName, context);
    }

    public void deleteById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String virtualMachineName = Utils.getValueFromIdByName(id, "virtualMachines");
        if (virtualMachineName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'virtualMachines'.", id)));
        }
        this.delete(resourceGroupName, virtualMachineName, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String virtualMachineName = Utils.getValueFromIdByName(id, "virtualMachines");
        if (virtualMachineName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'virtualMachines'.", id)));
        }
        this.delete(resourceGroupName, virtualMachineName, context);
    }

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

    private com.azure.resourcemanager.networkcloud.NetworkCloudManager manager() {
        return this.serviceManager;
    }

    public VirtualMachineImpl define(String name) {
        return new VirtualMachineImpl(name, this.manager());
    }
}
