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

package com.azure.resourcemanager.operationsmanagement.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.operationsmanagement.fluent.ManagementConfigurationsClient;
import com.azure.resourcemanager.operationsmanagement.fluent.models.ManagementConfigurationInner;
import com.azure.resourcemanager.operationsmanagement.fluent.models.ManagementConfigurationPropertiesListInner;
import com.azure.resourcemanager.operationsmanagement.models.ManagementConfiguration;
import com.azure.resourcemanager.operationsmanagement.models.ManagementConfigurationPropertiesList;
import com.azure.resourcemanager.operationsmanagement.models.ManagementConfigurations;

public final class ManagementConfigurationsImpl implements ManagementConfigurations {
    private static final ClientLogger LOGGER = new ClientLogger(ManagementConfigurationsImpl.class);

    private final ManagementConfigurationsClient innerClient;

    private final com.azure.resourcemanager.operationsmanagement.OperationsManagementManager serviceManager;

    public ManagementConfigurationsImpl(
        ManagementConfigurationsClient innerClient,
        com.azure.resourcemanager.operationsmanagement.OperationsManagementManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ManagementConfigurationPropertiesList> listBySubscriptionWithResponse(Context context) {
        Response<ManagementConfigurationPropertiesListInner> inner =
            this.serviceClient().listBySubscriptionWithResponse(context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ManagementConfigurationPropertiesListImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ManagementConfigurationPropertiesList listBySubscription() {
        ManagementConfigurationPropertiesListInner inner = this.serviceClient().listBySubscription();
        if (inner != null) {
            return new ManagementConfigurationPropertiesListImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public ManagementConfiguration getByResourceGroup(String resourceGroupName, String managementConfigurationName) {
        ManagementConfigurationInner inner =
            this.serviceClient().getByResourceGroup(resourceGroupName, managementConfigurationName);
        if (inner != null) {
            return new ManagementConfigurationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.operationsmanagement.OperationsManagementManager manager() {
        return this.serviceManager;
    }

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