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

package azure.resourcemanager.multiserviceolderversions.combined.implementation;

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

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

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

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

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