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

package com.azure.resourcemanager.azureadexternalidentities.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 com.azure.resourcemanager.azureadexternalidentities.fluent.GuestUsagesClient;
import com.azure.resourcemanager.azureadexternalidentities.fluent.models.GuestUsagesResourceInner;
import com.azure.resourcemanager.azureadexternalidentities.models.GuestUsages;
import com.azure.resourcemanager.azureadexternalidentities.models.GuestUsagesResource;

public final class GuestUsagesImpl implements GuestUsages {
    private static final ClientLogger LOGGER = new ClientLogger(GuestUsagesImpl.class);

    private final GuestUsagesClient innerClient;

    private final com.azure.resourcemanager.azureadexternalidentities.ExternalIdentitiesConfigurationManager
        serviceManager;

    public GuestUsagesImpl(
        GuestUsagesClient innerClient,
        com.azure.resourcemanager.azureadexternalidentities.ExternalIdentitiesConfigurationManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

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

    public GuestUsagesResource getByResourceGroup(String resourceGroupName, String resourceName) {
        GuestUsagesResourceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, resourceName);
        if (inner != null) {
            return new GuestUsagesResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public PagedIterable<GuestUsagesResource> list() {
        PagedIterable<GuestUsagesResourceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new GuestUsagesResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<GuestUsagesResource> list(Context context) {
        PagedIterable<GuestUsagesResourceInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new GuestUsagesResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<GuestUsagesResource> listByResourceGroup(String resourceGroupName) {
        PagedIterable<GuestUsagesResourceInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new GuestUsagesResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<GuestUsagesResource> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<GuestUsagesResourceInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new GuestUsagesResourceImpl(inner1, this.manager()));
    }

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

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

    public void deleteById(String id) {
        String resourceGroupName = Utils.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 resourceName = Utils.getValueFromIdByName(id, "guestUsages");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'guestUsages'.", id)));
        }
        this.deleteWithResponse(resourceGroupName, resourceName, Context.NONE);
    }

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

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

    private com.azure.resourcemanager.azureadexternalidentities.ExternalIdentitiesConfigurationManager manager() {
        return this.serviceManager;
    }

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