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

package com.azure.resourcemanager.devcenter.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.devcenter.fluent.NetworkConnectionsClient;
import com.azure.resourcemanager.devcenter.fluent.models.HealthCheckStatusDetailsInner;
import com.azure.resourcemanager.devcenter.fluent.models.NetworkConnectionInner;
import com.azure.resourcemanager.devcenter.fluent.models.OutboundEnvironmentEndpointInner;
import com.azure.resourcemanager.devcenter.models.HealthCheckStatusDetails;
import com.azure.resourcemanager.devcenter.models.NetworkConnection;
import com.azure.resourcemanager.devcenter.models.NetworkConnections;
import com.azure.resourcemanager.devcenter.models.OutboundEnvironmentEndpoint;

public final class NetworkConnectionsImpl implements NetworkConnections {
    private static final ClientLogger LOGGER = new ClientLogger(NetworkConnectionsImpl.class);

    private final NetworkConnectionsClient innerClient;

    private final com.azure.resourcemanager.devcenter.DevCenterManager serviceManager;

    public NetworkConnectionsImpl(
        NetworkConnectionsClient innerClient, com.azure.resourcemanager.devcenter.DevCenterManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<NetworkConnection> list() {
        PagedIterable<NetworkConnectionInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new NetworkConnectionImpl(inner1, this.manager()));
    }

    public PagedIterable<NetworkConnection> list(Integer top, Context context) {
        PagedIterable<NetworkConnectionInner> inner = this.serviceClient().list(top, context);
        return Utils.mapPage(inner, inner1 -> new NetworkConnectionImpl(inner1, this.manager()));
    }

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

    public PagedIterable<NetworkConnection> listByResourceGroup(
        String resourceGroupName, Integer top, Context context) {
        PagedIterable<NetworkConnectionInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, top, context);
        return Utils.mapPage(inner, inner1 -> new NetworkConnectionImpl(inner1, this.manager()));
    }

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

    public NetworkConnection getByResourceGroup(String resourceGroupName, String networkConnectionName) {
        NetworkConnectionInner inner =
            this.serviceClient().getByResourceGroup(resourceGroupName, networkConnectionName);
        if (inner != null) {
            return new NetworkConnectionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public PagedIterable<HealthCheckStatusDetails> listHealthDetails(
        String resourceGroupName, String networkConnectionName) {
        PagedIterable<HealthCheckStatusDetailsInner> inner =
            this.serviceClient().listHealthDetails(resourceGroupName, networkConnectionName);
        return Utils.mapPage(inner, inner1 -> new HealthCheckStatusDetailsImpl(inner1, this.manager()));
    }

    public PagedIterable<HealthCheckStatusDetails> listHealthDetails(
        String resourceGroupName, String networkConnectionName, Integer top, Context context) {
        PagedIterable<HealthCheckStatusDetailsInner> inner =
            this.serviceClient().listHealthDetails(resourceGroupName, networkConnectionName, top, context);
        return Utils.mapPage(inner, inner1 -> new HealthCheckStatusDetailsImpl(inner1, this.manager()));
    }

    public Response<HealthCheckStatusDetails> getHealthDetailsWithResponse(
        String resourceGroupName, String networkConnectionName, Context context) {
        Response<HealthCheckStatusDetailsInner> inner =
            this.serviceClient().getHealthDetailsWithResponse(resourceGroupName, networkConnectionName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new HealthCheckStatusDetailsImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public HealthCheckStatusDetails getHealthDetails(String resourceGroupName, String networkConnectionName) {
        HealthCheckStatusDetailsInner inner =
            this.serviceClient().getHealthDetails(resourceGroupName, networkConnectionName);
        if (inner != null) {
            return new HealthCheckStatusDetailsImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void runHealthChecks(String resourceGroupName, String networkConnectionName) {
        this.serviceClient().runHealthChecks(resourceGroupName, networkConnectionName);
    }

    public void runHealthChecks(String resourceGroupName, String networkConnectionName, Context context) {
        this.serviceClient().runHealthChecks(resourceGroupName, networkConnectionName, context);
    }

    public PagedIterable<OutboundEnvironmentEndpoint> listOutboundNetworkDependenciesEndpoints(
        String resourceGroupName, String networkConnectionName) {
        PagedIterable<OutboundEnvironmentEndpointInner> inner =
            this.serviceClient().listOutboundNetworkDependenciesEndpoints(resourceGroupName, networkConnectionName);
        return Utils.mapPage(inner, inner1 -> new OutboundEnvironmentEndpointImpl(inner1, this.manager()));
    }

    public PagedIterable<OutboundEnvironmentEndpoint> listOutboundNetworkDependenciesEndpoints(
        String resourceGroupName, String networkConnectionName, Integer top, Context context) {
        PagedIterable<OutboundEnvironmentEndpointInner> inner =
            this
                .serviceClient()
                .listOutboundNetworkDependenciesEndpoints(resourceGroupName, networkConnectionName, top, context);
        return Utils.mapPage(inner, inner1 -> new OutboundEnvironmentEndpointImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.devcenter.DevCenterManager manager() {
        return this.serviceManager;
    }

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