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

package azure.resourcemanager.multiservicesharedmodels.combined.implementation;

import azure.resourcemanager.multiservicesharedmodels.combined.fluent.VirtualMachinesClient;
import azure.resourcemanager.multiservicesharedmodels.combined.fluent.models.VirtualMachineInner;
import azure.resourcemanager.multiservicesharedmodels.combined.models.VirtualMachine;
import azure.resourcemanager.multiservicesharedmodels.combined.models.VirtualMachines;
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;

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

    private final VirtualMachinesClient innerClient;

    private final azure.resourcemanager.multiservicesharedmodels.combined.CombinedManager serviceManager;

    public VirtualMachinesImpl(VirtualMachinesClient innerClient,
        azure.resourcemanager.multiservicesharedmodels.combined.CombinedManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

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

    public VirtualMachine getById(String id) {
        String resourceGroupName = ResourceManagerUtils.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 vmName = ResourceManagerUtils.getValueFromIdByName(id, "virtualMachinesShared");
        if (vmName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'virtualMachinesShared'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, vmName, Context.NONE).getValue();
    }

    public Response<VirtualMachine> getByIdWithResponse(String id, Context context) {
        String resourceGroupName = ResourceManagerUtils.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 vmName = ResourceManagerUtils.getValueFromIdByName(id, "virtualMachinesShared");
        if (vmName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'virtualMachinesShared'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, vmName, context);
    }

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

    private azure.resourcemanager.multiservicesharedmodels.combined.CombinedManager manager() {
        return this.serviceManager;
    }

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