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

package com.azure.resourcemanager.deploymentmanager.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.deploymentmanager.fluent.StepsClient;
import com.azure.resourcemanager.deploymentmanager.fluent.models.StepResourceInner;
import com.azure.resourcemanager.deploymentmanager.models.StepResource;
import com.azure.resourcemanager.deploymentmanager.models.Steps;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public final class StepsImpl implements Steps {
    private static final ClientLogger LOGGER = new ClientLogger(StepsImpl.class);

    private final StepsClient innerClient;

    private final com.azure.resourcemanager.deploymentmanager.DeploymentManager serviceManager;

    public StepsImpl(
        StepsClient innerClient, com.azure.resourcemanager.deploymentmanager.DeploymentManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<StepResource> getByResourceGroupWithResponse(
        String resourceGroupName, String stepName, Context context) {
        Response<StepResourceInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, stepName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new StepResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public StepResource getByResourceGroup(String resourceGroupName, String stepName) {
        StepResourceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, stepName);
        if (inner != null) {
            return new StepResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void deleteByResourceGroup(String resourceGroupName, String stepName) {
        this.serviceClient().delete(resourceGroupName, stepName);
    }

    public Response<List<StepResource>> listWithResponse(String resourceGroupName, Context context) {
        Response<List<StepResourceInner>> inner = this.serviceClient().listWithResponse(resourceGroupName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                inner
                    .getValue()
                    .stream()
                    .map(inner1 -> new StepResourceImpl(inner1, this.manager()))
                    .collect(Collectors.toList()));
        } else {
            return null;
        }
    }

    public List<StepResource> list(String resourceGroupName) {
        List<StepResourceInner> inner = this.serviceClient().list(resourceGroupName);
        if (inner != null) {
            return Collections
                .unmodifiableList(
                    inner
                        .stream()
                        .map(inner1 -> new StepResourceImpl(inner1, this.manager()))
                        .collect(Collectors.toList()));
        } else {
            return Collections.emptyList();
        }
    }

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

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

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

    private com.azure.resourcemanager.deploymentmanager.DeploymentManager manager() {
        return this.serviceManager;
    }

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