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

package com.azure.resourcemanager.dataprotection.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.dataprotection.fluent.BackupInstancesClient;
import com.azure.resourcemanager.dataprotection.fluent.models.BackupInstanceResourceInner;
import com.azure.resourcemanager.dataprotection.fluent.models.OperationJobExtendedInfoInner;
import com.azure.resourcemanager.dataprotection.models.AzureBackupRehydrationRequest;
import com.azure.resourcemanager.dataprotection.models.AzureBackupRestoreRequest;
import com.azure.resourcemanager.dataprotection.models.BackupInstanceResource;
import com.azure.resourcemanager.dataprotection.models.BackupInstances;
import com.azure.resourcemanager.dataprotection.models.OperationJobExtendedInfo;
import com.azure.resourcemanager.dataprotection.models.SyncBackupInstanceRequest;
import com.azure.resourcemanager.dataprotection.models.TriggerBackupRequest;
import com.azure.resourcemanager.dataprotection.models.ValidateForBackupRequest;
import com.azure.resourcemanager.dataprotection.models.ValidateRestoreRequestObject;

public final class BackupInstancesImpl implements BackupInstances {
    private static final ClientLogger LOGGER = new ClientLogger(BackupInstancesImpl.class);

    private final BackupInstancesClient innerClient;

    private final com.azure.resourcemanager.dataprotection.DataProtectionManager serviceManager;

    public BackupInstancesImpl(
        BackupInstancesClient innerClient,
        com.azure.resourcemanager.dataprotection.DataProtectionManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<BackupInstanceResource> list(String resourceGroupName, String vaultName) {
        PagedIterable<BackupInstanceResourceInner> inner = this.serviceClient().list(resourceGroupName, vaultName);
        return Utils.mapPage(inner, inner1 -> new BackupInstanceResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<BackupInstanceResource> list(String resourceGroupName, String vaultName, Context context) {
        PagedIterable<BackupInstanceResourceInner> inner =
            this.serviceClient().list(resourceGroupName, vaultName, context);
        return Utils.mapPage(inner, inner1 -> new BackupInstanceResourceImpl(inner1, this.manager()));
    }

    public Response<BackupInstanceResource> getWithResponse(
        String resourceGroupName, String vaultName, String backupInstanceName, Context context) {
        Response<BackupInstanceResourceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, vaultName, backupInstanceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new BackupInstanceResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public BackupInstanceResource get(String resourceGroupName, String vaultName, String backupInstanceName) {
        BackupInstanceResourceInner inner = this.serviceClient().get(resourceGroupName, vaultName, backupInstanceName);
        if (inner != null) {
            return new BackupInstanceResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String vaultName, String backupInstanceName) {
        this.serviceClient().delete(resourceGroupName, vaultName, backupInstanceName);
    }

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

    public OperationJobExtendedInfo adhocBackup(
        String resourceGroupName, String vaultName, String backupInstanceName, TriggerBackupRequest parameters) {
        OperationJobExtendedInfoInner inner =
            this.serviceClient().adhocBackup(resourceGroupName, vaultName, backupInstanceName, parameters);
        if (inner != null) {
            return new OperationJobExtendedInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationJobExtendedInfo adhocBackup(
        String resourceGroupName,
        String vaultName,
        String backupInstanceName,
        TriggerBackupRequest parameters,
        Context context) {
        OperationJobExtendedInfoInner inner =
            this.serviceClient().adhocBackup(resourceGroupName, vaultName, backupInstanceName, parameters, context);
        if (inner != null) {
            return new OperationJobExtendedInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationJobExtendedInfo validateForBackup(
        String resourceGroupName, String vaultName, ValidateForBackupRequest parameters) {
        OperationJobExtendedInfoInner inner =
            this.serviceClient().validateForBackup(resourceGroupName, vaultName, parameters);
        if (inner != null) {
            return new OperationJobExtendedInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationJobExtendedInfo validateForBackup(
        String resourceGroupName, String vaultName, ValidateForBackupRequest parameters, Context context) {
        OperationJobExtendedInfoInner inner =
            this.serviceClient().validateForBackup(resourceGroupName, vaultName, parameters, context);
        if (inner != null) {
            return new OperationJobExtendedInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<BackupInstanceResource> getBackupInstanceOperationResultWithResponse(
        String resourceGroupName, String vaultName, String backupInstanceName, String operationId, Context context) {
        Response<BackupInstanceResourceInner> inner =
            this
                .serviceClient()
                .getBackupInstanceOperationResultWithResponse(
                    resourceGroupName, vaultName, backupInstanceName, operationId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new BackupInstanceResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public BackupInstanceResource getBackupInstanceOperationResult(
        String resourceGroupName, String vaultName, String backupInstanceName, String operationId) {
        BackupInstanceResourceInner inner =
            this
                .serviceClient()
                .getBackupInstanceOperationResult(resourceGroupName, vaultName, backupInstanceName, operationId);
        if (inner != null) {
            return new BackupInstanceResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void triggerRehydrate(
        String resourceGroupName,
        String vaultName,
        String backupInstanceName,
        AzureBackupRehydrationRequest parameters) {
        this.serviceClient().triggerRehydrate(resourceGroupName, vaultName, backupInstanceName, parameters);
    }

    public void triggerRehydrate(
        String resourceGroupName,
        String vaultName,
        String backupInstanceName,
        AzureBackupRehydrationRequest parameters,
        Context context) {
        this.serviceClient().triggerRehydrate(resourceGroupName, vaultName, backupInstanceName, parameters, context);
    }

    public OperationJobExtendedInfo triggerRestore(
        String resourceGroupName, String vaultName, String backupInstanceName, AzureBackupRestoreRequest parameters) {
        OperationJobExtendedInfoInner inner =
            this.serviceClient().triggerRestore(resourceGroupName, vaultName, backupInstanceName, parameters);
        if (inner != null) {
            return new OperationJobExtendedInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationJobExtendedInfo triggerRestore(
        String resourceGroupName,
        String vaultName,
        String backupInstanceName,
        AzureBackupRestoreRequest parameters,
        Context context) {
        OperationJobExtendedInfoInner inner =
            this.serviceClient().triggerRestore(resourceGroupName, vaultName, backupInstanceName, parameters, context);
        if (inner != null) {
            return new OperationJobExtendedInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void resumeBackups(String resourceGroupName, String vaultName, String backupInstanceName) {
        this.serviceClient().resumeBackups(resourceGroupName, vaultName, backupInstanceName);
    }

    public void resumeBackups(String resourceGroupName, String vaultName, String backupInstanceName, Context context) {
        this.serviceClient().resumeBackups(resourceGroupName, vaultName, backupInstanceName, context);
    }

    public void resumeProtection(String resourceGroupName, String vaultName, String backupInstanceName) {
        this.serviceClient().resumeProtection(resourceGroupName, vaultName, backupInstanceName);
    }

    public void resumeProtection(
        String resourceGroupName, String vaultName, String backupInstanceName, Context context) {
        this.serviceClient().resumeProtection(resourceGroupName, vaultName, backupInstanceName, context);
    }

    public void stopProtection(String resourceGroupName, String vaultName, String backupInstanceName) {
        this.serviceClient().stopProtection(resourceGroupName, vaultName, backupInstanceName);
    }

    public void stopProtection(String resourceGroupName, String vaultName, String backupInstanceName, Context context) {
        this.serviceClient().stopProtection(resourceGroupName, vaultName, backupInstanceName, context);
    }

    public void suspendBackups(String resourceGroupName, String vaultName, String backupInstanceName) {
        this.serviceClient().suspendBackups(resourceGroupName, vaultName, backupInstanceName);
    }

    public void suspendBackups(String resourceGroupName, String vaultName, String backupInstanceName, Context context) {
        this.serviceClient().suspendBackups(resourceGroupName, vaultName, backupInstanceName, context);
    }

    public void syncBackupInstance(
        String resourceGroupName, String vaultName, String backupInstanceName, SyncBackupInstanceRequest parameters) {
        this.serviceClient().syncBackupInstance(resourceGroupName, vaultName, backupInstanceName, parameters);
    }

    public void syncBackupInstance(
        String resourceGroupName,
        String vaultName,
        String backupInstanceName,
        SyncBackupInstanceRequest parameters,
        Context context) {
        this.serviceClient().syncBackupInstance(resourceGroupName, vaultName, backupInstanceName, parameters, context);
    }

    public OperationJobExtendedInfo validateForRestore(
        String resourceGroupName,
        String vaultName,
        String backupInstanceName,
        ValidateRestoreRequestObject parameters) {
        OperationJobExtendedInfoInner inner =
            this.serviceClient().validateForRestore(resourceGroupName, vaultName, backupInstanceName, parameters);
        if (inner != null) {
            return new OperationJobExtendedInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationJobExtendedInfo validateForRestore(
        String resourceGroupName,
        String vaultName,
        String backupInstanceName,
        ValidateRestoreRequestObject parameters,
        Context context) {
        OperationJobExtendedInfoInner inner =
            this
                .serviceClient()
                .validateForRestore(resourceGroupName, vaultName, backupInstanceName, parameters, context);
        if (inner != null) {
            return new OperationJobExtendedInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.dataprotection.DataProtectionManager manager() {
        return this.serviceManager;
    }

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