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

package com.azure.resourcemanager.workloads.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.workloads.fluent.ProviderInstancesClient;
import com.azure.resourcemanager.workloads.fluent.models.OperationStatusResultInner;
import com.azure.resourcemanager.workloads.fluent.models.ProviderInstanceInner;
import com.azure.resourcemanager.workloads.models.OperationStatusResult;
import com.azure.resourcemanager.workloads.models.ProviderInstance;
import com.azure.resourcemanager.workloads.models.ProviderInstances;

public final class ProviderInstancesImpl implements ProviderInstances {
    private static final ClientLogger LOGGER = new ClientLogger(ProviderInstancesImpl.class);

    private final ProviderInstancesClient innerClient;

    private final com.azure.resourcemanager.workloads.WorkloadsManager serviceManager;

    public ProviderInstancesImpl(
        ProviderInstancesClient innerClient, com.azure.resourcemanager.workloads.WorkloadsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<ProviderInstance> list(String resourceGroupName, String monitorName) {
        PagedIterable<ProviderInstanceInner> inner = this.serviceClient().list(resourceGroupName, monitorName);
        return Utils.mapPage(inner, inner1 -> new ProviderInstanceImpl(inner1, this.manager()));
    }

    public PagedIterable<ProviderInstance> list(String resourceGroupName, String monitorName, Context context) {
        PagedIterable<ProviderInstanceInner> inner = this.serviceClient().list(resourceGroupName, monitorName, context);
        return Utils.mapPage(inner, inner1 -> new ProviderInstanceImpl(inner1, this.manager()));
    }

    public Response<ProviderInstance> getWithResponse(
        String resourceGroupName, String monitorName, String providerInstanceName, Context context) {
        Response<ProviderInstanceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, monitorName, providerInstanceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ProviderInstanceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ProviderInstance get(String resourceGroupName, String monitorName, String providerInstanceName) {
        ProviderInstanceInner inner = this.serviceClient().get(resourceGroupName, monitorName, providerInstanceName);
        if (inner != null) {
            return new ProviderInstanceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationStatusResult delete(String resourceGroupName, String monitorName, String providerInstanceName) {
        OperationStatusResultInner inner =
            this.serviceClient().delete(resourceGroupName, monitorName, providerInstanceName);
        if (inner != null) {
            return new OperationStatusResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationStatusResult delete(
        String resourceGroupName, String monitorName, String providerInstanceName, Context context) {
        OperationStatusResultInner inner =
            this.serviceClient().delete(resourceGroupName, monitorName, providerInstanceName, context);
        if (inner != null) {
            return new OperationStatusResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.workloads.WorkloadsManager manager() {
        return this.serviceManager;
    }

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