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

package com.azure.resourcemanager.recoveryservicessiterecovery.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.recoveryservicessiterecovery.fluent.ReplicationNetworkMappingsClient;
import com.azure.resourcemanager.recoveryservicessiterecovery.fluent.models.NetworkMappingInner;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.NetworkMapping;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.ReplicationNetworkMappings;

public final class ReplicationNetworkMappingsImpl implements ReplicationNetworkMappings {
    private static final ClientLogger LOGGER = new ClientLogger(ReplicationNetworkMappingsImpl.class);

    private final ReplicationNetworkMappingsClient innerClient;

    private final com.azure.resourcemanager.recoveryservicessiterecovery.SiteRecoveryManager serviceManager;

    public ReplicationNetworkMappingsImpl(
        ReplicationNetworkMappingsClient innerClient,
        com.azure.resourcemanager.recoveryservicessiterecovery.SiteRecoveryManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<NetworkMapping> listByReplicationNetworks(
        String resourceName, String resourceGroupName, String fabricName, String networkName) {
        PagedIterable<NetworkMappingInner> inner =
            this.serviceClient().listByReplicationNetworks(resourceName, resourceGroupName, fabricName, networkName);
        return Utils.mapPage(inner, inner1 -> new NetworkMappingImpl(inner1, this.manager()));
    }

    public PagedIterable<NetworkMapping> listByReplicationNetworks(
        String resourceName, String resourceGroupName, String fabricName, String networkName, Context context) {
        PagedIterable<NetworkMappingInner> inner =
            this
                .serviceClient()
                .listByReplicationNetworks(resourceName, resourceGroupName, fabricName, networkName, context);
        return Utils.mapPage(inner, inner1 -> new NetworkMappingImpl(inner1, this.manager()));
    }

    public Response<NetworkMapping> getWithResponse(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String networkName,
        String networkMappingName,
        Context context) {
        Response<NetworkMappingInner> inner =
            this
                .serviceClient()
                .getWithResponse(resourceName, resourceGroupName, fabricName, networkName, networkMappingName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new NetworkMappingImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public NetworkMapping get(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String networkName,
        String networkMappingName) {
        NetworkMappingInner inner =
            this.serviceClient().get(resourceName, resourceGroupName, fabricName, networkName, networkMappingName);
        if (inner != null) {
            return new NetworkMappingImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String networkName,
        String networkMappingName) {
        this.serviceClient().delete(resourceName, resourceGroupName, fabricName, networkName, networkMappingName);
    }

    public void delete(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String networkName,
        String networkMappingName,
        Context context) {
        this
            .serviceClient()
            .delete(resourceName, resourceGroupName, fabricName, networkName, networkMappingName, context);
    }

    public PagedIterable<NetworkMapping> list(String resourceName, String resourceGroupName) {
        PagedIterable<NetworkMappingInner> inner = this.serviceClient().list(resourceName, resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new NetworkMappingImpl(inner1, this.manager()));
    }

    public PagedIterable<NetworkMapping> list(String resourceName, String resourceGroupName, Context context) {
        PagedIterable<NetworkMappingInner> inner = this.serviceClient().list(resourceName, resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new NetworkMappingImpl(inner1, this.manager()));
    }

    public NetworkMapping getById(String id) {
        String resourceName = Utils.getValueFromIdByName(id, "vaults");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'vaults'.", 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 fabricName = Utils.getValueFromIdByName(id, "replicationFabrics");
        if (fabricName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationFabrics'.", id)));
        }
        String networkName = Utils.getValueFromIdByName(id, "replicationNetworks");
        if (networkName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationNetworks'.", id)));
        }
        String networkMappingName = Utils.getValueFromIdByName(id, "replicationNetworkMappings");
        if (networkMappingName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationNetworkMappings'.",
                                id)));
        }
        return this
            .getWithResponse(resourceName, resourceGroupName, fabricName, networkName, networkMappingName, Context.NONE)
            .getValue();
    }

    public Response<NetworkMapping> getByIdWithResponse(String id, Context context) {
        String resourceName = Utils.getValueFromIdByName(id, "vaults");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'vaults'.", 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 fabricName = Utils.getValueFromIdByName(id, "replicationFabrics");
        if (fabricName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationFabrics'.", id)));
        }
        String networkName = Utils.getValueFromIdByName(id, "replicationNetworks");
        if (networkName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationNetworks'.", id)));
        }
        String networkMappingName = Utils.getValueFromIdByName(id, "replicationNetworkMappings");
        if (networkMappingName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationNetworkMappings'.",
                                id)));
        }
        return this
            .getWithResponse(resourceName, resourceGroupName, fabricName, networkName, networkMappingName, context);
    }

    public void deleteById(String id) {
        String resourceName = Utils.getValueFromIdByName(id, "vaults");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'vaults'.", 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 fabricName = Utils.getValueFromIdByName(id, "replicationFabrics");
        if (fabricName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationFabrics'.", id)));
        }
        String networkName = Utils.getValueFromIdByName(id, "replicationNetworks");
        if (networkName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationNetworks'.", id)));
        }
        String networkMappingName = Utils.getValueFromIdByName(id, "replicationNetworkMappings");
        if (networkMappingName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationNetworkMappings'.",
                                id)));
        }
        this.delete(resourceName, resourceGroupName, fabricName, networkName, networkMappingName, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, Context context) {
        String resourceName = Utils.getValueFromIdByName(id, "vaults");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'vaults'.", 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 fabricName = Utils.getValueFromIdByName(id, "replicationFabrics");
        if (fabricName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationFabrics'.", id)));
        }
        String networkName = Utils.getValueFromIdByName(id, "replicationNetworks");
        if (networkName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationNetworks'.", id)));
        }
        String networkMappingName = Utils.getValueFromIdByName(id, "replicationNetworkMappings");
        if (networkMappingName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationNetworkMappings'.",
                                id)));
        }
        this.delete(resourceName, resourceGroupName, fabricName, networkName, networkMappingName, context);
    }

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

    private com.azure.resourcemanager.recoveryservicessiterecovery.SiteRecoveryManager manager() {
        return this.serviceManager;
    }

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