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

package com.azure.resourcemanager.timeseriesinsights.implementation;

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.timeseriesinsights.fluent.ReferenceDataSetsClient;
import com.azure.resourcemanager.timeseriesinsights.fluent.models.ReferenceDataSetListResponseInner;
import com.azure.resourcemanager.timeseriesinsights.fluent.models.ReferenceDataSetResourceInner;
import com.azure.resourcemanager.timeseriesinsights.models.ReferenceDataSetListResponse;
import com.azure.resourcemanager.timeseriesinsights.models.ReferenceDataSetResource;
import com.azure.resourcemanager.timeseriesinsights.models.ReferenceDataSets;

public final class ReferenceDataSetsImpl implements ReferenceDataSets {
    private static final ClientLogger LOGGER = new ClientLogger(ReferenceDataSetsImpl.class);

    private final ReferenceDataSetsClient innerClient;

    private final com.azure.resourcemanager.timeseriesinsights.TimeSeriesInsightsManager serviceManager;

    public ReferenceDataSetsImpl(
        ReferenceDataSetsClient innerClient,
        com.azure.resourcemanager.timeseriesinsights.TimeSeriesInsightsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ReferenceDataSetResource> getWithResponse(
        String resourceGroupName, String environmentName, String referenceDataSetName, Context context) {
        Response<ReferenceDataSetResourceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, environmentName, referenceDataSetName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ReferenceDataSetResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ReferenceDataSetResource get(String resourceGroupName, String environmentName, String referenceDataSetName) {
        ReferenceDataSetResourceInner inner =
            this.serviceClient().get(resourceGroupName, environmentName, referenceDataSetName);
        if (inner != null) {
            return new ReferenceDataSetResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void delete(String resourceGroupName, String environmentName, String referenceDataSetName) {
        this.serviceClient().delete(resourceGroupName, environmentName, referenceDataSetName);
    }

    public Response<ReferenceDataSetListResponse> listByEnvironmentWithResponse(
        String resourceGroupName, String environmentName, Context context) {
        Response<ReferenceDataSetListResponseInner> inner =
            this.serviceClient().listByEnvironmentWithResponse(resourceGroupName, environmentName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ReferenceDataSetListResponseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ReferenceDataSetListResponse listByEnvironment(String resourceGroupName, String environmentName) {
        ReferenceDataSetListResponseInner inner =
            this.serviceClient().listByEnvironment(resourceGroupName, environmentName);
        if (inner != null) {
            return new ReferenceDataSetListResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.timeseriesinsights.TimeSeriesInsightsManager manager() {
        return this.serviceManager;
    }

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