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

package com.azure.resourcemanager.communication.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.communication.fluent.CommunicationServicesClient;
import com.azure.resourcemanager.communication.fluent.models.CheckNameAvailabilityResponseInner;
import com.azure.resourcemanager.communication.fluent.models.CommunicationServiceKeysInner;
import com.azure.resourcemanager.communication.fluent.models.CommunicationServiceResourceInner;
import com.azure.resourcemanager.communication.fluent.models.LinkedNotificationHubInner;
import com.azure.resourcemanager.communication.models.CheckNameAvailabilityResponse;
import com.azure.resourcemanager.communication.models.CommunicationServiceKeys;
import com.azure.resourcemanager.communication.models.CommunicationServiceResource;
import com.azure.resourcemanager.communication.models.CommunicationServices;
import com.azure.resourcemanager.communication.models.LinkNotificationHubParameters;
import com.azure.resourcemanager.communication.models.LinkedNotificationHub;
import com.azure.resourcemanager.communication.models.NameAvailabilityParameters;
import com.azure.resourcemanager.communication.models.RegenerateKeyParameters;

public final class CommunicationServicesImpl implements CommunicationServices {
    private static final ClientLogger LOGGER = new ClientLogger(CommunicationServicesImpl.class);

    private final CommunicationServicesClient innerClient;

    private final com.azure.resourcemanager.communication.CommunicationManager serviceManager;

    public CommunicationServicesImpl(
        CommunicationServicesClient innerClient,
        com.azure.resourcemanager.communication.CommunicationManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<CheckNameAvailabilityResponse> checkNameAvailabilityWithResponse(
        NameAvailabilityParameters nameAvailabilityParameters, Context context) {
        Response<CheckNameAvailabilityResponseInner> inner =
            this.serviceClient().checkNameAvailabilityWithResponse(nameAvailabilityParameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CheckNameAvailabilityResponseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CheckNameAvailabilityResponse checkNameAvailability(NameAvailabilityParameters nameAvailabilityParameters) {
        CheckNameAvailabilityResponseInner inner =
            this.serviceClient().checkNameAvailability(nameAvailabilityParameters);
        if (inner != null) {
            return new CheckNameAvailabilityResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<LinkedNotificationHub> linkNotificationHubWithResponse(
        String resourceGroupName,
        String communicationServiceName,
        LinkNotificationHubParameters linkNotificationHubParameters,
        Context context) {
        Response<LinkedNotificationHubInner> inner =
            this
                .serviceClient()
                .linkNotificationHubWithResponse(
                    resourceGroupName, communicationServiceName, linkNotificationHubParameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new LinkedNotificationHubImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public LinkedNotificationHub linkNotificationHub(String resourceGroupName, String communicationServiceName) {
        LinkedNotificationHubInner inner =
            this.serviceClient().linkNotificationHub(resourceGroupName, communicationServiceName);
        if (inner != null) {
            return new LinkedNotificationHubImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<CommunicationServiceResource> list() {
        PagedIterable<CommunicationServiceResourceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new CommunicationServiceResourceImpl(inner1, this.manager()));
    }

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

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

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

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

    public CommunicationServiceResource getByResourceGroup(String resourceGroupName, String communicationServiceName) {
        CommunicationServiceResourceInner inner =
            this.serviceClient().getByResourceGroup(resourceGroupName, communicationServiceName);
        if (inner != null) {
            return new CommunicationServiceResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public CommunicationServiceKeys listKeys(String resourceGroupName, String communicationServiceName) {
        CommunicationServiceKeysInner inner =
            this.serviceClient().listKeys(resourceGroupName, communicationServiceName);
        if (inner != null) {
            return new CommunicationServiceKeysImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<CommunicationServiceKeys> regenerateKeyWithResponse(
        String resourceGroupName,
        String communicationServiceName,
        RegenerateKeyParameters parameters,
        Context context) {
        Response<CommunicationServiceKeysInner> inner =
            this
                .serviceClient()
                .regenerateKeyWithResponse(resourceGroupName, communicationServiceName, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CommunicationServiceKeysImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CommunicationServiceKeys regenerateKey(
        String resourceGroupName, String communicationServiceName, RegenerateKeyParameters parameters) {
        CommunicationServiceKeysInner inner =
            this.serviceClient().regenerateKey(resourceGroupName, communicationServiceName, parameters);
        if (inner != null) {
            return new CommunicationServiceKeysImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.communication.CommunicationManager manager() {
        return this.serviceManager;
    }

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