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

package tsptest.armresourceprovider.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 tsptest.armresourceprovider.fluent.ChildExtensionResourceInterfacesClient;
import tsptest.armresourceprovider.fluent.models.ChildExtensionResourceInner;
import tsptest.armresourceprovider.models.ChildExtensionResource;
import tsptest.armresourceprovider.models.ChildExtensionResourceInterfaces;

public final class ChildExtensionResourceInterfacesImpl implements ChildExtensionResourceInterfaces {
    private static final ClientLogger LOGGER = new ClientLogger(ChildExtensionResourceInterfacesImpl.class);

    private final ChildExtensionResourceInterfacesClient innerClient;

    private final tsptest.armresourceprovider.ArmResourceProviderManager serviceManager;

    public ChildExtensionResourceInterfacesImpl(ChildExtensionResourceInterfacesClient innerClient,
        tsptest.armresourceprovider.ArmResourceProviderManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ChildExtensionResource> getWithResponse(String resourceUri, String topLevelArmResourceName,
        String childExtensionResourceName, Context context) {
        Response<ChildExtensionResourceInner> inner = this.serviceClient()
            .getWithResponse(resourceUri, topLevelArmResourceName, childExtensionResourceName, context);
        return new SimpleResponse<>(inner.getRequest(), inner.getStatusCode(), inner.getHeaders(),
            new ChildExtensionResourceImpl(inner.getValue(), this.manager()));
    }

    public ChildExtensionResource get(String resourceUri, String topLevelArmResourceName,
        String childExtensionResourceName) {
        ChildExtensionResourceInner inner
            = this.serviceClient().get(resourceUri, topLevelArmResourceName, childExtensionResourceName);
        if (inner != null) {
            return new ChildExtensionResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceUri, String topLevelArmResourceName, String childExtensionResourceName) {
        this.serviceClient().delete(resourceUri, topLevelArmResourceName, childExtensionResourceName);
    }

    public void delete(String resourceUri, String topLevelArmResourceName, String childExtensionResourceName,
        Context context) {
        this.serviceClient().delete(resourceUri, topLevelArmResourceName, childExtensionResourceName, context);
    }

    public PagedIterable<ChildExtensionResource> listByTopLevelArmResource(String resourceUri,
        String topLevelArmResourceName) {
        PagedIterable<ChildExtensionResourceInner> inner
            = this.serviceClient().listByTopLevelArmResource(resourceUri, topLevelArmResourceName);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new ChildExtensionResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<ChildExtensionResource> listByTopLevelArmResource(String resourceUri,
        String topLevelArmResourceName, Context context) {
        PagedIterable<ChildExtensionResourceInner> inner
            = this.serviceClient().listByTopLevelArmResource(resourceUri, topLevelArmResourceName, context);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new ChildExtensionResourceImpl(inner1, this.manager()));
    }

    public ChildExtensionResource getById(String id) {
        String resourceUri = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "resourceUri");
        if (resourceUri == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String topLevelArmResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "topLevelArmResourceName");
        if (topLevelArmResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'topLevelArmResources'.", id)));
        }
        String childExtensionResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "childExtensionResourceName");
        if (childExtensionResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(String
                .format("The resource ID '%s' is not valid. Missing path segment 'childExtensionResources'.", id)));
        }
        return this.getWithResponse(resourceUri, topLevelArmResourceName, childExtensionResourceName, Context.NONE)
            .getValue();
    }

    public Response<ChildExtensionResource> getByIdWithResponse(String id, Context context) {
        String resourceUri = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "resourceUri");
        if (resourceUri == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String topLevelArmResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "topLevelArmResourceName");
        if (topLevelArmResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'topLevelArmResources'.", id)));
        }
        String childExtensionResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "childExtensionResourceName");
        if (childExtensionResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(String
                .format("The resource ID '%s' is not valid. Missing path segment 'childExtensionResources'.", id)));
        }
        return this.getWithResponse(resourceUri, topLevelArmResourceName, childExtensionResourceName, context);
    }

    public void deleteById(String id) {
        String resourceUri = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "resourceUri");
        if (resourceUri == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String topLevelArmResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "topLevelArmResourceName");
        if (topLevelArmResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'topLevelArmResources'.", id)));
        }
        String childExtensionResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "childExtensionResourceName");
        if (childExtensionResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(String
                .format("The resource ID '%s' is not valid. Missing path segment 'childExtensionResources'.", id)));
        }
        this.delete(resourceUri, topLevelArmResourceName, childExtensionResourceName, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, Context context) {
        String resourceUri = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "resourceUri");
        if (resourceUri == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String topLevelArmResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "topLevelArmResourceName");
        if (topLevelArmResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'topLevelArmResources'.", id)));
        }
        String childExtensionResourceName = ResourceManagerUtils.getValueFromIdByParameterName(id,
            "/{resourceUri}/providers/TspTest.ArmResourceProvider/topLevelArmResources/{topLevelArmResourceName}/childExtensionResources/{childExtensionResourceName}",
            "childExtensionResourceName");
        if (childExtensionResourceName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(String
                .format("The resource ID '%s' is not valid. Missing path segment 'childExtensionResources'.", id)));
        }
        this.delete(resourceUri, topLevelArmResourceName, childExtensionResourceName, context);
    }

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

    private tsptest.armresourceprovider.ArmResourceProviderManager manager() {
        return this.serviceManager;
    }

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