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

package com.azure.resourcemanager.machinelearningservices.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.machinelearningservices.fluent.WorkspaceConnectionsClient;
import com.azure.resourcemanager.machinelearningservices.fluent.models.WorkspaceConnectionInner;
import com.azure.resourcemanager.machinelearningservices.models.WorkspaceConnection;
import com.azure.resourcemanager.machinelearningservices.models.WorkspaceConnections;
import com.fasterxml.jackson.annotation.JsonIgnore;

public final class WorkspaceConnectionsImpl implements WorkspaceConnections {
    @JsonIgnore private final ClientLogger logger = new ClientLogger(WorkspaceConnectionsImpl.class);

    private final WorkspaceConnectionsClient innerClient;

    private final com.azure.resourcemanager.machinelearningservices.MachineLearningServicesManager serviceManager;

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

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

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

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

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

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

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

    public WorkspaceConnection 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<WorkspaceConnection> 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).getValue();
    }

    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.machinelearningservices.MachineLearningServicesManager manager() {
        return this.serviceManager;
    }

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