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

package com.azure.resourcemanager.databricks.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.databricks.fluent.VNetPeeringsClient;
import com.azure.resourcemanager.databricks.fluent.models.VirtualNetworkPeeringInner;
import com.azure.resourcemanager.databricks.models.VNetPeerings;
import com.azure.resourcemanager.databricks.models.VirtualNetworkPeering;

public final class VNetPeeringsImpl implements VNetPeerings {
    private static final ClientLogger LOGGER = new ClientLogger(VNetPeeringsImpl.class);

    private final VNetPeeringsClient innerClient;

    private final com.azure.resourcemanager.databricks.AzureDatabricksManager serviceManager;

    public VNetPeeringsImpl(
        VNetPeeringsClient innerClient, com.azure.resourcemanager.databricks.AzureDatabricksManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

    public VirtualNetworkPeering get(String resourceGroupName, String workspaceName, String peeringName) {
        VirtualNetworkPeeringInner inner = this.serviceClient().get(resourceGroupName, workspaceName, peeringName);
        if (inner != null) {
            return new VirtualNetworkPeeringImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public PagedIterable<VirtualNetworkPeering> listByWorkspace(String resourceGroupName, String workspaceName) {
        PagedIterable<VirtualNetworkPeeringInner> inner =
            this.serviceClient().listByWorkspace(resourceGroupName, workspaceName);
        return Utils.mapPage(inner, inner1 -> new VirtualNetworkPeeringImpl(inner1, this.manager()));
    }

    public PagedIterable<VirtualNetworkPeering> listByWorkspace(
        String resourceGroupName, String workspaceName, Context context) {
        PagedIterable<VirtualNetworkPeeringInner> inner =
            this.serviceClient().listByWorkspace(resourceGroupName, workspaceName, context);
        return Utils.mapPage(inner, inner1 -> new VirtualNetworkPeeringImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.databricks.AzureDatabricksManager manager() {
        return this.serviceManager;
    }

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