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

package com.azure.resourcemanager.managednetworkfabric.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.managednetworkfabric.fluent.NetworkToNetworkInterconnectsClient;
import com.azure.resourcemanager.managednetworkfabric.fluent.models.NetworkToNetworkInterconnectInner;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkToNetworkInterconnect;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkToNetworkInterconnects;

public final class NetworkToNetworkInterconnectsImpl implements NetworkToNetworkInterconnects {
    private static final ClientLogger LOGGER = new ClientLogger(NetworkToNetworkInterconnectsImpl.class);

    private final NetworkToNetworkInterconnectsClient innerClient;

    private final com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager;

    public NetworkToNetworkInterconnectsImpl(
        NetworkToNetworkInterconnectsClient innerClient,
        com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<NetworkToNetworkInterconnect> getWithResponse(
        String resourceGroupName, String networkFabricName, String networkToNetworkInterconnectName, Context context) {
        Response<NetworkToNetworkInterconnectInner> inner =
            this
                .serviceClient()
                .getWithResponse(resourceGroupName, networkFabricName, networkToNetworkInterconnectName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new NetworkToNetworkInterconnectImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public NetworkToNetworkInterconnect get(
        String resourceGroupName, String networkFabricName, String networkToNetworkInterconnectName) {
        NetworkToNetworkInterconnectInner inner =
            this.serviceClient().get(resourceGroupName, networkFabricName, networkToNetworkInterconnectName);
        if (inner != null) {
            return new NetworkToNetworkInterconnectImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String networkFabricName, String networkToNetworkInterconnectName) {
        this.serviceClient().delete(resourceGroupName, networkFabricName, networkToNetworkInterconnectName);
    }

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

    public PagedIterable<NetworkToNetworkInterconnect> list(String resourceGroupName, String networkFabricName) {
        PagedIterable<NetworkToNetworkInterconnectInner> inner =
            this.serviceClient().list(resourceGroupName, networkFabricName);
        return Utils.mapPage(inner, inner1 -> new NetworkToNetworkInterconnectImpl(inner1, this.manager()));
    }

    public PagedIterable<NetworkToNetworkInterconnect> list(
        String resourceGroupName, String networkFabricName, Context context) {
        PagedIterable<NetworkToNetworkInterconnectInner> inner =
            this.serviceClient().list(resourceGroupName, networkFabricName, context);
        return Utils.mapPage(inner, inner1 -> new NetworkToNetworkInterconnectImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager manager() {
        return this.serviceManager;
    }

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