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

package com.azure.resourcemanager.digitaltwins.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.digitaltwins.fluent.TimeSeriesDatabaseConnectionsClient;
import com.azure.resourcemanager.digitaltwins.fluent.models.TimeSeriesDatabaseConnectionInner;
import com.azure.resourcemanager.digitaltwins.models.CleanupConnectionArtifacts;
import com.azure.resourcemanager.digitaltwins.models.TimeSeriesDatabaseConnection;
import com.azure.resourcemanager.digitaltwins.models.TimeSeriesDatabaseConnections;

public final class TimeSeriesDatabaseConnectionsImpl implements TimeSeriesDatabaseConnections {
    private static final ClientLogger LOGGER = new ClientLogger(TimeSeriesDatabaseConnectionsImpl.class);

    private final TimeSeriesDatabaseConnectionsClient innerClient;

    private final com.azure.resourcemanager.digitaltwins.AzureDigitalTwinsManager serviceManager;

    public TimeSeriesDatabaseConnectionsImpl(
        TimeSeriesDatabaseConnectionsClient innerClient,
        com.azure.resourcemanager.digitaltwins.AzureDigitalTwinsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<TimeSeriesDatabaseConnection> list(String resourceGroupName, String resourceName) {
        PagedIterable<TimeSeriesDatabaseConnectionInner> inner =
            this.serviceClient().list(resourceGroupName, resourceName);
        return Utils.mapPage(inner, inner1 -> new TimeSeriesDatabaseConnectionImpl(inner1, this.manager()));
    }

    public PagedIterable<TimeSeriesDatabaseConnection> list(
        String resourceGroupName, String resourceName, Context context) {
        PagedIterable<TimeSeriesDatabaseConnectionInner> inner =
            this.serviceClient().list(resourceGroupName, resourceName, context);
        return Utils.mapPage(inner, inner1 -> new TimeSeriesDatabaseConnectionImpl(inner1, this.manager()));
    }

    public Response<TimeSeriesDatabaseConnection> getWithResponse(
        String resourceGroupName, String resourceName, String timeSeriesDatabaseConnectionName, Context context) {
        Response<TimeSeriesDatabaseConnectionInner> inner =
            this
                .serviceClient()
                .getWithResponse(resourceGroupName, resourceName, timeSeriesDatabaseConnectionName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new TimeSeriesDatabaseConnectionImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public TimeSeriesDatabaseConnection get(
        String resourceGroupName, String resourceName, String timeSeriesDatabaseConnectionName) {
        TimeSeriesDatabaseConnectionInner inner =
            this.serviceClient().get(resourceGroupName, resourceName, timeSeriesDatabaseConnectionName);
        if (inner != null) {
            return new TimeSeriesDatabaseConnectionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public TimeSeriesDatabaseConnection delete(
        String resourceGroupName, String resourceName, String timeSeriesDatabaseConnectionName) {
        TimeSeriesDatabaseConnectionInner inner =
            this.serviceClient().delete(resourceGroupName, resourceName, timeSeriesDatabaseConnectionName);
        if (inner != null) {
            return new TimeSeriesDatabaseConnectionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public TimeSeriesDatabaseConnection delete(
        String resourceGroupName,
        String resourceName,
        String timeSeriesDatabaseConnectionName,
        CleanupConnectionArtifacts cleanupConnectionArtifacts,
        Context context) {
        TimeSeriesDatabaseConnectionInner inner =
            this
                .serviceClient()
                .delete(
                    resourceGroupName,
                    resourceName,
                    timeSeriesDatabaseConnectionName,
                    cleanupConnectionArtifacts,
                    context);
        if (inner != null) {
            return new TimeSeriesDatabaseConnectionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public TimeSeriesDatabaseConnection 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 resourceName = Utils.getValueFromIdByName(id, "digitalTwinsInstances");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'digitalTwinsInstances'.",
                                id)));
        }
        String timeSeriesDatabaseConnectionName = Utils.getValueFromIdByName(id, "timeSeriesDatabaseConnections");
        if (timeSeriesDatabaseConnectionName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment"
                                    + " 'timeSeriesDatabaseConnections'.",
                                id)));
        }
        CleanupConnectionArtifacts localCleanupConnectionArtifacts = null;
        return this
            .delete(
                resourceGroupName,
                resourceName,
                timeSeriesDatabaseConnectionName,
                localCleanupConnectionArtifacts,
                Context.NONE);
    }

    public TimeSeriesDatabaseConnection deleteByIdWithResponse(
        String id, CleanupConnectionArtifacts cleanupConnectionArtifacts, 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 resourceName = Utils.getValueFromIdByName(id, "digitalTwinsInstances");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'digitalTwinsInstances'.",
                                id)));
        }
        String timeSeriesDatabaseConnectionName = Utils.getValueFromIdByName(id, "timeSeriesDatabaseConnections");
        if (timeSeriesDatabaseConnectionName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment"
                                    + " 'timeSeriesDatabaseConnections'.",
                                id)));
        }
        return this
            .delete(
                resourceGroupName, resourceName, timeSeriesDatabaseConnectionName, cleanupConnectionArtifacts, context);
    }

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

    private com.azure.resourcemanager.digitaltwins.AzureDigitalTwinsManager manager() {
        return this.serviceManager;
    }

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