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

package com.azure.resourcemanager.vmwarecloudsimple.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.vmwarecloudsimple.fluent.VirtualMachinesClient;
import com.azure.resourcemanager.vmwarecloudsimple.fluent.models.VirtualMachineInner;
import com.azure.resourcemanager.vmwarecloudsimple.models.StopMode;
import com.azure.resourcemanager.vmwarecloudsimple.models.VirtualMachine;
import com.azure.resourcemanager.vmwarecloudsimple.models.VirtualMachineStopMode;
import com.azure.resourcemanager.vmwarecloudsimple.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.vmwarecloudsimple.VMwareCloudSimpleManager serviceManager;

    public VirtualMachinesImpl(
        VirtualMachinesClient innerClient,
        com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager 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(String filter, Integer top, String skipToken, Context context) {
        PagedIterable<VirtualMachineInner> inner = this.serviceClient().list(filter, top, skipToken, 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, String filter, Integer top, String skipToken, Context context) {
        PagedIterable<VirtualMachineInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, filter, top, skipToken, 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 delete(String resourceGroupName, String referer, String virtualMachineName) {
        this.serviceClient().delete(resourceGroupName, referer, virtualMachineName);
    }

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

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

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

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

    public void stop(
        String resourceGroupName,
        String referer,
        String virtualMachineName,
        StopMode mode,
        VirtualMachineStopMode m,
        Context context) {
        this.serviceClient().stop(resourceGroupName, referer, virtualMachineName, mode, m, 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)));
        }
        String localReferer = null;
        this.delete(resourceGroupName, localReferer, virtualMachineName, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, String referer, 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, referer, virtualMachineName, context);
    }

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

    private com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager manager() {
        return this.serviceManager;
    }

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