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

package com.azure.resourcemanager.connectedvmware.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.connectedvmware.fluent.VirtualMachinesClient;
import com.azure.resourcemanager.connectedvmware.fluent.models.VirtualMachineAssessPatchesResultInner;
import com.azure.resourcemanager.connectedvmware.fluent.models.VirtualMachineInner;
import com.azure.resourcemanager.connectedvmware.fluent.models.VirtualMachineInstallPatchesResultInner;
import com.azure.resourcemanager.connectedvmware.models.StopVirtualMachineOptions;
import com.azure.resourcemanager.connectedvmware.models.VirtualMachine;
import com.azure.resourcemanager.connectedvmware.models.VirtualMachineAssessPatchesResult;
import com.azure.resourcemanager.connectedvmware.models.VirtualMachineInstallPatchesParameters;
import com.azure.resourcemanager.connectedvmware.models.VirtualMachineInstallPatchesResult;
import com.azure.resourcemanager.connectedvmware.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.connectedvmware.ConnectedVMwareManager serviceManager;

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

    public VirtualMachineAssessPatchesResult assessPatches(String resourceGroupName, String name) {
        VirtualMachineAssessPatchesResultInner inner = this.serviceClient().assessPatches(resourceGroupName, name);
        if (inner != null) {
            return new VirtualMachineAssessPatchesResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public VirtualMachineAssessPatchesResult assessPatches(String resourceGroupName, String name, Context context) {
        VirtualMachineAssessPatchesResultInner inner =
            this.serviceClient().assessPatches(resourceGroupName, name, context);
        if (inner != null) {
            return new VirtualMachineAssessPatchesResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public VirtualMachineInstallPatchesResult installPatches(
        String resourceGroupName, String name, VirtualMachineInstallPatchesParameters installPatchesInput) {
        VirtualMachineInstallPatchesResultInner inner =
            this.serviceClient().installPatches(resourceGroupName, name, installPatchesInput);
        if (inner != null) {
            return new VirtualMachineInstallPatchesResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public VirtualMachineInstallPatchesResult installPatches(
        String resourceGroupName,
        String name,
        VirtualMachineInstallPatchesParameters installPatchesInput,
        Context context) {
        VirtualMachineInstallPatchesResultInner inner =
            this.serviceClient().installPatches(resourceGroupName, name, installPatchesInput, context);
        if (inner != null) {
            return new VirtualMachineInstallPatchesResultImpl(inner, 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 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 void delete(String resourceGroupName, String virtualMachineName, Boolean force, Boolean retain) {
        this.serviceClient().delete(resourceGroupName, virtualMachineName, force, retain);
    }

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

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

    public void stop(String resourceGroupName, String virtualMachineName, StopVirtualMachineOptions body) {
        this.serviceClient().stop(resourceGroupName, virtualMachineName, body);
    }

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

    public void stop(
        String resourceGroupName, String virtualMachineName, StopVirtualMachineOptions body, Context context) {
        this.serviceClient().stop(resourceGroupName, virtualMachineName, body, 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 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 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 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)));
        }
        Boolean localForce = null;
        Boolean localRetain = null;
        this.delete(resourceGroupName, virtualMachineName, localForce, localRetain, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, Boolean force, Boolean retain, 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, force, retain, context);
    }

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

    private com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager manager() {
        return this.serviceManager;
    }

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