// 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.RolloutsClient;
import com.azure.resourcemanager.deploymentmanager.fluent.models.RolloutInner;
import com.azure.resourcemanager.deploymentmanager.models.Rollout;
import com.azure.resourcemanager.deploymentmanager.models.Rollouts;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public final class RolloutsImpl implements Rollouts {
    private static final ClientLogger LOGGER = new ClientLogger(RolloutsImpl.class);

    private final RolloutsClient innerClient;

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

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

    public Response<Rollout> getByResourceGroupWithResponse(
        String resourceGroupName, String rolloutName, Integer retryAttempt, Context context) {
        Response<RolloutInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, rolloutName, retryAttempt, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RolloutImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Rollout getByResourceGroup(String resourceGroupName, String rolloutName) {
        RolloutInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, rolloutName);
        if (inner != null) {
            return new RolloutImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public Response<Rollout> cancelWithResponse(String resourceGroupName, String rolloutName, Context context) {
        Response<RolloutInner> inner = this.serviceClient().cancelWithResponse(resourceGroupName, rolloutName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RolloutImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Rollout cancel(String resourceGroupName, String rolloutName) {
        RolloutInner inner = this.serviceClient().cancel(resourceGroupName, rolloutName);
        if (inner != null) {
            return new RolloutImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Rollout> restartWithResponse(
        String resourceGroupName, String rolloutName, Boolean skipSucceeded, Context context) {
        Response<RolloutInner> inner =
            this.serviceClient().restartWithResponse(resourceGroupName, rolloutName, skipSucceeded, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RolloutImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Rollout restart(String resourceGroupName, String rolloutName) {
        RolloutInner inner = this.serviceClient().restart(resourceGroupName, rolloutName);
        if (inner != null) {
            return new RolloutImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

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