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

package com.azure.resourcemanager.streamanalytics.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.streamanalytics.fluent.TransformationsClient;
import com.azure.resourcemanager.streamanalytics.fluent.models.TransformationInner;
import com.azure.resourcemanager.streamanalytics.models.Transformation;
import com.azure.resourcemanager.streamanalytics.models.Transformations;
import com.azure.resourcemanager.streamanalytics.models.TransformationsGetResponse;
import com.fasterxml.jackson.annotation.JsonIgnore;

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

    private final TransformationsClient innerClient;

    private final com.azure.resourcemanager.streamanalytics.StreamAnalyticsManager serviceManager;

    public TransformationsImpl(
        TransformationsClient innerClient,
        com.azure.resourcemanager.streamanalytics.StreamAnalyticsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Transformation get(String resourceGroupName, String jobName, String transformationName) {
        TransformationInner inner = this.serviceClient().get(resourceGroupName, jobName, transformationName);
        if (inner != null) {
            return new TransformationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Transformation> getWithResponse(
        String resourceGroupName, String jobName, String transformationName, Context context) {
        TransformationsGetResponse inner =
            this.serviceClient().getWithResponse(resourceGroupName, jobName, transformationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new TransformationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

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

    public Response<Transformation> 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 jobName = Utils.getValueFromIdByName(id, "streamingjobs");
        if (jobName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'streamingjobs'.", id)));
        }
        String transformationName = Utils.getValueFromIdByName(id, "transformations");
        if (transformationName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'transformations'.", id)));
        }
        return this.getWithResponse(resourceGroupName, jobName, transformationName, context);
    }

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

    private com.azure.resourcemanager.streamanalytics.StreamAnalyticsManager manager() {
        return this.serviceManager;
    }

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