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

package azure.resourcemanager.nonresource.implementation;

import azure.resourcemanager.nonresource.fluent.NonResourceOperationsClient;
import azure.resourcemanager.nonresource.fluent.models.NonResourceInner;
import azure.resourcemanager.nonresource.models.NonResource;
import azure.resourcemanager.nonresource.models.NonResourceOperations;
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 NonResourceOperationsImpl implements NonResourceOperations {
    private static final ClientLogger LOGGER = new ClientLogger(NonResourceOperationsImpl.class);

    private final NonResourceOperationsClient innerClient;

    private final azure.resourcemanager.nonresource.NonResourceManager serviceManager;

    public NonResourceOperationsImpl(NonResourceOperationsClient innerClient,
        azure.resourcemanager.nonresource.NonResourceManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<NonResource> getWithResponse(String location, String parameter, Context context) {
        Response<NonResourceInner> inner = this.serviceClient().getWithResponse(location, parameter, context);
        return new SimpleResponse<>(inner.getRequest(), inner.getStatusCode(), inner.getHeaders(),
            new NonResourceImpl(inner.getValue(), this.manager()));
    }

    public NonResource get(String location, String parameter) {
        NonResourceInner inner = this.serviceClient().get(location, parameter);
        if (inner != null) {
            return new NonResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public NonResource getById(String id) {
        String location = ResourceManagerUtils.getValueFromIdByName(id, "locations");
        if (location == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'locations'.", id)));
        }
        String parameter = ResourceManagerUtils.getValueFromIdByName(id, "otherParameters");
        if (parameter == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'otherParameters'.", id)));
        }
        return this.getWithResponse(location, parameter, Context.NONE).getValue();
    }

    public Response<NonResource> getByIdWithResponse(String id, Context context) {
        String location = ResourceManagerUtils.getValueFromIdByName(id, "locations");
        if (location == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'locations'.", id)));
        }
        String parameter = ResourceManagerUtils.getValueFromIdByName(id, "otherParameters");
        if (parameter == null) {
            throw LOGGER.logExceptionAsError(new IllegalArgumentException(
                String.format("The resource ID '%s' is not valid. Missing path segment 'otherParameters'.", id)));
        }
        return this.getWithResponse(location, parameter, context);
    }

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

    private azure.resourcemanager.nonresource.NonResourceManager manager() {
        return this.serviceManager;
    }

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