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

package com.azure.resourcemanager.webpubsub.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.webpubsub.fluent.WebPubSubHubsClient;
import com.azure.resourcemanager.webpubsub.fluent.models.WebPubSubHubInner;
import com.azure.resourcemanager.webpubsub.models.WebPubSubHub;
import com.azure.resourcemanager.webpubsub.models.WebPubSubHubs;

public final class WebPubSubHubsImpl implements WebPubSubHubs {
    private static final ClientLogger LOGGER = new ClientLogger(WebPubSubHubsImpl.class);

    private final WebPubSubHubsClient innerClient;

    private final com.azure.resourcemanager.webpubsub.WebPubSubManager serviceManager;

    public WebPubSubHubsImpl(
        WebPubSubHubsClient innerClient, com.azure.resourcemanager.webpubsub.WebPubSubManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<WebPubSubHub> list(String resourceGroupName, String resourceName) {
        PagedIterable<WebPubSubHubInner> inner = this.serviceClient().list(resourceGroupName, resourceName);
        return Utils.mapPage(inner, inner1 -> new WebPubSubHubImpl(inner1, this.manager()));
    }

    public PagedIterable<WebPubSubHub> list(String resourceGroupName, String resourceName, Context context) {
        PagedIterable<WebPubSubHubInner> inner = this.serviceClient().list(resourceGroupName, resourceName, context);
        return Utils.mapPage(inner, inner1 -> new WebPubSubHubImpl(inner1, this.manager()));
    }

    public Response<WebPubSubHub> getWithResponse(
        String hubName, String resourceGroupName, String resourceName, Context context) {
        Response<WebPubSubHubInner> inner =
            this.serviceClient().getWithResponse(hubName, resourceGroupName, resourceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new WebPubSubHubImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public WebPubSubHub get(String hubName, String resourceGroupName, String resourceName) {
        WebPubSubHubInner inner = this.serviceClient().get(hubName, resourceGroupName, resourceName);
        if (inner != null) {
            return new WebPubSubHubImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void delete(String hubName, String resourceGroupName, String resourceName, Context context) {
        this.serviceClient().delete(hubName, resourceGroupName, resourceName, context);
    }

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

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

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

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

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

    private com.azure.resourcemanager.webpubsub.WebPubSubManager manager() {
        return this.serviceManager;
    }

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