// 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.NestedsClient;
import azure.resourcemanager.resources.fluent.models.NestedProxyResourceInner;
import azure.resourcemanager.resources.models.NestedProxyResource;
import azure.resourcemanager.resources.models.Nesteds;
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 NestedsImpl implements Nesteds {
    private static final ClientLogger LOGGER = new ClientLogger(NestedsImpl.class);

    private final NestedsClient innerClient;

    private final azure.resourcemanager.resources.ResourcesManager serviceManager;

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

    public Response<NestedProxyResource> getWithResponse(String resourceGroupName, String topLevelTrackedResourceName,
        String nextedProxyResourceName, Context context) {
        Response<NestedProxyResourceInner> inner = this.serviceClient()
            .getWithResponse(resourceGroupName, topLevelTrackedResourceName, nextedProxyResourceName, context);
        return new SimpleResponse<>(inner.getRequest(), inner.getStatusCode(), inner.getHeaders(),
            new NestedProxyResourceImpl(inner.getValue(), this.manager()));
    }

    public NestedProxyResource get(String resourceGroupName, String topLevelTrackedResourceName,
        String nextedProxyResourceName) {
        NestedProxyResourceInner inner
            = this.serviceClient().get(resourceGroupName, topLevelTrackedResourceName, nextedProxyResourceName);
        if (inner != null) {
            return new NestedProxyResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String topLevelTrackedResourceName, String nextedProxyResourceName) {
        this.serviceClient().delete(resourceGroupName, topLevelTrackedResourceName, nextedProxyResourceName);
    }

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

    public PagedIterable<NestedProxyResource> listByTopLevelTrackedResource(String resourceGroupName,
        String topLevelTrackedResourceName) {
        PagedIterable<NestedProxyResourceInner> inner
            = this.serviceClient().listByTopLevelTrackedResource(resourceGroupName, topLevelTrackedResourceName);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new NestedProxyResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<NestedProxyResource> listByTopLevelTrackedResource(String resourceGroupName,
        String topLevelTrackedResourceName, Context context) {
        PagedIterable<NestedProxyResourceInner> inner = this.serviceClient()
            .listByTopLevelTrackedResource(resourceGroupName, topLevelTrackedResourceName, context);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new NestedProxyResourceImpl(inner1, this.manager()));
    }

    public NestedProxyResource 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 topLevelTrackedResourceName = ResourceManagerUtils.getValueFromIdByName(id, "topLevelTrackedResources");
        if (topLevelTrackedResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(String
                .format("The resource ID '%s' is not valid. Missing path segment 'topLevelTrackedResources'.", id)));
        }
        String nextedProxyResourceName = ResourceManagerUtils.getValueFromIdByName(id, "nestedProxyResources");
        if (nextedProxyResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'nestedProxyResources'.", id)));
        }
        return this
            .getWithResponse(resourceGroupName, topLevelTrackedResourceName, nextedProxyResourceName, Context.NONE)
            .getValue();
    }

    public Response<NestedProxyResource> 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 topLevelTrackedResourceName = ResourceManagerUtils.getValueFromIdByName(id, "topLevelTrackedResources");
        if (topLevelTrackedResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(String
                .format("The resource ID '%s' is not valid. Missing path segment 'topLevelTrackedResources'.", id)));
        }
        String nextedProxyResourceName = ResourceManagerUtils.getValueFromIdByName(id, "nestedProxyResources");
        if (nextedProxyResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'nestedProxyResources'.", id)));
        }
        return this.getWithResponse(resourceGroupName, topLevelTrackedResourceName, nextedProxyResourceName, context);
    }

    public void deleteById(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 topLevelTrackedResourceName = ResourceManagerUtils.getValueFromIdByName(id, "topLevelTrackedResources");
        if (topLevelTrackedResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(String
                .format("The resource ID '%s' is not valid. Missing path segment 'topLevelTrackedResources'.", id)));
        }
        String nextedProxyResourceName = ResourceManagerUtils.getValueFromIdByName(id, "nestedProxyResources");
        if (nextedProxyResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'nestedProxyResources'.", id)));
        }
        this.delete(resourceGroupName, topLevelTrackedResourceName, nextedProxyResourceName, Context.NONE);
    }

    public void deleteByIdWithResponse(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 topLevelTrackedResourceName = ResourceManagerUtils.getValueFromIdByName(id, "topLevelTrackedResources");
        if (topLevelTrackedResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(String
                .format("The resource ID '%s' is not valid. Missing path segment 'topLevelTrackedResources'.", id)));
        }
        String nextedProxyResourceName = ResourceManagerUtils.getValueFromIdByName(id, "nestedProxyResources");
        if (nextedProxyResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'nestedProxyResources'.", id)));
        }
        this.delete(resourceGroupName, topLevelTrackedResourceName, nextedProxyResourceName, context);
    }

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

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

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