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

package azure.resourcemanager.resources.implementation;

import azure.resourcemanager.resources.fluent.ExtensionsResourcesClient;
import azure.resourcemanager.resources.fluent.models.ExtensionsResourceInner;
import azure.resourcemanager.resources.models.ExtensionsResource;
import azure.resourcemanager.resources.models.ExtensionsResources;
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;

public final class ExtensionsResourcesImpl implements ExtensionsResources {
    private static final ClientLogger LOGGER = new ClientLogger(ExtensionsResourcesImpl.class);

    private final ExtensionsResourcesClient innerClient;

    private final azure.resourcemanager.resources.ResourcesManager serviceManager;

    public ExtensionsResourcesImpl(ExtensionsResourcesClient innerClient,
        azure.resourcemanager.resources.ResourcesManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ExtensionsResource> getWithResponse(String resourceUri, String extensionsResourceName,
        Context context) {
        Response<ExtensionsResourceInner> inner
            = this.serviceClient().getWithResponse(resourceUri, extensionsResourceName, context);
        return new SimpleResponse<>(inner.getRequest(), inner.getStatusCode(), inner.getHeaders(),
            new ExtensionsResourceImpl(inner.getValue(), this.manager()));
    }

    public ExtensionsResource get(String resourceUri, String extensionsResourceName) {
        ExtensionsResourceInner inner = this.serviceClient().get(resourceUri, extensionsResourceName);
        if (inner != null) {
            return new ExtensionsResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void deleteByResourceGroup(String resourceUri, String extensionsResourceName) {
        this.serviceClient().delete(resourceUri, extensionsResourceName);
    }

    public PagedIterable<ExtensionsResource> listByScope(String resourceUri) {
        PagedIterable<ExtensionsResourceInner> inner = this.serviceClient().listByScope(resourceUri);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new ExtensionsResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<ExtensionsResource> listByScope(String resourceUri, Context context) {
        PagedIterable<ExtensionsResourceInner> inner = this.serviceClient().listByScope(resourceUri, context);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new ExtensionsResourceImpl(inner1, this.manager()));
    }

    public ExtensionsResource getById(String id) {
        String resourceUri = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/Azure.ResourceManager.Resources/extensionsResources/{extensionsResourceName}",
            "resourceUri");
        if (resourceUri == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String extensionsResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/Azure.ResourceManager.Resources/extensionsResources/{extensionsResourceName}",
            "extensionsResourceName");
        if (extensionsResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'extensionsResources'.", id)));
        }
        return this.getWithResponse(resourceUri, extensionsResourceName, Context.NONE).getValue();
    }

    public Response<ExtensionsResource> getByIdWithResponse(String id, Context context) {
        String resourceUri = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/Azure.ResourceManager.Resources/extensionsResources/{extensionsResourceName}",
            "resourceUri");
        if (resourceUri == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String extensionsResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/Azure.ResourceManager.Resources/extensionsResources/{extensionsResourceName}",
            "extensionsResourceName");
        if (extensionsResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'extensionsResources'.", id)));
        }
        return this.getWithResponse(resourceUri, extensionsResourceName, context);
    }

    public void deleteById(String id) {
        String resourceUri = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/Azure.ResourceManager.Resources/extensionsResources/{extensionsResourceName}",
            "resourceUri");
        if (resourceUri == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String extensionsResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/Azure.ResourceManager.Resources/extensionsResources/{extensionsResourceName}",
            "extensionsResourceName");
        if (extensionsResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'extensionsResources'.", id)));
        }
        this.deleteByResourceGroupWithResponse(resourceUri, extensionsResourceName, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(String id, Context context) {
        String resourceUri = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/Azure.ResourceManager.Resources/extensionsResources/{extensionsResourceName}",
            "resourceUri");
        if (resourceUri == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String extensionsResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/Azure.ResourceManager.Resources/extensionsResources/{extensionsResourceName}",
            "extensionsResourceName");
        if (extensionsResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'extensionsResources'.", id)));
        }
        return this.deleteByResourceGroupWithResponse(resourceUri, extensionsResourceName, context);
    }

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

    private azure.resourcemanager.resources.ResourcesManager manager() {
        return this.serviceManager;
    }

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