// 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.WebPubSubsClient;
import com.azure.resourcemanager.webpubsub.fluent.models.NameAvailabilityInner;
import com.azure.resourcemanager.webpubsub.fluent.models.SkuListInner;
import com.azure.resourcemanager.webpubsub.fluent.models.WebPubSubKeysInner;
import com.azure.resourcemanager.webpubsub.fluent.models.WebPubSubResourceInner;
import com.azure.resourcemanager.webpubsub.models.NameAvailability;
import com.azure.resourcemanager.webpubsub.models.NameAvailabilityParameters;
import com.azure.resourcemanager.webpubsub.models.RegenerateKeyParameters;
import com.azure.resourcemanager.webpubsub.models.SkuList;
import com.azure.resourcemanager.webpubsub.models.WebPubSubKeys;
import com.azure.resourcemanager.webpubsub.models.WebPubSubResource;
import com.azure.resourcemanager.webpubsub.models.WebPubSubs;

public final class WebPubSubsImpl implements WebPubSubs {
    private static final ClientLogger LOGGER = new ClientLogger(WebPubSubsImpl.class);

    private final WebPubSubsClient innerClient;

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

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

    public Response<NameAvailability> checkNameAvailabilityWithResponse(
        String location, NameAvailabilityParameters parameters, Context context) {
        Response<NameAvailabilityInner> inner =
            this.serviceClient().checkNameAvailabilityWithResponse(location, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new NameAvailabilityImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public NameAvailability checkNameAvailability(String location, NameAvailabilityParameters parameters) {
        NameAvailabilityInner inner = this.serviceClient().checkNameAvailability(location, parameters);
        if (inner != null) {
            return new NameAvailabilityImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<WebPubSubResource> list() {
        PagedIterable<WebPubSubResourceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new WebPubSubResourceImpl(inner1, this.manager()));
    }

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

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

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

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

    public WebPubSubResource getByResourceGroup(String resourceGroupName, String resourceName) {
        WebPubSubResourceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, resourceName);
        if (inner != null) {
            return new WebPubSubResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public WebPubSubKeys listKeys(String resourceGroupName, String resourceName) {
        WebPubSubKeysInner inner = this.serviceClient().listKeys(resourceGroupName, resourceName);
        if (inner != null) {
            return new WebPubSubKeysImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public WebPubSubKeys regenerateKey(
        String resourceGroupName, String resourceName, RegenerateKeyParameters parameters) {
        WebPubSubKeysInner inner = this.serviceClient().regenerateKey(resourceGroupName, resourceName, parameters);
        if (inner != null) {
            return new WebPubSubKeysImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public WebPubSubKeys regenerateKey(
        String resourceGroupName, String resourceName, RegenerateKeyParameters parameters, Context context) {
        WebPubSubKeysInner inner =
            this.serviceClient().regenerateKey(resourceGroupName, resourceName, parameters, context);
        if (inner != null) {
            return new WebPubSubKeysImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public SkuList listSkus(String resourceGroupName, String resourceName) {
        SkuListInner inner = this.serviceClient().listSkus(resourceGroupName, resourceName);
        if (inner != null) {
            return new SkuListImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public WebPubSubResource 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, "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.getByResourceGroupWithResponse(resourceGroupName, resourceName, Context.NONE).getValue();
    }

    public Response<WebPubSubResource> 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, "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.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, "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(resourceGroupName, resourceName, Context.NONE);
    }

    public 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, "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(resourceGroupName, resourceName, context);
    }

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

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

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