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

package tsptest.armversioned.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.armversioned.fluent.TopLevelArmResourcesClient;
import tsptest.armversioned.fluent.models.TopLevelArmResourceInner;
import tsptest.armversioned.models.TopLevelArmResource;
import tsptest.armversioned.models.TopLevelArmResources;

public final class TopLevelArmResourcesImpl implements TopLevelArmResources {
    private static final ClientLogger LOGGER = new ClientLogger(TopLevelArmResourcesImpl.class);

    private final TopLevelArmResourcesClient innerClient;

    private final tsptest.armversioned.ArmVersionedManager serviceManager;

    public TopLevelArmResourcesImpl(TopLevelArmResourcesClient innerClient,
        tsptest.armversioned.ArmVersionedManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<TopLevelArmResource> list() {
        PagedIterable<TopLevelArmResourceInner> inner = this.serviceClient().list();
        return ResourceManagerUtils.mapPage(inner, inner1 -> new TopLevelArmResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<TopLevelArmResource> list(String parameter, String newParameter, Context context) {
        PagedIterable<TopLevelArmResourceInner> inner = this.serviceClient().list(parameter, newParameter, context);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new TopLevelArmResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<TopLevelArmResource> list(String parameter, Context context) {
        PagedIterable<TopLevelArmResourceInner> inner = this.serviceClient().list(parameter, context);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new TopLevelArmResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<TopLevelArmResource> listByResourceGroup(String resourceGroupName) {
        PagedIterable<TopLevelArmResourceInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new TopLevelArmResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<TopLevelArmResource> listByResourceGroup(String resourceGroupName, String parameter,
        String newParameter, Context context) {
        PagedIterable<TopLevelArmResourceInner> inner
            = this.serviceClient().listByResourceGroup(resourceGroupName, parameter, newParameter, context);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new TopLevelArmResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<TopLevelArmResource> listByResourceGroup(String resourceGroupName, String parameter,
        Context context) {
        PagedIterable<TopLevelArmResourceInner> inner
            = this.serviceClient().listByResourceGroup(resourceGroupName, parameter, context);
        return ResourceManagerUtils.mapPage(inner, inner1 -> new TopLevelArmResourceImpl(inner1, this.manager()));
    }

    public Response<TopLevelArmResource> getByResourceGroupWithResponse(String resourceGroupName,
        String topLevelArmResourcePropertiesName, String parameter, String newParameter, Context context) {
        Response<TopLevelArmResourceInner> inner = this.serviceClient()
            .getByResourceGroupWithResponse(resourceGroupName, topLevelArmResourcePropertiesName, parameter,
                newParameter, context);
        if (inner != null) {
            return new SimpleResponse<>(inner.getRequest(), inner.getStatusCode(), inner.getHeaders(),
                new TopLevelArmResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Response<TopLevelArmResource> getByResourceGroupWithResponse(String resourceGroupName,
        String topLevelArmResourcePropertiesName, String parameter, Context context) {
        Response<TopLevelArmResourceInner> inner = this.serviceClient()
            .getByResourceGroupWithResponse(resourceGroupName, topLevelArmResourcePropertiesName, parameter, context);
        if (inner != null) {
            return new SimpleResponse<>(inner.getRequest(), inner.getStatusCode(), inner.getHeaders(),
                new TopLevelArmResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public TopLevelArmResource getByResourceGroup(String resourceGroupName, String topLevelArmResourcePropertiesName) {
        TopLevelArmResourceInner inner
            = this.serviceClient().getByResourceGroup(resourceGroupName, topLevelArmResourcePropertiesName);
        if (inner != null) {
            return new TopLevelArmResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public Response<Void> actionWithResponse(String resourceGroupName, String topLevelArmResourcePropertiesName,
        String parameter, String newParameter, Context context) {
        return this.serviceClient()
            .actionWithResponse(resourceGroupName, topLevelArmResourcePropertiesName, parameter, newParameter, context);
    }

    public Response<Void> actionWithResponse(String resourceGroupName, String topLevelArmResourcePropertiesName,
        String parameter, Context context) {
        return this.serviceClient()
            .actionWithResponse(resourceGroupName, topLevelArmResourcePropertiesName, parameter, context);
    }

    public void action(String resourceGroupName, String topLevelArmResourcePropertiesName) {
        this.serviceClient().action(resourceGroupName, topLevelArmResourcePropertiesName);
    }

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

    public Response<TopLevelArmResource> getByIdWithResponse(String id, String parameter, String newParameter,
        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 topLevelArmResourcePropertiesName
            = ResourceManagerUtils.getValueFromIdByName(id, "topLevelArmResources");
        if (topLevelArmResourcePropertiesName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'topLevelArmResources'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, topLevelArmResourcePropertiesName, parameter,
            newParameter, 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 topLevelArmResourcePropertiesName
            = ResourceManagerUtils.getValueFromIdByName(id, "topLevelArmResources");
        if (topLevelArmResourcePropertiesName == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'topLevelArmResources'.", id)));
        }
        String localParameter = null;
        String localNewParameter = null;
        this.deleteWithResponse(resourceGroupName, topLevelArmResourcePropertiesName, localParameter, localNewParameter,
            Context.NONE);
    }

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

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

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

    private tsptest.armversioned.ArmVersionedManager manager() {
        return this.serviceManager;
    }

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