// 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.DedicatedCloudNodesClient;
import com.azure.resourcemanager.vmwarecloudsimple.fluent.models.DedicatedCloudNodeInner;
import com.azure.resourcemanager.vmwarecloudsimple.models.DedicatedCloudNode;
import com.azure.resourcemanager.vmwarecloudsimple.models.DedicatedCloudNodes;

public final class DedicatedCloudNodesImpl implements DedicatedCloudNodes {
    private static final ClientLogger LOGGER = new ClientLogger(DedicatedCloudNodesImpl.class);

    private final DedicatedCloudNodesClient innerClient;

    private final com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager serviceManager;

    public DedicatedCloudNodesImpl(
        DedicatedCloudNodesClient innerClient,
        com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<DedicatedCloudNode> list() {
        PagedIterable<DedicatedCloudNodeInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new DedicatedCloudNodeImpl(inner1, this.manager()));
    }

    public PagedIterable<DedicatedCloudNode> list(String filter, Integer top, String skipToken, Context context) {
        PagedIterable<DedicatedCloudNodeInner> inner = this.serviceClient().list(filter, top, skipToken, context);
        return Utils.mapPage(inner, inner1 -> new DedicatedCloudNodeImpl(inner1, this.manager()));
    }

    public PagedIterable<DedicatedCloudNode> listByResourceGroup(String resourceGroupName) {
        PagedIterable<DedicatedCloudNodeInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new DedicatedCloudNodeImpl(inner1, this.manager()));
    }

    public PagedIterable<DedicatedCloudNode> listByResourceGroup(
        String resourceGroupName, String filter, Integer top, String skipToken, Context context) {
        PagedIterable<DedicatedCloudNodeInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, filter, top, skipToken, context);
        return Utils.mapPage(inner, inner1 -> new DedicatedCloudNodeImpl(inner1, this.manager()));
    }

    public Response<DedicatedCloudNode> getByResourceGroupWithResponse(
        String resourceGroupName, String dedicatedCloudNodeName, Context context) {
        Response<DedicatedCloudNodeInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, dedicatedCloudNodeName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new DedicatedCloudNodeImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public DedicatedCloudNode getByResourceGroup(String resourceGroupName, String dedicatedCloudNodeName) {
        DedicatedCloudNodeInner inner =
            this.serviceClient().getByResourceGroup(resourceGroupName, dedicatedCloudNodeName);
        if (inner != null) {
            return new DedicatedCloudNodeImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> deleteByResourceGroupWithResponse(
        String resourceGroupName, String dedicatedCloudNodeName, Context context) {
        return this.serviceClient().deleteWithResponse(resourceGroupName, dedicatedCloudNodeName, context);
    }

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

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

    public Response<DedicatedCloudNode> 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 dedicatedCloudNodeName = Utils.getValueFromIdByName(id, "dedicatedCloudNodes");
        if (dedicatedCloudNodeName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'dedicatedCloudNodes'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, dedicatedCloudNodeName, 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 dedicatedCloudNodeName = Utils.getValueFromIdByName(id, "dedicatedCloudNodes");
        if (dedicatedCloudNodeName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'dedicatedCloudNodes'.", id)));
        }
        this.deleteByResourceGroupWithResponse(resourceGroupName, dedicatedCloudNodeName, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(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 dedicatedCloudNodeName = Utils.getValueFromIdByName(id, "dedicatedCloudNodes");
        if (dedicatedCloudNodeName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'dedicatedCloudNodes'.", id)));
        }
        return this.deleteByResourceGroupWithResponse(resourceGroupName, dedicatedCloudNodeName, context);
    }

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

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

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