// 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.WebPubSubCustomDomainsClient;
import com.azure.resourcemanager.webpubsub.fluent.models.CustomDomainInner;
import com.azure.resourcemanager.webpubsub.models.CustomDomain;
import com.azure.resourcemanager.webpubsub.models.WebPubSubCustomDomains;

public final class WebPubSubCustomDomainsImpl implements WebPubSubCustomDomains {
    private static final ClientLogger LOGGER = new ClientLogger(WebPubSubCustomDomainsImpl.class);

    private final WebPubSubCustomDomainsClient innerClient;

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

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

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

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

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

    public CustomDomain get(String resourceGroupName, String resourceName, String name) {
        CustomDomainInner inner = this.serviceClient().get(resourceGroupName, resourceName, name);
        if (inner != null) {
            return new CustomDomainImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

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

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