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

package com.azure.resourcemanager.synapse.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.synapse.fluent.WorkspaceManagedSqlServerEncryptionProtectorsClient;
import com.azure.resourcemanager.synapse.fluent.models.EncryptionProtectorInner;
import com.azure.resourcemanager.synapse.models.EncryptionProtector;
import com.azure.resourcemanager.synapse.models.EncryptionProtectorName;
import com.azure.resourcemanager.synapse.models.WorkspaceManagedSqlServerEncryptionProtectors;

public final class WorkspaceManagedSqlServerEncryptionProtectorsImpl
    implements WorkspaceManagedSqlServerEncryptionProtectors {
    private static final ClientLogger LOGGER =
        new ClientLogger(WorkspaceManagedSqlServerEncryptionProtectorsImpl.class);

    private final WorkspaceManagedSqlServerEncryptionProtectorsClient innerClient;

    private final com.azure.resourcemanager.synapse.SynapseManager serviceManager;

    public WorkspaceManagedSqlServerEncryptionProtectorsImpl(
        WorkspaceManagedSqlServerEncryptionProtectorsClient innerClient,
        com.azure.resourcemanager.synapse.SynapseManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<EncryptionProtector> getWithResponse(
        String resourceGroupName,
        String workspaceName,
        EncryptionProtectorName encryptionProtectorName,
        Context context) {
        Response<EncryptionProtectorInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, encryptionProtectorName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new EncryptionProtectorImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public EncryptionProtector get(
        String resourceGroupName, String workspaceName, EncryptionProtectorName encryptionProtectorName) {
        EncryptionProtectorInner inner =
            this.serviceClient().get(resourceGroupName, workspaceName, encryptionProtectorName);
        if (inner != null) {
            return new EncryptionProtectorImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<EncryptionProtector> list(String resourceGroupName, String workspaceName) {
        PagedIterable<EncryptionProtectorInner> inner = this.serviceClient().list(resourceGroupName, workspaceName);
        return Utils.mapPage(inner, inner1 -> new EncryptionProtectorImpl(inner1, this.manager()));
    }

    public PagedIterable<EncryptionProtector> list(String resourceGroupName, String workspaceName, Context context) {
        PagedIterable<EncryptionProtectorInner> inner =
            this.serviceClient().list(resourceGroupName, workspaceName, context);
        return Utils.mapPage(inner, inner1 -> new EncryptionProtectorImpl(inner1, this.manager()));
    }

    public void revalidate(
        String resourceGroupName, String workspaceName, EncryptionProtectorName encryptionProtectorName) {
        this.serviceClient().revalidate(resourceGroupName, workspaceName, encryptionProtectorName);
    }

    public void revalidate(
        String resourceGroupName,
        String workspaceName,
        EncryptionProtectorName encryptionProtectorName,
        Context context) {
        this.serviceClient().revalidate(resourceGroupName, workspaceName, encryptionProtectorName, context);
    }

    public EncryptionProtector 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 workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        String encryptionProtectorNameLocal = Utils.getValueFromIdByName(id, "encryptionProtector");
        if (encryptionProtectorNameLocal == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'encryptionProtector'.", id)));
        }
        EncryptionProtectorName encryptionProtectorName =
            EncryptionProtectorName.fromString(encryptionProtectorNameLocal);
        return this.getWithResponse(resourceGroupName, workspaceName, encryptionProtectorName, Context.NONE).getValue();
    }

    public Response<EncryptionProtector> 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 workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        String encryptionProtectorNameLocal = Utils.getValueFromIdByName(id, "encryptionProtector");
        if (encryptionProtectorNameLocal == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'encryptionProtector'.", id)));
        }
        EncryptionProtectorName encryptionProtectorName =
            EncryptionProtectorName.fromString(encryptionProtectorNameLocal);
        return this.getWithResponse(resourceGroupName, workspaceName, encryptionProtectorName, context);
    }

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

    private com.azure.resourcemanager.synapse.SynapseManager manager() {
        return this.serviceManager;
    }

    public EncryptionProtectorImpl define(EncryptionProtectorName name) {
        return new EncryptionProtectorImpl(name, this.manager());
    }
}
