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

package com.azure.resourcemanager.desktopvirtualization.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.desktopvirtualization.fluent.ApplicationGroupsClient;
import com.azure.resourcemanager.desktopvirtualization.fluent.models.ApplicationGroupInner;
import com.azure.resourcemanager.desktopvirtualization.models.ApplicationGroup;
import com.azure.resourcemanager.desktopvirtualization.models.ApplicationGroups;

public final class ApplicationGroupsImpl implements ApplicationGroups {
    private static final ClientLogger LOGGER = new ClientLogger(ApplicationGroupsImpl.class);

    private final ApplicationGroupsClient innerClient;

    private final com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager;

    public ApplicationGroupsImpl(
        ApplicationGroupsClient innerClient,
        com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

    public ApplicationGroup getByResourceGroup(String resourceGroupName, String applicationGroupName) {
        ApplicationGroupInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, applicationGroupName);
        if (inner != null) {
            return new ApplicationGroupImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public PagedIterable<ApplicationGroup> listByResourceGroup(String resourceGroupName) {
        PagedIterable<ApplicationGroupInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new ApplicationGroupImpl(inner1, this.manager()));
    }

    public PagedIterable<ApplicationGroup> listByResourceGroup(
        String resourceGroupName,
        String filter,
        Integer pageSize,
        Boolean isDescending,
        Integer initialSkip,
        Context context) {
        PagedIterable<ApplicationGroupInner> inner =
            this
                .serviceClient()
                .listByResourceGroup(resourceGroupName, filter, pageSize, isDescending, initialSkip, context);
        return Utils.mapPage(inner, inner1 -> new ApplicationGroupImpl(inner1, this.manager()));
    }

    public PagedIterable<ApplicationGroup> list() {
        PagedIterable<ApplicationGroupInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new ApplicationGroupImpl(inner1, this.manager()));
    }

    public PagedIterable<ApplicationGroup> list(String filter, Context context) {
        PagedIterable<ApplicationGroupInner> inner = this.serviceClient().list(filter, context);
        return Utils.mapPage(inner, inner1 -> new ApplicationGroupImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager manager() {
        return this.serviceManager;
    }

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