// 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.ReplicationProtectionContainerMappingsClient;
import com.azure.resourcemanager.recoveryservicessiterecovery.fluent.models.ProtectionContainerMappingInner;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.ProtectionContainerMapping;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.RemoveProtectionContainerMappingInput;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.ReplicationProtectionContainerMappings;

public final class ReplicationProtectionContainerMappingsImpl implements ReplicationProtectionContainerMappings {
    private static final ClientLogger LOGGER = new ClientLogger(ReplicationProtectionContainerMappingsImpl.class);

    private final ReplicationProtectionContainerMappingsClient innerClient;

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

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

    public PagedIterable<ProtectionContainerMapping> listByReplicationProtectionContainers(
        String resourceName, String resourceGroupName, String fabricName, String protectionContainerName) {
        PagedIterable<ProtectionContainerMappingInner> inner =
            this
                .serviceClient()
                .listByReplicationProtectionContainers(
                    resourceName, resourceGroupName, fabricName, protectionContainerName);
        return Utils.mapPage(inner, inner1 -> new ProtectionContainerMappingImpl(inner1, this.manager()));
    }

    public PagedIterable<ProtectionContainerMapping> listByReplicationProtectionContainers(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String protectionContainerName,
        Context context) {
        PagedIterable<ProtectionContainerMappingInner> inner =
            this
                .serviceClient()
                .listByReplicationProtectionContainers(
                    resourceName, resourceGroupName, fabricName, protectionContainerName, context);
        return Utils.mapPage(inner, inner1 -> new ProtectionContainerMappingImpl(inner1, this.manager()));
    }

    public Response<ProtectionContainerMapping> getWithResponse(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String protectionContainerName,
        String mappingName,
        Context context) {
        Response<ProtectionContainerMappingInner> inner =
            this
                .serviceClient()
                .getWithResponse(
                    resourceName, resourceGroupName, fabricName, protectionContainerName, mappingName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ProtectionContainerMappingImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ProtectionContainerMapping get(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String protectionContainerName,
        String mappingName) {
        ProtectionContainerMappingInner inner =
            this.serviceClient().get(resourceName, resourceGroupName, fabricName, protectionContainerName, mappingName);
        if (inner != null) {
            return new ProtectionContainerMappingImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void purge(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String protectionContainerName,
        String mappingName) {
        this.serviceClient().purge(resourceName, resourceGroupName, fabricName, protectionContainerName, mappingName);
    }

    public void purge(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String protectionContainerName,
        String mappingName,
        Context context) {
        this
            .serviceClient()
            .purge(resourceName, resourceGroupName, fabricName, protectionContainerName, mappingName, context);
    }

    public void delete(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String protectionContainerName,
        String mappingName,
        RemoveProtectionContainerMappingInput removalInput) {
        this
            .serviceClient()
            .delete(resourceName, resourceGroupName, fabricName, protectionContainerName, mappingName, removalInput);
    }

    public void delete(
        String resourceName,
        String resourceGroupName,
        String fabricName,
        String protectionContainerName,
        String mappingName,
        RemoveProtectionContainerMappingInput removalInput,
        Context context) {
        this
            .serviceClient()
            .delete(
                resourceName,
                resourceGroupName,
                fabricName,
                protectionContainerName,
                mappingName,
                removalInput,
                context);
    }

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

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

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

    public Response<ProtectionContainerMapping> 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 protectionContainerName = Utils.getValueFromIdByName(id, "replicationProtectionContainers");
        if (protectionContainerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment"
                                    + " 'replicationProtectionContainers'.",
                                id)));
        }
        String mappingName = Utils.getValueFromIdByName(id, "replicationProtectionContainerMappings");
        if (mappingName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment"
                                    + " 'replicationProtectionContainerMappings'.",
                                id)));
        }
        return this
            .getWithResponse(
                resourceName, resourceGroupName, fabricName, protectionContainerName, mappingName, context);
    }

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

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

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