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

package com.azure.resourcemanager.logic.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.logic.fluent.IntegrationServiceEnvironmentsClient;
import com.azure.resourcemanager.logic.fluent.models.IntegrationServiceEnvironmentInner;
import com.azure.resourcemanager.logic.models.IntegrationServiceEnvironment;
import com.azure.resourcemanager.logic.models.IntegrationServiceEnvironments;

public final class IntegrationServiceEnvironmentsImpl implements IntegrationServiceEnvironments {
    private static final ClientLogger LOGGER = new ClientLogger(IntegrationServiceEnvironmentsImpl.class);

    private final IntegrationServiceEnvironmentsClient innerClient;

    private final com.azure.resourcemanager.logic.LogicManager serviceManager;

    public IntegrationServiceEnvironmentsImpl(
        IntegrationServiceEnvironmentsClient innerClient, com.azure.resourcemanager.logic.LogicManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<IntegrationServiceEnvironment> list() {
        PagedIterable<IntegrationServiceEnvironmentInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new IntegrationServiceEnvironmentImpl(inner1, this.manager()));
    }

    public PagedIterable<IntegrationServiceEnvironment> list(Integer top, Context context) {
        PagedIterable<IntegrationServiceEnvironmentInner> inner = this.serviceClient().list(top, context);
        return Utils.mapPage(inner, inner1 -> new IntegrationServiceEnvironmentImpl(inner1, this.manager()));
    }

    public PagedIterable<IntegrationServiceEnvironment> listByResourceGroup(String resourceGroup) {
        PagedIterable<IntegrationServiceEnvironmentInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroup);
        return Utils.mapPage(inner, inner1 -> new IntegrationServiceEnvironmentImpl(inner1, this.manager()));
    }

    public PagedIterable<IntegrationServiceEnvironment> listByResourceGroup(
        String resourceGroup, Integer top, Context context) {
        PagedIterable<IntegrationServiceEnvironmentInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroup, top, context);
        return Utils.mapPage(inner, inner1 -> new IntegrationServiceEnvironmentImpl(inner1, this.manager()));
    }

    public Response<IntegrationServiceEnvironment> getByResourceGroupWithResponse(
        String resourceGroup, String integrationServiceEnvironmentName, Context context) {
        Response<IntegrationServiceEnvironmentInner> inner =
            this
                .serviceClient()
                .getByResourceGroupWithResponse(resourceGroup, integrationServiceEnvironmentName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new IntegrationServiceEnvironmentImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public IntegrationServiceEnvironment getByResourceGroup(
        String resourceGroup, String integrationServiceEnvironmentName) {
        IntegrationServiceEnvironmentInner inner =
            this.serviceClient().getByResourceGroup(resourceGroup, integrationServiceEnvironmentName);
        if (inner != null) {
            return new IntegrationServiceEnvironmentImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void deleteByResourceGroup(String resourceGroup, String integrationServiceEnvironmentName) {
        this.serviceClient().delete(resourceGroup, integrationServiceEnvironmentName);
    }

    public Response<Void> restartWithResponse(
        String resourceGroup, String integrationServiceEnvironmentName, Context context) {
        return this.serviceClient().restartWithResponse(resourceGroup, integrationServiceEnvironmentName, context);
    }

    public void restart(String resourceGroup, String integrationServiceEnvironmentName) {
        this.serviceClient().restart(resourceGroup, integrationServiceEnvironmentName);
    }

    public IntegrationServiceEnvironment getById(String id) {
        String resourceGroup = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroup == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String integrationServiceEnvironmentName = Utils.getValueFromIdByName(id, "integrationServiceEnvironments");
        if (integrationServiceEnvironmentName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment"
                                    + " 'integrationServiceEnvironments'.",
                                id)));
        }
        return this
            .getByResourceGroupWithResponse(resourceGroup, integrationServiceEnvironmentName, Context.NONE)
            .getValue();
    }

    public Response<IntegrationServiceEnvironment> getByIdWithResponse(String id, Context context) {
        String resourceGroup = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroup == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String integrationServiceEnvironmentName = Utils.getValueFromIdByName(id, "integrationServiceEnvironments");
        if (integrationServiceEnvironmentName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment"
                                    + " 'integrationServiceEnvironments'.",
                                id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroup, integrationServiceEnvironmentName, context);
    }

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

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

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

    private com.azure.resourcemanager.logic.LogicManager manager() {
        return this.serviceManager;
    }

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