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

package com.azure.resourcemanager.recoveryservicesbackup.implementation;

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.recoveryservicesbackup.fluent.ProtectedItemsClient;
import com.azure.resourcemanager.recoveryservicesbackup.fluent.models.ProtectedItemResourceInner;
import com.azure.resourcemanager.recoveryservicesbackup.models.ProtectedItemResource;
import com.azure.resourcemanager.recoveryservicesbackup.models.ProtectedItems;

public final class ProtectedItemsImpl implements ProtectedItems {
    private static final ClientLogger LOGGER = new ClientLogger(ProtectedItemsImpl.class);

    private final ProtectedItemsClient innerClient;

    private final com.azure.resourcemanager.recoveryservicesbackup.RecoveryServicesBackupManager serviceManager;

    public ProtectedItemsImpl(
        ProtectedItemsClient innerClient,
        com.azure.resourcemanager.recoveryservicesbackup.RecoveryServicesBackupManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ProtectedItemResource> getWithResponse(
        String vaultName,
        String resourceGroupName,
        String fabricName,
        String containerName,
        String protectedItemName,
        String filter,
        Context context) {
        Response<ProtectedItemResourceInner> inner =
            this
                .serviceClient()
                .getWithResponse(
                    vaultName, resourceGroupName, fabricName, containerName, protectedItemName, filter, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ProtectedItemResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ProtectedItemResource get(
        String vaultName, String resourceGroupName, String fabricName, String containerName, String protectedItemName) {
        ProtectedItemResourceInner inner =
            this.serviceClient().get(vaultName, resourceGroupName, fabricName, containerName, protectedItemName);
        if (inner != null) {
            return new ProtectedItemResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> deleteWithResponse(
        String vaultName,
        String resourceGroupName,
        String fabricName,
        String containerName,
        String protectedItemName,
        Context context) {
        return this
            .serviceClient()
            .deleteWithResponse(vaultName, resourceGroupName, fabricName, containerName, protectedItemName, context);
    }

    public void delete(
        String vaultName, String resourceGroupName, String fabricName, String containerName, String protectedItemName) {
        this.serviceClient().delete(vaultName, resourceGroupName, fabricName, containerName, protectedItemName);
    }

    public ProtectedItemResource getById(String id) {
        String vaultName = Utils.getValueFromIdByName(id, "vaults");
        if (vaultName == 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, "backupFabrics");
        if (fabricName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'backupFabrics'.", id)));
        }
        String containerName = Utils.getValueFromIdByName(id, "protectionContainers");
        if (containerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'protectionContainers'.",
                                id)));
        }
        String protectedItemName = Utils.getValueFromIdByName(id, "protectedItems");
        if (protectedItemName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'protectedItems'.", id)));
        }
        String localFilter = null;
        return this
            .getWithResponse(
                vaultName, resourceGroupName, fabricName, containerName, protectedItemName, localFilter, Context.NONE)
            .getValue();
    }

    public Response<ProtectedItemResource> getByIdWithResponse(String id, String filter, Context context) {
        String vaultName = Utils.getValueFromIdByName(id, "vaults");
        if (vaultName == 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, "backupFabrics");
        if (fabricName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'backupFabrics'.", id)));
        }
        String containerName = Utils.getValueFromIdByName(id, "protectionContainers");
        if (containerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'protectionContainers'.",
                                id)));
        }
        String protectedItemName = Utils.getValueFromIdByName(id, "protectedItems");
        if (protectedItemName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'protectedItems'.", id)));
        }
        return this
            .getWithResponse(
                vaultName, resourceGroupName, fabricName, containerName, protectedItemName, filter, context);
    }

    public void deleteById(String id) {
        String vaultName = Utils.getValueFromIdByName(id, "vaults");
        if (vaultName == 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, "backupFabrics");
        if (fabricName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'backupFabrics'.", id)));
        }
        String containerName = Utils.getValueFromIdByName(id, "protectionContainers");
        if (containerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'protectionContainers'.",
                                id)));
        }
        String protectedItemName = Utils.getValueFromIdByName(id, "protectedItems");
        if (protectedItemName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'protectedItems'.", id)));
        }
        this
            .deleteWithResponse(
                vaultName, resourceGroupName, fabricName, containerName, protectedItemName, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(String id, Context context) {
        String vaultName = Utils.getValueFromIdByName(id, "vaults");
        if (vaultName == 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, "backupFabrics");
        if (fabricName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'backupFabrics'.", id)));
        }
        String containerName = Utils.getValueFromIdByName(id, "protectionContainers");
        if (containerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'protectionContainers'.",
                                id)));
        }
        String protectedItemName = Utils.getValueFromIdByName(id, "protectedItems");
        if (protectedItemName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'protectedItems'.", id)));
        }
        return this
            .deleteWithResponse(vaultName, resourceGroupName, fabricName, containerName, protectedItemName, context);
    }

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

    private com.azure.resourcemanager.recoveryservicesbackup.RecoveryServicesBackupManager manager() {
        return this.serviceManager;
    }

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