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

package com.azure.resourcemanager.voiceservices.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.voiceservices.fluent.CommunicationsGatewaysClient;
import com.azure.resourcemanager.voiceservices.fluent.models.CommunicationsGatewayInner;
import com.azure.resourcemanager.voiceservices.models.CommunicationsGateway;
import com.azure.resourcemanager.voiceservices.models.CommunicationsGateways;

public final class CommunicationsGatewaysImpl implements CommunicationsGateways {
    private static final ClientLogger LOGGER = new ClientLogger(CommunicationsGatewaysImpl.class);

    private final CommunicationsGatewaysClient innerClient;

    private final com.azure.resourcemanager.voiceservices.VoiceServicesManager serviceManager;

    public CommunicationsGatewaysImpl(
        CommunicationsGatewaysClient innerClient,
        com.azure.resourcemanager.voiceservices.VoiceServicesManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<CommunicationsGateway> list() {
        PagedIterable<CommunicationsGatewayInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new CommunicationsGatewayImpl(inner1, this.manager()));
    }

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

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

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

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

    public CommunicationsGateway getByResourceGroup(String resourceGroupName, String communicationsGatewayName) {
        CommunicationsGatewayInner inner =
            this.serviceClient().getByResourceGroup(resourceGroupName, communicationsGatewayName);
        if (inner != null) {
            return new CommunicationsGatewayImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.voiceservices.VoiceServicesManager manager() {
        return this.serviceManager;
    }

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