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

package azure.resourcemanager.methodsubscriptionid.implementation;

import azure.resourcemanager.methodsubscriptionid.fluent.MixedSubscriptionPlacementResourceGroupResourceOperationsClient;
import azure.resourcemanager.methodsubscriptionid.fluent.models.ResourceGroupResourceInner;
import azure.resourcemanager.methodsubscriptionid.models.MixedSubscriptionPlacementResourceGroupResourceOperations;
import azure.resourcemanager.methodsubscriptionid.models.ResourceGroupResource;
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 MixedSubscriptionPlacementResourceGroupResourceOperationsImpl
    implements MixedSubscriptionPlacementResourceGroupResourceOperations {
    private static final ClientLogger LOGGER
        = new ClientLogger(MixedSubscriptionPlacementResourceGroupResourceOperationsImpl.class);

    private final MixedSubscriptionPlacementResourceGroupResourceOperationsClient innerClient;

    private final azure.resourcemanager.methodsubscriptionid.MethodSubscriptionIdManager serviceManager;

    public MixedSubscriptionPlacementResourceGroupResourceOperationsImpl(
        MixedSubscriptionPlacementResourceGroupResourceOperationsClient innerClient,
        azure.resourcemanager.methodsubscriptionid.MethodSubscriptionIdManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ResourceGroupResource> getByResourceGroupWithResponse(String resourceGroupName,
        String resourceGroupResourceName, Context context) {
        Response<ResourceGroupResourceInner> inner = this.serviceClient()
            .getByResourceGroupWithResponse(resourceGroupName, resourceGroupResourceName, context);
        return new SimpleResponse<>(inner.getRequest(), inner.getStatusCode(), inner.getHeaders(),
            new ResourceGroupResourceImpl(inner.getValue(), this.manager()));
    }

    public ResourceGroupResource getByResourceGroup(String resourceGroupName, String resourceGroupResourceName) {
        ResourceGroupResourceInner inner
            = this.serviceClient().getByResourceGroup(resourceGroupName, resourceGroupResourceName);
        if (inner != null) {
            return new ResourceGroupResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

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

    private azure.resourcemanager.methodsubscriptionid.MethodSubscriptionIdManager manager() {
        return this.serviceManager;
    }

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