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

package azure.resourcemanager.commonproperties.implementation;

import azure.resourcemanager.commonproperties.fluent.ErrorsClient;
import azure.resourcemanager.commonproperties.fluent.models.ConfidentialResourceInner;
import azure.resourcemanager.commonproperties.models.ConfidentialResource;
import azure.resourcemanager.commonproperties.models.Errors;
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 ErrorsImpl implements Errors {
    private static final ClientLogger LOGGER = new ClientLogger(ErrorsImpl.class);

    private final ErrorsClient innerClient;

    private final azure.resourcemanager.commonproperties.CommonPropertiesManager serviceManager;

    public ErrorsImpl(ErrorsClient innerClient,
        azure.resourcemanager.commonproperties.CommonPropertiesManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ConfidentialResource> getByResourceGroupWithResponse(String resourceGroupName,
        String confidentialResourceName, Context context) {
        Response<ConfidentialResourceInner> inner
            = this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, confidentialResourceName, context);
        if (inner != null) {
            return new SimpleResponse<>(inner.getRequest(), inner.getStatusCode(), inner.getHeaders(),
                new ConfidentialResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ConfidentialResource getByResourceGroup(String resourceGroupName, String confidentialResourceName) {
        ConfidentialResourceInner inner
            = this.serviceClient().getByResourceGroup(resourceGroupName, confidentialResourceName);
        if (inner != null) {
            return new ConfidentialResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private azure.resourcemanager.commonproperties.CommonPropertiesManager manager() {
        return this.serviceManager;
    }

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