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

package com.azure.resourcemanager.advisor.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.advisor.fluent.SuppressionsClient;
import com.azure.resourcemanager.advisor.fluent.models.SuppressionContractInner;
import com.azure.resourcemanager.advisor.models.SuppressionContract;
import com.azure.resourcemanager.advisor.models.Suppressions;

public final class SuppressionsImpl implements Suppressions {
    private static final ClientLogger LOGGER = new ClientLogger(SuppressionsImpl.class);

    private final SuppressionsClient innerClient;

    private final com.azure.resourcemanager.advisor.AdvisorManager serviceManager;

    public SuppressionsImpl(
        SuppressionsClient innerClient, com.azure.resourcemanager.advisor.AdvisorManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<SuppressionContract> getWithResponse(
        String resourceUri, String recommendationId, String name, Context context) {
        Response<SuppressionContractInner> inner =
            this.serviceClient().getWithResponse(resourceUri, recommendationId, name, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new SuppressionContractImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public SuppressionContract get(String resourceUri, String recommendationId, String name) {
        SuppressionContractInner inner = this.serviceClient().get(resourceUri, recommendationId, name);
        if (inner != null) {
            return new SuppressionContractImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> deleteWithResponse(
        String resourceUri, String recommendationId, String name, Context context) {
        return this.serviceClient().deleteWithResponse(resourceUri, recommendationId, name, context);
    }

    public void delete(String resourceUri, String recommendationId, String name) {
        this.serviceClient().delete(resourceUri, recommendationId, name);
    }

    public PagedIterable<SuppressionContract> list() {
        PagedIterable<SuppressionContractInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new SuppressionContractImpl(inner1, this.manager()));
    }

    public PagedIterable<SuppressionContract> list(Integer top, String skipToken, Context context) {
        PagedIterable<SuppressionContractInner> inner = this.serviceClient().list(top, skipToken, context);
        return Utils.mapPage(inner, inner1 -> new SuppressionContractImpl(inner1, this.manager()));
    }

    public SuppressionContract getById(String id) {
        String resourceUri =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "resourceUri");
        if (resourceUri == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String recommendationId =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "recommendationId");
        if (recommendationId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'recommendations'.", id)));
        }
        String name =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "name");
        if (name == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'suppressions'.", id)));
        }
        return this.getWithResponse(resourceUri, recommendationId, name, Context.NONE).getValue();
    }

    public Response<SuppressionContract> getByIdWithResponse(String id, Context context) {
        String resourceUri =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "resourceUri");
        if (resourceUri == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String recommendationId =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "recommendationId");
        if (recommendationId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'recommendations'.", id)));
        }
        String name =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "name");
        if (name == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'suppressions'.", id)));
        }
        return this.getWithResponse(resourceUri, recommendationId, name, context);
    }

    public void deleteById(String id) {
        String resourceUri =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "resourceUri");
        if (resourceUri == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String recommendationId =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "recommendationId");
        if (recommendationId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'recommendations'.", id)));
        }
        String name =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "name");
        if (name == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'suppressions'.", id)));
        }
        this.deleteWithResponse(resourceUri, recommendationId, name, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(String id, Context context) {
        String resourceUri =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "resourceUri");
        if (resourceUri == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'resourceUri'.", id)));
        }
        String recommendationId =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "recommendationId");
        if (recommendationId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'recommendations'.", id)));
        }
        String name =
            Utils
                .getValueFromIdByParameterName(
                    id,
                    "/{resourceUri}/providers/Microsoft.Advisor/recommendations/{recommendationId}/suppressions/{name}",
                    "name");
        if (name == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'suppressions'.", id)));
        }
        return this.deleteWithResponse(resourceUri, recommendationId, name, context);
    }

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

    private com.azure.resourcemanager.advisor.AdvisorManager manager() {
        return this.serviceManager;
    }

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