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

package com.azure.resourcemanager.machinelearning.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.machinelearning.fluent.WorkspaceConnectionsClient;
import com.azure.resourcemanager.machinelearning.fluent.models.WorkspaceConnectionPropertiesV2BasicResourceInner;
import com.azure.resourcemanager.machinelearning.models.WorkspaceConnectionPropertiesV2BasicResource;
import com.azure.resourcemanager.machinelearning.models.WorkspaceConnections;

public final class WorkspaceConnectionsImpl implements WorkspaceConnections {
    private static final ClientLogger LOGGER = new ClientLogger(WorkspaceConnectionsImpl.class);

    private final WorkspaceConnectionsClient innerClient;

    private final com.azure.resourcemanager.machinelearning.MachineLearningManager serviceManager;

    public WorkspaceConnectionsImpl(
        WorkspaceConnectionsClient innerClient,
        com.azure.resourcemanager.machinelearning.MachineLearningManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<WorkspaceConnectionPropertiesV2BasicResource> getWithResponse(
        String resourceGroupName, String workspaceName, String connectionName, Context context) {
        Response<WorkspaceConnectionPropertiesV2BasicResourceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, connectionName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new WorkspaceConnectionPropertiesV2BasicResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public WorkspaceConnectionPropertiesV2BasicResource get(
        String resourceGroupName, String workspaceName, String connectionName) {
        WorkspaceConnectionPropertiesV2BasicResourceInner inner =
            this.serviceClient().get(resourceGroupName, workspaceName, connectionName);
        if (inner != null) {
            return new WorkspaceConnectionPropertiesV2BasicResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> deleteWithResponse(
        String resourceGroupName, String workspaceName, String connectionName, Context context) {
        return this.serviceClient().deleteWithResponse(resourceGroupName, workspaceName, connectionName, context);
    }

    public void delete(String resourceGroupName, String workspaceName, String connectionName) {
        this.serviceClient().delete(resourceGroupName, workspaceName, connectionName);
    }

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

    public PagedIterable<WorkspaceConnectionPropertiesV2BasicResource> list(
        String resourceGroupName, String workspaceName, String target, String category, Context context) {
        PagedIterable<WorkspaceConnectionPropertiesV2BasicResourceInner> inner =
            this.serviceClient().list(resourceGroupName, workspaceName, target, category, context);
        return Utils
            .mapPage(inner, inner1 -> new WorkspaceConnectionPropertiesV2BasicResourceImpl(inner1, this.manager()));
    }

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

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

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

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

    private com.azure.resourcemanager.machinelearning.MachineLearningManager manager() {
        return this.serviceManager;
    }

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