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

package com.azure.resourcemanager.signalr.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.signalr.fluent.SignalRCustomDomainsClient;
import com.azure.resourcemanager.signalr.fluent.models.CustomDomainInner;
import com.azure.resourcemanager.signalr.models.CustomDomain;
import com.azure.resourcemanager.signalr.models.SignalRCustomDomains;

public final class SignalRCustomDomainsImpl implements SignalRCustomDomains {
    private static final ClientLogger LOGGER = new ClientLogger(SignalRCustomDomainsImpl.class);

    private final SignalRCustomDomainsClient innerClient;

    private final com.azure.resourcemanager.signalr.SignalRManager serviceManager;

    public SignalRCustomDomainsImpl(
        SignalRCustomDomainsClient innerClient, com.azure.resourcemanager.signalr.SignalRManager 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, "signalR");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'signalR'.", 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, "signalR");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'signalR'.", 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, "signalR");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'signalR'.", 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, "signalR");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'signalR'.", 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 SignalRCustomDomainsClient serviceClient() {
        return this.innerClient;
    }

    private com.azure.resourcemanager.signalr.SignalRManager manager() {
        return this.serviceManager;
    }

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