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

package azure.resourcemanager.multiservice.combined.implementation;

import azure.resourcemanager.multiservice.combined.fluent.VirtualMachinesClient;
import azure.resourcemanager.multiservice.combined.fluent.models.VirtualMachineInner;
import azure.resourcemanager.multiservice.combined.models.VirtualMachine;
import azure.resourcemanager.multiservice.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.multiservice.combined.CombinedManager serviceManager;

    public VirtualMachinesImpl(VirtualMachinesClient innerClient,
        azure.resourcemanager.multiservice.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, "virtualMachines");
        if (vmName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'virtualMachines'.", 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, "virtualMachines");
        if (vmName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'virtualMachines'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, vmName, context);
    }

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

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

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