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

package com.azure.resourcemanager.providerhub.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.providerhub.fluent.DefaultRolloutsClient;
import com.azure.resourcemanager.providerhub.fluent.models.DefaultRolloutInner;
import com.azure.resourcemanager.providerhub.models.DefaultRollout;
import com.azure.resourcemanager.providerhub.models.DefaultRollouts;

public final class DefaultRolloutsImpl implements DefaultRollouts {
    private static final ClientLogger LOGGER = new ClientLogger(DefaultRolloutsImpl.class);

    private final DefaultRolloutsClient innerClient;

    private final com.azure.resourcemanager.providerhub.ProviderHubManager serviceManager;

    public DefaultRolloutsImpl(
        DefaultRolloutsClient innerClient, com.azure.resourcemanager.providerhub.ProviderHubManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<DefaultRollout> getWithResponse(String providerNamespace, String rolloutName, Context context) {
        Response<DefaultRolloutInner> inner =
            this.serviceClient().getWithResponse(providerNamespace, rolloutName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new DefaultRolloutImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public DefaultRollout get(String providerNamespace, String rolloutName) {
        DefaultRolloutInner inner = this.serviceClient().get(providerNamespace, rolloutName);
        if (inner != null) {
            return new DefaultRolloutImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public PagedIterable<DefaultRollout> listByProviderRegistration(String providerNamespace) {
        PagedIterable<DefaultRolloutInner> inner = this.serviceClient().listByProviderRegistration(providerNamespace);
        return Utils.mapPage(inner, inner1 -> new DefaultRolloutImpl(inner1, this.manager()));
    }

    public PagedIterable<DefaultRollout> listByProviderRegistration(String providerNamespace, Context context) {
        PagedIterable<DefaultRolloutInner> inner =
            this.serviceClient().listByProviderRegistration(providerNamespace, context);
        return Utils.mapPage(inner, inner1 -> new DefaultRolloutImpl(inner1, this.manager()));
    }

    public Response<Void> stopWithResponse(String providerNamespace, String rolloutName, Context context) {
        return this.serviceClient().stopWithResponse(providerNamespace, rolloutName, context);
    }

    public void stop(String providerNamespace, String rolloutName) {
        this.serviceClient().stop(providerNamespace, rolloutName);
    }

    public DefaultRollout getById(String id) {
        String providerNamespace = Utils.getValueFromIdByName(id, "providerRegistrations");
        if (providerNamespace == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'providerRegistrations'.",
                                id)));
        }
        String rolloutName = Utils.getValueFromIdByName(id, "defaultRollouts");
        if (rolloutName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'defaultRollouts'.", id)));
        }
        return this.getWithResponse(providerNamespace, rolloutName, Context.NONE).getValue();
    }

    public Response<DefaultRollout> getByIdWithResponse(String id, Context context) {
        String providerNamespace = Utils.getValueFromIdByName(id, "providerRegistrations");
        if (providerNamespace == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'providerRegistrations'.",
                                id)));
        }
        String rolloutName = Utils.getValueFromIdByName(id, "defaultRollouts");
        if (rolloutName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'defaultRollouts'.", id)));
        }
        return this.getWithResponse(providerNamespace, rolloutName, context);
    }

    public void deleteById(String id) {
        String providerNamespace = Utils.getValueFromIdByName(id, "providerRegistrations");
        if (providerNamespace == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'providerRegistrations'.",
                                id)));
        }
        String rolloutName = Utils.getValueFromIdByName(id, "defaultRollouts");
        if (rolloutName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'defaultRollouts'.", id)));
        }
        this.deleteByResourceGroupWithResponse(providerNamespace, rolloutName, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(String id, Context context) {
        String providerNamespace = Utils.getValueFromIdByName(id, "providerRegistrations");
        if (providerNamespace == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'providerRegistrations'.",
                                id)));
        }
        String rolloutName = Utils.getValueFromIdByName(id, "defaultRollouts");
        if (rolloutName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'defaultRollouts'.", id)));
        }
        return this.deleteByResourceGroupWithResponse(providerNamespace, rolloutName, context);
    }

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

    private com.azure.resourcemanager.providerhub.ProviderHubManager manager() {
        return this.serviceManager;
    }

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